Re: Setting malloc pointer to NULL?
- From: Victor Bazarov <v.Abazarov@xxxxxxxxxxxx>
- Date: Thu, 09 Oct 2008 18:21:52 -0400
Robby wrote:
ooh man, Im getting some weird stuff happening here!
Let me ask you guys something. After you free a pointer that was created with malloc (which set aside memory in the heap for a struct) should we set that pointer to NULL ?
If you're unsure of others who might again try to free it, yes, you should. If you need to free the memory ASAP and then you need to know whether it's been freed, then the NULL is a very good indicator (you can test for it in an 'if'). If you're concerned with calling 'free' more than once, then, in most cases, it's better to eliminate even the possibility of double freeing by writing *correct code*. Let the memory be allocated until you need to re-allocate, and only free right before setting it to a new value. Or free it right before the pointer variable goes out of scope, then the danger of double freeing will subside...
Theoretical general questions like yours are difficult to answer with any reasonable degree of certainty. Perhaps you want to provide the context or the circumstances that prompted your question...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
- Follow-Ups:
- Re: Setting malloc pointer to NULL?
- From: Robby
- Re: Setting malloc pointer to NULL?
- References:
- Setting malloc pointer to NULL?
- From: Robby
- Setting malloc pointer to NULL?
- Prev by Date: Re: Setting malloc pointer to NULL?
- Next by Date: Re: Calling VB.net from C++
- Previous by thread: Re: Setting malloc pointer to NULL?
- Next by thread: Re: Setting malloc pointer to NULL?
- Index(es):
Relevant Pages
|