Mail Archives: djgpp/2000/02/28/19:23:21
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 -