www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/01/27/16:45:13

From: "The Big Z" <bigz AT beebleB DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: malloc/free crash
Date: Mon, 27 Jan 2003 21:40:25 +0000 (UTC)
Organization: BT Openworld
Lines: 63
Message-ID: <b14909$p77$1@knossos.btinternet.com>
References: <3E3560E0 DOT 3070200 AT tutopia DOT com>
NNTP-Posting-Host: host217-35-151-36.in-addr.btopenworld.com
X-Trace: knossos.btinternet.com 1043703625 25831 217.35.151.36 (27 Jan 2003 21:40:25 GMT)
X-Complaints-To: news-complaints AT lists DOT btinternet DOT com
NNTP-Posting-Date: Mon, 27 Jan 2003 21:40:25 +0000 (UTC)
X-Newsreader: Microsoft Outlook Express 6.00.2720.3000
X-MSMail-Priority: Normal
X-Priority: 3
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I had a simular problem, it turned out the I was freeing an unallocated
pointer! Although I was testing that the pointer was !NULL before issuing
free(),  I was assuming that free() would nulify the pointer and it does
not - The trick is to reassign NULL to the pointer again after the free().
That way if you attempt to issue free() again before reallocating the
pointer you'll get away with it :-)  Something like this....

//Defining the pointer
char *mypointer = NULL;
...
//Allocating the memory
    if(mypointer = = NULL)
        mypointer = (char *) malloc(size);
...
//Using the memory
    if (mypointer != NULL)
        memcpy(mypointer,something,size);...
//Freeing the memory
    if (mypointer != NULL)
        {
        free (mypointer);
        mypointer = NULL;   //<=== THIS IS THE MAGIC LINE....
        }

I now always check for NULL before a malloc(), check for !NULL before using
a pointer or issuing a free() and assign NULL to the pointer again after the
free() - It solved all my wierd dynamic allocation crashes in one go!!


Regards


Tim


"fru" <elfrucool AT tutopia DOT com> wrote in message
news:3E3560E0 DOT 3070200 AT tutopia DOT com...
> I have runtime problems with malloc and free functions, simple:
> when I use malloc/free several times, there is a moment when my
> program crashes and display a message: page segmentation fault at .....
>
> when I compile the same program with borland c++ 3.0 (DOS), there
> is no problem, and there is no problem with cygwin
>
> I'm using the latest version of djgpp (binaries) but I had the problem
with
> a version from 1998 too
>
> ¿who wants the source to check it? It's simple and short to analize (the
> bug part)
> but (I think) too large to post in the mailing list
>
>   source: '.c' files '.h' files and Makefile
>
> comments: <elfrucool AT tutopia DOT com> (please add in the subject something
> like: DJGPP's list)
>
> ***Note 1: Sorry about my English
> ***Note 2: Spanish speackers: pueden comentarlo en español
>
>


- Raw text -


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