www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/01/27/22:17:22

Message-ID: <3E35F500.3040606@tutopia.com>
Date: Mon, 27 Jan 2003 21:12:00 -0600
From: fru <elfrucool AT tutopia DOT com>
User-Agent: Mozilla/5.0 (Windows; U; Win98; es-HN; rv:1.3a) Gecko/20021212
X-Accept-Language: es-mx, en-us, es-co, es-ar, es,
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: malloc/free crash OOPS!
References: <3E3560E0 DOT 3070200 AT tutopia DOT com> <b14909$p77$1 AT knossos DOT btinternet DOT com>
In-Reply-To: <b14909$p77$1@knossos.btinternet.com>
Reply-To: djgpp AT delorie DOT com

oops,
I found the mistake: strings
1. a string has 2 parts: the text and the null terminaror character '\x0'
2. malloc( strlen( str ) ) is too bad because you need 1 byte extra to 
'\x0' character
3. solution:  malloc( strlen( str ) + 1 )

sorry everybody :-$ (maybe it helped someone else)
thanks to Hans-Bernhard Broeker and Tim

>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
>>
>>
>>    
>>
>
>  
>
>fru <elfrucool AT tutopia DOT com> wrote:
>  
>
>>> 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 .....
>>    
>>
>
>This almost certainly means you did something wrong with the pointers
>returned by one or more of your malloc() calls.  See the DJGPP FAQ,
>issue #9.2: Programs that crash in `malloc' or `free'.
>
>I definitely recommend YAMD for findin this kind of bug.  Give it a try.
> -- Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de) Even if all 
> the snow were burnt, ashes would remain.
>
>
>  
>


- Raw text -


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