J
John Doe
Do I need to check for NULL before delete [] p?
Do I need to check for NULL before delete [] p?
Is it written somewhere in some kind of official document(C++ standard ) ?dmparadiselm said:Do I need to check for NULL before delete [] p?
No need. every time after delete[] p;
p=NULL;
dmparadiselm said:Do I need to check for NULL before delete [] p?No need. every time after delete[] p;
p=NULL;
Is it written somewhere in some kind of official document(C++ standard ) ?
dmparadiselm said:It is guaranteed that a NULL pointer deletion is harmless in C++
standard.
But setting every deleted pointer to 0 doesn't solve real problems.
void f(void *ptr)
{
delete ptr;
ptr = 0;
}
int main()
{
int *ip = new int;
f(ip);
delete ip; // double delete, despite setting ptr to 0
return 0;
}
[snip]Leandro said:But that's because what is passed to f() is a copy of the pointer. If
the pointer was passed by reference, the code would be ok.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.