From: "Ben Peddell" Newsgroups: comp.os.msdos.djgpp References: <3E6225AD DOT 1341CC2B AT ec-red DOT com> <3E62BFC8 DOT 4E12DED8 AT ec-red DOT com> Subject: Re: Problem with malloc/free Lines: 36 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: <7PD8a.59359$jM5.151272@newsfeeds.bigpond.com> Date: Mon, 3 Mar 2003 17:48:49 +1000 NNTP-Posting-Host: 144.134.90.43 X-Trace: newsfeeds.bigpond.com 1046677827 144.134.90.43 (Mon, 03 Mar 2003 18:50:27 EST) NNTP-Posting-Date: Mon, 03 Mar 2003 18:50:27 EST Organization: Telstra BigPond Internet Services (http://www.bigpond.com) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Sergio Aguayo wrote in message news:3E62BFC8 DOT 4E12DED8 AT ec-red 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]); That is in a for loop, isn't it? > > there's where the SIGSEGV occurs. > > Sergio > Check that your reads are not larger than your buffers. If they are, then they could be overwriting the 16 bytes between mallocs. I think that these 16 bytes tell free() how big the buffer is, as well as some other stuff. What about if you added all of the filelengths together, added 80 bytes to that, and then malloc()'d that space. Then use the first 80 bytes for the pointer array. Then set each pointer in the array to the right position within the buffer, so that each pointer points the [length of the previous file] after the pointer to the previous file. When you're finished, all you have to do is free() the pointer array, which will automatically free your entire buffer.