[Moved] A question about pointers.
-
What is the proper way to initialize a pointer? Can it be initialized to NULL?
-
The the way to test for NULL is simply:
If (pointer == null)
or
if (pointer == 0)
Yes?
-
^ This may not work.
http://www.codeguru.com/forum/archive/index.php/t-358371.htmlSimply go
@
if (ptrName)
cout << “Pointer points to something”;
else
cout << “Pointer is NULL”;
@Yet, make sure, the pointer is initialized, as described in more detail in the link. ;)
-
You can of course also initialize member pointers in the initializer list of the constructor.
-
Initialization in the initializer list is a good idea. Did you know you can even new objects there? Things like this are legal:
@
MyQObject::MyQObject(parent):
m_timer(new QTimer(this))
{
...
}
@For pointers that you wish to initialize elsewhere: NULL is more of a C construct. For C++, using 0 is more common.
-
If you don't ant to check your pointers for 0, you can also use the so called NullObject pattern.
This means you initialise a pointer to a static object with the same interface that just does nothing. The you can avoid this (if(0 != p) ...
-
[quote author="Taamalus" date="1300504867"]^ This may not work.
http://www.codeguru.com/forum/archive/index.php/t-358371.html
[/quote]What's the rationale behind this? To my knowledge even in poor old C (without ++) "0" is the all-valid constant for a null pointer (regardless of the internal representation for the actual machine). So
@
if(ptr == 0)
doFancyThings();
@is always valid code (See "comp.lang.c FAQ list, Question 5.2":http://c-faq.com/null/null2.html and "Question 5.5":http://c-faq.com/null/machnon0.html).
Though the short comparison you suggested, is always valid too ("C FAQ, 5.3":http://c-faq.com/null/ptrtest.html) and also more readable, so to prefer over the "noisy" one, IMHO.
-
Somewhat unrelated, but the assumption that accessing the 0 pointer will always results in a segmentation fault is not necessarily true. See https://lwn.net/Articles/342330/ for a very interesting description of what can happen when 0 suddenly becomes a valid pointer.
-
[quote author="Gerolf" date="1300560358"]I also saw something like this:
@
int CClass::foo()
{
if(0 == this)
return 0
do stuff
}
@with this syntax, this is absolutly safe:
@
CClass* p = 0;
p->foo();
@This was some method in I think MFC API[/quote]
Completely weird... I would not suppose this to work on other compilers than Microsoft's :-)
-
[quote author="Volker" date="1300533129"][quote author="Taamalus" date="1300504867"]^ This may not work.
http://www.codeguru.com/forum/archive/index.php/t-358371.html
[/quote]What's the rationale behind this? To my knowledge even in poor old C (without ++) "0" is the all-valid constant for a null pointer (regardless of the internal representation for the actual machine). So
@
if(ptr == 0)
doFancyThings();
@is always valid code (See "comp.lang.c FAQ list, Question 5.2":http://c-faq.com/null/null2.html and "Question 5.5":http://c-faq.com/null/machnon0.html).
[/quote]
Thanks, I stand corrected. :) Still, there is no way I will use it, but from now on, just as a personal preference.
http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer
this link is just to save face :D on my preferencesAlso thanks Gerolf! Re: Null Object Patterns, not for C++ but for LISP! Cheers! :)