From: "Wormy" Newsgroups: comp.os.msdos.djgpp Subject: Re: Problems with MALLOC and FREE Date: Fri, 10 Dec 1999 19:33:51 +0100 Organization: University of Economics and Business Administration, Vienna, Austria Lines: 56 Message-ID: <82rh3j$sfu@cantine.wu-wien.ac.at> References: NNTP-Posting-Host: 212186012098.11.wu-wien.teleweb.at X-Newsreader: Microsoft Outlook Express 4.72.3110.1 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Eli Zaretskii schrieb in Nachricht ... > >On Thu, 9 Dec 1999, Wormy wrote: > >> But if, at shutdown, I do >> >> for (x=0; x<4096; x++) >> if (mem[x] != NULL) >> free (mem[x]); >> >> IT HAPPENS... WELL, MORE THAN ONLY SOMETIMES THAT I GET A SIGSEGV!!!!! >> WHY IS THAT??? > >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??! 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! Funny thing is it happens even if I check if mem[i] != NULL ----> So the program shouldn't free any memory that wasn't freed yet... Could it be a bug with DJGPP or is it maybe my computer? Hm... maybe I should test it on several computers?