Mail Archives: djgpp/1999/12/22/01:49:19
Wormy (wormy AT technologist DOT com) wrote:
: 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?
So you are saying if you do the code below, it will crash.
Or rather, you allocate, do stuff in between that may corrupt the
memory and you see a random crash.
If the code below does not crash then, your problem is elsewhere.
--------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main ()
{
char *mem[12000];
int i;
/* prolog */
for (i = 0; i < 12000; i++)
{
if ((mem[i] = (char *)malloc(64*sizeof(char))) == NULL)
{
perror ("Malloc failed");
exit (1);
}
else
memset(mem[i], 0, 64*sizeof(char));
}
/* epilogue */
for (i = 0; i < 12000; i++)
{
if (mem[i] != NULL)
free (mem[i]);
}
return 0;
}
--------------
--
au revoir, alain
----
Aussi haut que l'on soit assis, on est toujours assis que sur son cul !!!
- Raw text -