Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Calling free() on an uninitialised pointer?

Reply
Thread Tools

Calling free() on an uninitialised pointer?

 
 
santosh
Guest
Posts: n/a
 
      12-04-2005
Hi,

If I call free() with a uninitialised pointer, will the program
state become undefined, or will free() return harmlessly?

Incidentally, is there a way in Standard C to determine
weather a pointer points to a valid block of allocated
memory?

Thanks for your time.

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      12-04-2005
santosh wrote:

> If I call free() with a uninitialised pointer, will the program
> state become undefined, or will free() return harmlessly?


a program that does this exhibits undefined behaviour. It *may*
return harmlessly or give a segmentation error or something even
nastier. Don't ever do this.

what is safe is:-
p = NULL;
free(p);

> Incidentally, is there a way in Standard C to determine
> weather a pointer points to a valid block of allocated
> memory?


no


--
Nick Keighley

I don't use drugs, my dreams are frightening enough. --M. C. Escher

 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      12-04-2005
santosh a écrit :
> Hi,
>
> If I call free() with a uninitialised pointer, will the program
> state become undefined, or will free() return harmlessly?


Undefined behaviour. A serious bug. Don't do that. Never.

> Incidentally, is there a way in Standard C to determine
> weather a pointer points to a valid block of allocated
> memory?


Make it point to NULL by default or once free() has been performed. I
recommend this construction :

free(p), p = NULL;

It makes it easy to test, and it happens that free (NULL) is defined and
harmless.

--
A+

Emmanuel Delahaye
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      12-04-2005
In article <(E-Mail Removed). com>,
"santosh" <(E-Mail Removed)> wrote:

> Hi,
>
> If I call free() with a uninitialised pointer, will the program
> state become undefined, or will free() return harmlessly?


First, it is undefined behavior, so _anything_ can happen. Especially,
it might return harmlessly every time you do this while you are
developping your program, and the first time it is used seriously it
could crash the computer it is running on and cause considerable
financial damage. Don't do this.

What exactly happens depends on your implementation. The value of an
uninitialised pointer variable might by great coincidence be the same as
some pointer variable that was initialised by calling malloc. In that
case, calling free () will free that pointer, with likely harmful
consequences. That is not as unlikely as it seems, because an
uninitialised variable might be kept in a register that happens to
contain another completely unrelated variable of the calling function.
 
Reply With Quote
 
Daniel Rudy
Guest
Posts: n/a
 
      12-04-2005
At about the time of 12/4/2005 2:17 AM, santosh stated the following:

> Hi,
>
> If I call free() with a uninitialised pointer, will the program
> state become undefined, or will free() return harmlessly?


Maybe, maybe not. The behavior is undefined. Undefined meaning that
*ANYTHING* can happen, including your harddisk being reformatted (Which
is admittidly an extream case).

> Incidentally, is there a way in Standard C to determine
> weather a pointer points to a valid block of allocated
> memory?


What I do is initialize all pointers to NULL when my program starts and
on entries to functions right in the initialzation section. This is a
good practice as it gives you a known starting point to go from.

int ptest(int size)
{
void *p;

p = NULL;
p = malloc(size);
if (p == NULL) return(-1);
/* perform some action here */
free(p);
return(0);
}

In this case here, p can never have an unknown value. If the call to
malloc fails, then malloc returns NULL, which can be tested for. Get
into the habbit of setting all your pointers to NULL before and after
you use them.

> Thanks for your time.
>


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      12-04-2005
santosh wrote:
> Hi,
>
> If I call free() with a uninitialised pointer, will the program
> state become undefined, or will free() return harmlessly?
>

Undefined behavior. You can call free() once and only once on a pointer
value returned by malloc & company. You can free(NULL) with no effect.

> Incidentally, is there a way in Standard C to determine
> weather a pointer points to a valid block of allocated
> memory?
>

There is no way to determine by inspection the validity of any pointer
value. The only pointer value that you are guaranteed to know something
about is NULL and that it doesn't 'point' to anything.

> Thanks for your time.
>

I've never had any real problem with the malloc()/free() stuff probably
because of programming habit. I always write the beginning and end of
functions at the same time. For example I begin a generic program with..

