By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

How can this not be?

P: n/a
I had the most bizarre C++ experience yet. I was traversing a tree using a
visitor pattern which I had mistakenly given bad information about where to
go in the tree. I'm pretty sure it was climbing out of the bottom. That
doesn't surprize me. The fact that it caused the program to crash is to be
expected. But when I looked at the stack in the debugger, it was saying
that this=0x0, and then showing me the value of a member of the object that
had a null pointer for /this/.

What the way the program works is that I pass a visitor object to a member
function on the root node of the tree. Then nodes pass the visitor to one
of their child nodes until it reaches a leaf. When it gets to a leaf, the
traversal function behaves differently, in that it calls a method on the
visitor like this visitorObject->visit(this). I could trace it past the
call to visit(this). That tells me that the functions were invoked for a
non-existant object. Does anybody else find that strange?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:tq********************@speakeasy.net...
I had the most bizarre C++ experience yet. I was traversing a tree using a visitor pattern which I had mistakenly given bad information about where to go in the tree. I'm pretty sure it was climbing out of the bottom. That
doesn't surprize me. The fact that it caused the program to crash is to be expected. But when I looked at the stack in the debugger, it was saying
that this=0x0, and then showing me the value of a member of the object that had a null pointer for /this/.

What the way the program works is that I pass a visitor object to a member
function on the root node of the tree. Then nodes pass the visitor to one
of their child nodes until it reaches a leaf. When it gets to a leaf, the
traversal function behaves differently, in that it calls a method on the
visitor like this visitorObject->visit(this). I could trace it past the
call to visit(this). That tells me that the functions were invoked for a
non-existant object. Does anybody else find that strange?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org


Not really, code is not stored in the object, just the data.
Jul 22 '05 #2

P: n/a

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:tq********************@speakeasy.net...
I had the most bizarre C++ experience yet. I was traversing a tree using a visitor pattern which I had mistakenly given bad information about where to go in the tree. I'm pretty sure it was climbing out of the bottom. That
doesn't surprize me. The fact that it caused the program to crash is to be expected. But when I looked at the stack in the debugger, it was saying
that this=0x0, and then showing me the value of a member of the object that had a null pointer for /this/.


When C++ is implemented, this is generally a parameter to a member function
much like any other. You just managed to pass 0 as that parameter.

john
Jul 22 '05 #3

P: n/a
John Harrison wrote:
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:tq********************@speakeasy.net...
I had the most bizarre C++ experience yet. I was traversing a tree using


a
visitor pattern which I had mistakenly given bad information about where


to
go in the tree. I'm pretty sure it was climbing out of the bottom. That
doesn't surprize me. The fact that it caused the program to crash is to


be
expected. But when I looked at the stack in the debugger, it was saying
that this=0x0, and then showing me the value of a member of the object


that
had a null pointer for /this/.

When C++ is implemented, this is generally a parameter to a member function
much like any other. You just managed to pass 0 as that parameter.


It used to be allowed, even required in some cases, to check 'this' for
being NULL because it sometimes was possible to call a member function for
a non-existent object through a null pointer. Not anymore, fortunately.

Victor
Jul 22 '05 #4

P: n/a
Steven T. Hatton wrote:
But when I looked at the stack in the debugger, it was
saying that this=0x0, and then showing me the value of a member of the
object that had a null pointer for /this/.
A reckless debugger.

[...] That tells me that the functions were invoked
for a
non-existant object. Does anybody else find that strange?


Are you referring to the fact that

struct A
{
void foo() { cout << this << endl; }
};

int main()
{
A*a=0;
a->foo();
}

prints 0 with most compilers? Well dereferencing 0 is undefined, anyway
it seems that the actual condition isn't easy to recognize without
runtime overhead, an implementation that doesn't need to dereference
anything (read:access memory) might work... And usually:

struct A
{
virtual void foo() { cout << this << endl; }
};

will crashboom, 'cause most Compilers store the vtbl in the first bytes
of the object, coming to access adress 0 here[1].

Marco

This isn't relevant anymore, but MFC had a couple of *virtual* methods
that had a "if (this==NULL) {...}" statement inside..Don't know if that
was a kind of voodoo or direct qualified call paranoia.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.