Possible Duplicate:
Function Free() in C doesn't work
I just asked a question about free(), check the link below. If I can't use if(pointer != NULL), then what can I use, just in case it doesn't work because of a stupid mistake I made.
Possible Duplicate:
Function Free() in C doesn't work
I just asked a question about free(), check the link below. If I can't use if(pointer != NULL), then what can I use, just in case it doesn't work because of a stupid mistake I made.
Whenever you free a pointer you should set it to null - then you can use your test safetly
free(pointer);
pointer=NULL;
if(!pointer)
{
pointer=malloc(42);
}
You could even create your own free function so you dont forget to set to null.
void myfree(void **ptr)
{
free(*ptr);
*ptr=NULL;
}
Don't use naked pointers.
Use std::shared_ptr<OBJ>
and std::unique_ptr<OBJ>