#include <stdio.h>

int main(void) {

return 0;
}

...and only then, fill it in with code. I tend to do the same with
malloc/free and fopen/fclose. Properly written, whether to free or
fclose is not the question, just do it.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      12-04-2005
Emmanuel Delahaye wrote:
> santosh a écrit :
>
>> Hi,
>>
>> If I call free() with a uninitialised pointer, will the program
>> state become undefined, or will free() return harmlessly?

>
>
> Undefined behaviour. A serious bug. Don't do that. Never.
>
>> Incidentally, is there a way in Standard C to determine
>> weather a pointer points to a valid block of allocated
>> memory?

>
>
> Make it point to NULL by default or once free() has been performed. I
> recommend this construction :
>
> free(p), p = NULL;
>
> It makes it easy to test, and it happens that free (NULL) is defined and
> harmless.


Unfortunately, there are cases where doing so will mask a logic error
wherein otherwise a pointer would be free()-ed twice. So be careful.

HTH,
--ag

>



--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-04-2005
"santosh" <(E-Mail Removed)> writes:
> If I call free() with a uninitialised pointer, will the program
> state become undefined, or will free() return harmlessly?


This is, of course undefined behavior, but not because of the call to
free(). Any attempt to evaluate an uninitialized pointer variable
invokes undefined behavior. For example:

int *ptr; /* uninitialized */
if (ptr == NULL) ...; /* undefined behavior */

Realistically, the call to free() is more likely to cause something
bad to happen than the evaluation of the argument.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      12-04-2005
On 2005-12-04, Artie Gold <(E-Mail Removed)> wrote:
> Emmanuel Delahaye wrote:
>> santosh a écrit :
>>
>>> Hi,
>>>
>>> If I call free() with a uninitialised pointer, will the program
>>> state become undefined, or will free() return harmlessly?

>>
>>
>> Undefined behaviour. A serious bug. Don't do that. Never.
>>
>>> Incidentally, is there a way in Standard C to determine
>>> weather a pointer points to a valid block of allocated
>>> memory?

>>
>>
>> Make it point to NULL by default or once free() has been performed. I
>> recommend this construction :
>>
>> free(p), p = NULL;
>>
>> It makes it easy to test, and it happens that free (NULL) is defined and
>> harmless.

>
> Unfortunately, there are cases where doing so will mask a logic error
> wherein otherwise a pointer would be free()-ed twice. So be careful.


it'll mask it or solve it? setting it to null guarantees that it won't
be freed twice, so this is hardly a "mask" - it actually changes the
logic to prevent the error
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-05-2005
Keith Thompson wrote:
> "santosh" <(E-Mail Removed)> writes:
> > If I call free() with a uninitialised pointer, will the program
> > state become undefined, or will free() return harmlessly?

>
> This is, of course undefined behavior, but not because of the call to
> free(). Any attempt to evaluate an uninitialized pointer variable
> invokes undefined behavior. For example:
>
> int *ptr; /* uninitialized */
> if (ptr == NULL) ...; /* undefined behavior */
>
> Realistically, the call to free() is more likely to cause something
> bad to happen than the evaluation of the argument.


Thank you all for your clear replies.

To sum up, if I understand correctly, good programming implies
that a pointer must either be NULL or must contain a known
value, returned by malloc() and co.

And if I do want to manipulate the lvalue, it is better to make a
copy of the original pointer and deal with it. The lvalue must also
be unchanged (i.e. the one that malloc() returned), when the pointer
is passed to free().

Are the above assumptions correct?

Is even using the address of operator on an uninitialised pointer
undefined?

Thanks all.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sendto(msg) points to uninitialised byte(s) BlueJ C Programming 6 10-19-2007 02:58 PM
uninitialised variable but NO error geek.arnuld@gmail.com C++ 2 02-04-2007 11:05 AM
uninitialised variable but NO error geek.arnuld@gmail.com C++ 2 02-04-2007 08:19 AM
value of an uninitialised variable Andy Fish XML 7 01-10-2005 04:37 PM
C++: Uninitialised Variable Passed as a Parm Gene Wirchenko C++ 22 12-19-2003 02:26 PM



Advertisments