Topic: Accessing methods with NULL pointers....
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 7 Oct 1994 18:39:30 GMT Raw View
ldh@cs.brown.edu (Laurent Hasson) writes:
>Basically, my question can be summed up to this more basic and
>language level question:
>
> - Is the call of a member function dependent on the this pointer
> (assuming that the function is not virtual)?
Yes - "the effect of calling a nonstatic member function of a class X
for something that is not an object of class X is undefined" (ARM 9.3).
> - If i call a function through a NULL pointer, what makes the
> program crash, the call itself (thus, calling a function IS
> dependent on the value of the "this" pointer), OR accessing a member
> data inside the function?
The call itself gives the program undefined behaviour.
Undefined behaviour can include crashing the program, or it
can include doing what you want.
> From experience, it seems that accessing data members IS the problem
>when a function is called through a NULL pointer, not the actual
>calling of the function.
This is of most (all?) existing implementations. Nevertheless, if
you want to write portable, standard-conforming programs, you should
not use that technique.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: ldh@cs.brown.edu (Laurent Hasson)
Date: Mon, 26 Sep 1994 22:00:07 GMT Raw View
Here is a sample code:
class A
{ public:
A(...) { ... }
~A(...) { ... }
f(...) { if (this == NULL) return;
// Your code here.....
}
}
main()
{ A* Toto = NULL;
Toto->f(...); // I don't want it to seg_fault here!
}
For virtual functions, as expected, this doesn't work, so i use this
other hack:
class A
{ public:
A(...) { ... }
~A(...) { ... }
f(...) { if (this == NULL) return;
return Virtual_f(...);
}
protected:
virtual Virtual_f(...) { //code here....
}
}
Yes, i could use smart/safe pointers and all, but this is a little bit
overkill for what i am looking for. All i want is:
1/ avoid testing for NULL each time i access an object,
2/ avoid a seg_fault if i use a NULL pointer,
3/ have a behavior on a per-function basis that makes the call
remain consistent for the calling code even if the pointer is
NULL.
This works fine on 4 compilers i have tried:
Borland C++,
MS Visual C++,
g++
SUN's CC.
Is this OK or not? I looked on the ARM, and could not find any
reference to this kind of behavior...
Laurent.
Author: ldh@cs.brown.edu (Laurent Hasson)
Date: Mon, 26 Sep 1994 23:47:49 GMT Raw View
Basically, my question can be summed up to this more basic and
language level question:
- Is the call of a member function dependent on the this pointer
(assuming that the function is not virtual)?
- If i call a function through a NULL pointer, what makes the
program crash, the call itself (thus, calling a function IS
dependent on the value of the "this" pointer), OR accessing a member
data inside the function?
From experience, it seems that accessing data members IS the problem
when a function is called through a NULL pointer, not the actual
calling of the function.
Laurent.