delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/28/19:23:21

Date: Tue, 29 Feb 2000 05:26:09 +0600 (LKT)
From: Kalum Somaratna aka Grendel <kalum AT crosswinds DOT net>
X-Sender: root AT darkstar DOT grendel DOT net
To: djgpp AT delorie DOT com
cc: Marcus <mesthlm AT hem DOT passagen DOT se>
Subject: Re: Prevent crash on free()
In-Reply-To: <89ee3g$8pf$1@zingo.tninet.se>
Message-ID: <Pine.LNX.4.10.10002290514060.1008-100000@darkstar.grendel.net>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: dj-admin AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Mon, 28 Feb 2000, Marcus wrote:

> I take it again. I have a string, ok?
> How do I know if this string is not automatic freed?
> 
> If I have a function like this
> func (char* str)
> 
> Is it impossible to know if the function have been called this way
> func ("...");
> or
> char* a = malloc(10);
> func (a);
> ?
> 
> Do you understand what I mean?

Well Marcus, I think that you want to know whether the argument that is
passed to your func is a pointer that has been allocated memory or not
(maybe a pointer that has already being freed or worse still one that has 
not been allocated). 

You wnat to know wehther it is a char *p, or a char *p=malloc(10); 
Am I correct?

Well you can check to see wether the argument to the function ie char *p
is this case is NULL (0) or not. If it is NULL (p=0) then that means that
this should _not_ be freed. And that this is due to a error in the
original program. 

so example code would be

free_func (char * str)
{
if (str==0) /* don't free this*/
{
 printf("error null pointer passed, this should be a programmer error");
 exit(1); /* exit or return a value indicaing a error
}

free (p); /* else free the pointer and return
}


Apart from that quick check I am not aware off anyother method of reliably
telling wether a pointer is allocated from malloc and is safe to free it.

Hope this is what you want,
Grendel
 
> Hi, I'm a signature virus. plz set me as your signature and help me spread
> :)

Hi, I'm a signature virus. plz set me as your signature and help me spread
:)


- Raw text -


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