Re: Null Pointer Considerations
Frederick Gotham wrote:
Matthias Hofmann posted:
I cannot give you a definition of an "invalid object", but
the easiest way to get an "invalid pointer", without
invoking undefined behaviour, should be:
int* get_invalid_ptr()
{
int* p;
return p;
}
I'm pretty sure that DOES invoke undefined behaviour, just as
would the following:
int Func()
{
int i;
return i;
}
Returning by value results in a copy-construction, so you'll
be reading a garbage value which might trap.
Correct. There is one important difference with regards to
int's, however. Once an int becomes valid, it can only become
invalid through undefined behavior. Pointers can become invalid
if the underlying memory ceases to "exist" (in the sense of the
standard). Thus the following two functions also display
undefined behavior in their return statements:
int* f() { int i ; return &i ; }
int* f() { int* p = new int() ; delete p ; return p ; }
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]