May 18, 2005

Achieved the ‘impossible’…

Posted in C++, Computers at 7:55 pm by pmatos

Lately I’ve been having very bad days and… oh well, besides I’m sick for 4 days now, my work is not going as well as I would like. I’ve been running against a semantic C++ error which I just solved 1 hour ago after about a week of searching. The error was terribly simple… I had a class hierarchy as follows:
A -> B -> C
and I had created 2 objects, and I have two objects:
A * a1 = new A();
C * a2 = new A();

I had a public getter in C:
virtual unsigned long getAttr() { return 0; }

and in B I had:
virtual unsigned long getAttr() const { return attr; }

For objects like a1 I received the value of attr and for objects like a2 I received 0. Well, after days of ill-searching (literally, since I’m sick) I found out the problem was with the definition of getAttr. Due to the missing declaration of const in C, polymorphism was not working. Adding const to the definition in C made it work. Fifteen minutes later I add a much nicer. The error resulted in “segmentation fault” without debugging options and worked ok with debugging option on. So I ran valgrind on the debugged version which after various errors and warning outputted told me (what motivated this post):

valgrind: the `impossible’ happened:
Killed by fatal signal

Isn’t this nice… :D


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: