Consider:
char *p=NULL;
free(p) // or
delete p;
What will happen if I use free
and delete
on p
?
Question one: nothing will happen.
From the current draft of ISO/IEC 14882 (or: C++):
20.8.15 C Library [c.malloc]
The contents [of
, that is: where
free
lives,] are the same as the Standard C library [(see intro.refs for that)] header, with the following changes: [nothing that effects this answer].
So, from ISO/IEC 9899:1999 (or: C):
7.20.3.2 The
free
functionIf [the]
ptr
[parameter] is a null pointer, no action occurs.
From the C++ standard again, for information about delete
this time:
3.7.4.2 Deallocation functions [basic.stc.dynamic.deallocation]
The value of the first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation function is one supplied in the standard library, the call has no effect.
See also: