September 22nd, 2003, 04:16 AM
Freeing a NULL pointer?
If we want to remove a pointer which points to NULL, how would we go about doing that? This is for C anyway. Thanks
September 22nd, 2003, 04:32 AM
i don't know much about c but i believe you could use the malloc and free commands. Have you looked into that?
check this out.http://www.iota-six.co.uk/c/40_memor.htm
Last edited by jonnyfive; September 22nd, 2003 at 04:35 AM.
September 22nd, 2003, 06:15 AM
If the pointer is NULL, there is no memory associated with it (whether it was properly free'ed or not). I believe it is always an error to free/delete a NULL pointer. Just check to see if it is NULL and if so, skip the free.
September 22nd, 2003, 06:25 AM
You cannot 'remove a pointer'. You can free memory that a pointer references if that memory was dynamically allocated. If the pointer has the value NULL (or zero), then it does not point to any allocated memory, so free'ing make no sense, and will cause an error. For safety any pointers you use should be initialised to NULL or zero, and then any free'ing you do should test the pointer first:
if( ptr != 0 )
free( ptr ) ;
ptr = 0 ; // ensure cannot free'd twice
September 22nd, 2003, 09:08 AM
As far as I know, calling free() for a pointer that is NULL does not cause any error. Let me check... Take a look at the MSDN page for free():
"void free( void *memblock );
...If memblock is NULL, the pointer is ignored and free immediately returns...."
September 22nd, 2003, 02:12 PM
Which only begs the question that if it works (and it does I tried it at your suggestion), why is he asking the question?
I know of systems where this does not work (embedded systems admittedly), so I still recommend this approach.
September 23rd, 2003, 12:10 AM
Not all memory functions are that kind
"If the hgblMem parameter is NULL, GlobalFree fails and the system generates an access violation exception."
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
September 23rd, 2003, 04:46 AM
What occurs when you free a NULL pointer is dependent upon you libc implementation. Microsoft likes to protect your from your own idiocy, it would seem. Try this with pretty much any other libc implementation and you'll get into trouble. It's good for a segfault on most UNIX systems.
If you're asking this question at all, expect to see lots and lots of segfaults for a while. You should know what your pointers refer to, and not be freeing them without knowing what you're freeing. The suggestion of zeroing any pointer that has been freed is a good one. It will save you a lot of grief.
Articles and commentary on web development
September 23rd, 2003, 08:00 AM
Are you referring to the free() function? Since free() is part of the C language, I would assume that MS's implementation follows that of the standard. Usually when there is something specific to Microsoft, it is clearly stated as MS specific in the text. For example, the C++ keywords page has a section enclosed in Microsoft Specific..END Microsoft Specific tags.
September 23rd, 2003, 09:40 AM
Pedantic point admittedly, but it is part of the ANSI Standard Library for C, not part of the language itself.
September 23rd, 2003, 10:31 AM
Okay, a review of OpenBSD source code shows that indeed it bails out when you try to free a NULL pointer. Although this is the most solid code of all the free UNICES, it's probably safe to assume that other libc implementations follow the same procedure. So I'm off to find me a recipe for crow.
The point remains, however, that it's a good idea to know what your memory refers to before going off to free it.