Dereferencing NULL Pointer, without a Seg Fault
blog.8thlight.com
blog.8thlight.com
I also found it rather odd that the author spent so much time defining what a pointer is and what dereferencing is, when the article won't be of any interest whatsoever to someone who doesn't already know these things.
And lastly, when I saw the headline about dereferencing NULL, I expected it to be a discussion regarding the circumstances in which you can actually map address 0 to valid memory (and therefore dereference NULL without crashing), and the consequences thereof.
To put it another way, such an optimizer would transform incorrect programs into correct programs.
If you write correct programs, the optimization is useless since it only transforms incorrect programs. If you write incorrect programs, the optimization is dangerous since it makes it harder for you to figure out that your program is wrong.
Yeah. In retrospect my point doesn't really make any sense at all. Not sure what I was thinking there.
If your compiler isn't cheeky you can use this stuff in funny ways with constructs like `if(this)` in your member functions. Technically such a check could be optimised out because `this==null` can only result from undefined behaviour, but in practice it probably won't be.
1: http://stackoverflow.com/questions/2474018/when-does-invokin...
class NullChecker {
public:
bool isNull() { return !!this; }
};
int main() {
NullChecker *nc = 0;
if(nc->isNull()) doSomething();
return 0;
}
Forgive my C++ syntax as it's been a while, but this seems like it would work to me.Technically I look at methods as functions with a hidden first parameter that accepts a pointer to the class object's data. If you don't try to dereference that pointer, then the function should work like static member.
I was wondering if the compiler will add some check code preventing me from calling any method on a NULL pointed obj. But obviously its permitting it.
Of course virtual methods are different. Then you are calling a pointer to a function, the address of which should be stored in your obj data structure.
Not really contributing to the conversation, but it jumped out at me.
int **a = NULL;
int *b = NULL;
int c = 0;
...
if (a ! = NULL)
b = *a;
if (b != NULL)
c = *b;
you can remove all the NULL checks if *NULL = 0)AIX was an example of such a platform (at least until version 5.3, i haven't used it since then).
void Cow::milk()
{
if (!this) throw std::runtime_error("NULL isn't a cow!");
// rest of the code
}V8 actually uses this to implement a pretty cool trick. V8 objects are tagged--tag bits distinguish valid pointers from integer immediate values. The integer class has no data, it stores its information in the 'this' pointer.
Of course, I don't think that works on modern systems anymore, with vm.mmap_min_addr and friends.
http://stackoverflow.com/questions/1640309/c-static-members-...
The actual "m" is found by looking at the "p" class if it's non-virtual, and at the "v-table" in p if it's "virtual".
If "m" is static, no class lookup, or object v-table lookup is done, and the call is p->m() ~= m()