if this == NULL 
Author Message
 if this == NULL

Hi

Is the below valid C++?

class Null {
 int v;
public:
 int testFunc() {
  if( this == NULL ) return 3;
  return v; }

Quote:
};

int main() {
Null* pNull = NULL;
int acc_viol = pNull->testFunc();

Quote:
}

Is checking "this" againts NULL a defined behavior
according to the C++ standard when "this" is NULL?

Thanks,
Danny

p.s: I know this doesn't have anything to do with STL, but
people here is more likely to know C++ standard than any
other place.



Tue, 12 Apr 2005 16:59:37 GMT  
 if this == NULL


Quote:
>Hi

>Is the below valid C++?

>class Null {
> int v;
>public:
> int testFunc() {
>  if( this == NULL ) return 3;
>  return v; }
>};

>int main() {
>Null* pNull = NULL;
>int acc_viol = pNull->testFunc();

Undefined behaviour - deference of null pointer.

Quote:
>}

>Is checking "this" againts NULL a defined behavior
>according to the C++ standard when "this" is NULL?

Checking against NULL is fine, if a bit pointless since this can never
legally be NULL. Calling a member function through a null pointer is
undefined behaviour, according to the standard.

Quote:
>p.s: I know this doesn't have anything to do with STL, but
>people here is more likely to know C++ standard than any
>other place.

comp.lang.c++, comp.lang.c++.moderated and possibly comp.std.c++ are
better alternatives...

Tom



Tue, 12 Apr 2005 19:33:36 GMT  
 if this == NULL

Quote:

> [...]
>  int testFunc() {
>   if( this == NULL ) return 3;
>   return v; }
> [...]
> Is checking "this" againts NULL a defined behavior
> according to the C++ standard when "this" is NULL?

  Invoking a member function for a non-existing
  object invokes undefined behaviour. (That
  doesn't mean your code won't work in most
  cases. However, if on some machine, using some
  compiler, the program formats your hard disk,
  according to the standard that would be fine.)

Quote:
> Thanks,
> Danny
> [...]

  Schobi

--

I'm hschober at gmx dot de



Tue, 12 Apr 2005 19:41:17 GMT  
 if this == NULL

Quote:

> Hi

> Is the below valid C++?

> class Null {
>  int v;
> public:
>  int testFunc() {
>   if( this == NULL ) return 3;
>   return v; }
> };

> int main() {
> Null* pNull = NULL;
> int acc_viol = pNull->testFunc();
> }

> Is checking "this" againts NULL a defined behavior
> according to the C++ standard when "this" is NULL?

According to the C++ standard, checking "this" against NULL
is pointless, because you need to invoke undefined behaviour _before_
you get into the function where check resides.

E.g. in the code above, in function main() you invoke undefined behaviour
by
    int acc_viol = pNull->testFunc();

Everything might happen _before_ the body of testFunc() gets executed.
So if the check in testFunc() against NULL worked, it does mean
the only thing that nothing horrible happened (except, may be, a number of
formatted HDDs and launched missiles).

However, such questions are better asked on comp.lang.c++,
because people there can give you a _real_ examples and corrections.

HTH,
Ruslan Abdikeev



Tue, 12 Apr 2005 22:25:47 GMT  
 if this == NULL

Quote:
>>Is checking "this" againts NULL a defined behavior
>>according to the C++ standard when "this" is NULL?

>Checking against NULL is fine, if a bit pointless since
this can never
>legally be NULL. Calling a member function through a null
pointer is
>undefined behaviour, according to the standard.

I thought member functions are just functions where you
have the object's pointer passed as an implicit "this"
parameter, so if the "this" is never dereferenced inside
the function, what could happen? You don't need to
dereference a pointer to call member functions right? ...
wrong?


Thu, 14 Apr 2005 00:45:26 GMT  
 if this == NULL

Quote:

>>>Is checking "this" againts NULL a defined behavior
>>>according to the C++ standard when "this" is NULL?

>>Checking against NULL is fine, if a bit pointless since
>this can never
>>legally be NULL. Calling a member function through a null
>pointer is
>>undefined behaviour, according to the standard.

>I thought member functions are just functions where you
>have the object's pointer passed as an implicit "this"
>parameter, so if the "this" is never dereferenced inside
>the function, what could happen? You don't need to
>dereference a pointer to call member functions right? ...
>wrong?

As long as the function isn't virtual, it will typically "work", but
it's still undefined. MFC functions like GetSafeHwnd rely on it
working:

_AFXWIN_INLINE HWND CWnd::GetSafeHwnd() const
        { return this == NULL ? NULL : m_hWnd; }

I wouldn't write code like this, because it's undefined and not worth
checking for, IMO. Concerning virtual functions, they are typically
looked up by dereferencing the pointer to get at the class's vtbl, so
you will likely crash if you call a virtual function through a null
pointer.

--
Doug Harrison
Microsoft MVP - Visual C++



Thu, 14 Apr 2005 05:01:43 GMT  
 if this == NULL

Quote:
> I thought member functions are just functions where you
> have the object's pointer passed as an implicit "this"
> parameter, so if the "this" is never dereferenced inside
> the function, what could happen? You don't need to
> dereference a pointer to call member functions right? ...
> wrong?

Also if you try to access a data member, an offset will be applied to this,
and then the resulting pointer will be dereferenced, which results in grief
if this == NULL.

However, you can safely assume that always this != NULL, and in case that
even this would be NULL, there's nothing wrong with your code.

--

Ehsan Akhgari



[WWW: http://www.beginthread.com/Ehsan ]



Thu, 14 Apr 2005 12:21:38 GMT  
 if this == NULL


Quote:
> >>Is checking "this" againts NULL a defined behavior
> >>according to the C++ standard when "this" is NULL?

> >Checking against NULL is fine, if a bit pointless since
> this can never
> >legally be NULL. Calling a member function through a null
> pointer is
> >undefined behaviour, according to the standard.

> I thought member functions are just functions where you
> have the object's pointer passed as an implicit "this"
> parameter, so if the "this" is never dereferenced inside
> the function, what could happen? You don't need to
> dereference a pointer to call member functions right? ...
> wrong?

If in your member function you access member data, there is an implicit
dereference of this.

Stephen Howe



Fri, 15 Apr 2005 20:39:42 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. NULL v null pointer constant

2. Global pointer is null sometimes even when initialized to some non-null value

3. NULL as a null function pointer

4. confused null pointer with null pointer constant

5. memcpy( NULL, NULL 0 ) - safe or not?

6. NULL isn't 0/0 isn't NULL

7. MFC ODBC NULL/NOT NULL

8. (foo == (foo_t *)NULL) vs. ((void *)foo == NULL)

9. Null Pointer Assignment and (null)

10. Null pointer cast to far is not null

11. CreateNewFrame(NULL,NULL)

12. Null DateTime and Null int

 

 
Powered by phpBB® Forum Software