Sign in to follow this  

free()

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ye olde undefined behaviour. The C Standard allows anything to happen. Most likely your program will crash. Alternatively it may appear to do nothing. The behaviour may even change from one run of the program to another. Just don't do it!

Σnigma

Share this post


Link to post
Share on other sites
Random behaviour or exception. The best thing to do is initialise the pointer then check it before you free it.

int *pTemp = 0;

// Check if not NULL
if(pTemp)
{
free(pTemp);
pTemp = 0;
}


Share this post


Link to post
Share on other sites
There is never any need to check a pointer for nullness when using free, delete or delete[]. All three memory deallocation functions/operators are required to be equivalent to a no-op if passed a null pointer.

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
What would happen if i tried to free a pointer which isnt pointing anywhere yet?

"[It] could start a chain reaction that would unravel the very fabric of the space time continuum, and destroy the entire universe! Granted, that's a worse case scenario. The destruction might in fact be very localized, limited to our own galaxy."

Quote:
Original post by biscuitz
so free(bob) wouldnt be horrible?

Uninitalized variables contain a random value (i.e. whatever was in memory at that location before). Assuming that they will be zero is a common, yet fatal, mistake.

Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
so free(bob) wouldnt be horrible?

int * bob;
free(bob);
is undefined.

int * bob = 0;
free(bob);
is perfectly fine.

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by yaustar
Random behaviour or exception.

An exception is highly unlikely (though technically possible because of that undefined behavior thing). The functions in the C standard library inherited by C++ are mandated to not use the C++ exception mechanisms for error reporting.

Share this post


Link to post
Share on other sites
I'm not sure about what I'm gonna say but if I'm right I think its worth mentioning (*so check it out!*)

On latest versions of visual studio (if u are using that of course !) and with proper runtim checks activated you would get a nice popup saying 'runtime check failed, variable being used without being initialized' (or something like this)

That was not exactly your question I think, but it could be good to know anyway.

Share this post


Link to post
Share on other sites
the undefined behavoir is a result of the random value that existed in the address (it's own
address , cause it takes the last value) before the program was intilized . so that value
can be for some despensible application (e.g WMP) or it could have been created for a more
important task. anyway you see it , your safer setting it to zero when it's initlized.

Share this post


Link to post
Share on other sites

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this