delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/22/01:49:19

From: alainm AT news DOT RISQ DOT QC DOT CA (Alain Magloire)
Subject: Re: Problems with MALLOC and FREE
Newsgroups: comp.os.msdos.djgpp
References: <Pine DOT SUN DOT 3 DOT 91 DOT 991209160149 DOT 22560D-100000 AT is> <82rh3j$sfu AT cantine DOT wu-wien DOT ac DOT at>
X-Newsreader: TIN [version 1.2 PL2]
Lines: 101
Message-ID: <ngZ74.59482$0c2.439871@carnaval.risq.qc.ca>
Date: Wed, 22 Dec 1999 05:24:35 GMT
NNTP-Posting-Host: 132.206.63.174
X-Complaints-To: abuse AT mcgill DOT ca
X-Trace: carnaval.risq.qc.ca 945840275 132.206.63.174 (Wed, 22 Dec 1999 00:24:35 EST)
NNTP-Posting-Date: Wed, 22 Dec 1999 00:24:35 EST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


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