Date: Sun, 12 Dec 1999 14:05:50 +0200 (IST) From: Eli Zaretskii X-Sender: eliz AT is To: Wormy cc: djgpp AT delorie DOT com Subject: Re: Problems with MALLOC and FREE In-Reply-To: <82rh3j$sfu@cantine.wu-wien.ac.at> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk On Fri, 10 Dec 1999, Wormy wrote: > >This usually means that your program either overwrites the buffers that > >you allocate (like if it puts more than 64 bytes into each 64-byte > >buffer), or that it free()'s some of the buffers more than once. > > BUT WHY DOES IT FREE MORE THAN ONCE??! Most probably, because of bug(s) in your code. > It even happens sometimes if I execute the following (one after the other): > > char *mem[12000]; > > for (i=0; i<12000; i++) // THIS PART > ALWAYS WORKS RIGHT ON MY COMP > // > THE MEMORIES ARE PROPERLY ALLOCATED I CAN > // > WRITE AND READ TO THEM > { > if ((mem[i]=(char *)malloc(64*sizeof(char)))==NULL) > HANDLE ERROR > else > memset(mem[i], 0, 64*sizeof(char)); > } > > // AND NOW WITHOUT DOING ANYTHING > > for (i=0; i<12000; i++) > { > if (mem[i] != NULL) > free (mem[i]); > } > > // AND SOMETIMES IT HAPPENS THAT THE PROGRAM SHUTS DOWN WITH A SIGSEGV! Please post the smallest complete program that can be compiled and used to reproduce these problems on your machine. It is very hard to help you without seeing the complete program. I'm guessing that the buggy code is somewhere in the fragments that you omitted.