delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/03/02/21:40:36

Sender: root AT delorie DOT com
Message-ID: <3E62BFC8.4E12DED8@ec-red.com>
Date: Sun, 02 Mar 2003 21:36:56 -0500
From: Sergio Aguayo <sergioag AT ec-red DOT com>
Organization: The GIMI-team
X-Mailer: Mozilla 4.73 [en] (X11; U; Linux 2.4.20 i686)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Problem with malloc/free
References: <3E6225AD DOT 1341CC2B AT ec-red DOT com> <Xns933270944F6EBasu1cornelledu AT 132 DOT 236 DOT 56 DOT 8>
Reply-To: djgpp AT delorie DOT com

No, i'm declaring an array of pointers:

    char    *pcode[20];

then i allocate memory for each one in a "for" as follows:

    pcode[i] = (char*)malloc(length of file to load);

when i finish, i call:

   free(pcode[i]);

there's where the SIGSEGV occurs.

Sergio


"A. Sinan Unur" wrote:

> Sergio Aguayo <sergioag AT ec-red DOT com> wrote in news:3E6225AD DOT 1341CC2B AT ec-
> red.com:
>
> > The problem is the following: i'm using DJGPP (the version available
> > right now), i'm using malloc with an array of pointers (20 char *) for
> > loading a file (a total of 20 files at once). The problem comes when i
> > call free to release that memory, there occures a SIGSEGV signal. I'm
> > also using allegro.
>
> without some code that we can compile and diagnose, there is not much we
> can do.
>
> however, you may have only allocated memory for the pointers, and nothing
> for those pointers to point to.
>
> int i;
>
> char **ptr_array = malloc(20 * sizeof(char *));
> /* check if malloc returned NULL and handle the error */
> for(i = 0; i != 20; ++i)
> {
>   ptr_array[i] = malloc(some space);
>   /* check if malloc returned NULL and handle the error */
> }
>
> the codeabove was typed directly into the newsreader, and not tested.
>
> --
> A. Sinan Unur
> asu1 AT c-o-r-n-e-l-l DOT edu
> Remove dashes for address
> Spam bait: mailto:uce AT ftc DOT gov

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019