How does this work without instantiating an object of the Student class?

0 views
Skip to first unread message

amit.bolaka...@gmail.com

unread,
Dec 14, 2007, 4:29:32 PM12/14/07
to
#include <iostream>
using namespace std;

class Person{
public:
virtual void print () { cout << "In parent" << endl; }
void onlyInPerson() { cout << "Only in Person/parent" <<
endl; }
};

class Student: public Person{
public:
Student() { cout << "Student constructor." << endl; }
void print() { cout << "In child" << endl; }
void onlyInChild() { cout << "Only in Child/Student" <<
endl; }
};

int main()
{
Person* p ;
Student* s;
s->onlyInChild();
return 0;
}

siddhu

unread,
Dec 14, 2007, 4:33:08 PM12/14/07
to
> }- Hide quoted text -
>
> - Show quoted text -

Although function is getting called, But its undefined behavior.

David Côme

unread,
Dec 14, 2007, 4:34:33 PM12/14/07
to
On Fri, 14 Dec 2007 22:29:32 +0100, <amit.bolaka...@gmail.com>
wrote:


I'm not sure but for me it's an undefined behaviour

amit.bolaka...@gmail.com

unread,
Dec 14, 2007, 4:45:50 PM12/14/07
to
On Dec 14, 4:34 pm, David Côme <davidc...@wanadoo.fr> wrote:
> On Fri, 14 Dec 2007 22:29:32 +0100, <amit.bolakani.techni...@gmail.com>

It seems to compile and run fine! I'm pretty surprised and it throws
me off a bit in terms of how it runs fine?

amit.bolaka...@gmail.com

unread,
Dec 14, 2007, 4:47:24 PM12/14/07
to

Did any of you guys try this with your compiler. I tried it with g++
3.4.4 on cygwin.

Default User

unread,
Dec 14, 2007, 4:53:51 PM12/14/07
to
amit.bolaka...@gmail.com wrote:


> int main()
> {
> Person* p ;
> Student* s;
> s->onlyInChild();
> return 0;
> }

It's Undefined Behavior to deference an invalid pointer. There is no
defined behavior for Undefined Behavior. It is not a syntax error. It
is not a constraint violation. There is no required diagnostic.


Brian


tragomaskhalos

unread,
Dec 14, 2007, 4:59:18 PM12/14/07
to
> me off a bit in terms of how it runs fine?- Hide quoted text -
>

That it happens to work is no surprise - onlyInChild does not access
any members, and the method is not virtual, hence the garbage this
pointer is
not dereferenced. Neverthless it's still undefined behaviour.

amit.bolaka...@gmail.com

unread,
Dec 14, 2007, 5:03:11 PM12/14/07
to
On Dec 14, 4:59 pm, tragomaskhalos <dave.du.verg...@logicacmg.com>
wrote:

Isin't the garbage this pointer dereferenced to to call the member
function onlyInChild() ?

Vaclav Haisman

unread,
Dec 14, 2007, 5:05:04 PM12/14/07
to
The programme compiles because it is well-formed and probably even runs
because the methods actually do not touch anything in the object itself but
your programme is invoking undefined behaviour.

--
VH

amit.bolaka...@gmail.com

unread,
Dec 14, 2007, 5:33:27 PM12/14/07
to
On Dec 14, 5:05 pm, Vaclav Haisman <v.hais...@sh.cvut.cz> wrote:

Ok, so I understand that its undefined behavior. However, is the
garbage this pointer dereferenced to call the member method?

Default User

unread,
Dec 14, 2007, 5:35:42 PM12/14/07
to
amit.bolaka...@gmail.com wrote:


> Isin't the garbage this pointer dereferenced to to call the member
> function onlyInChild() ?


Maybe, maybe not. Who cares?

There. Is. No. Defined. Behavior. For. Undefined. Behavior.

Say that over and over to yourself until you GET IT.

Brian

amit.bolaka...@gmail.com

unread,
Dec 14, 2007, 5:38:06 PM12/14/07
to
On Dec 14, 5:35 pm, "Default User" <defaultuse...@yahoo.com> wrote:

No worries Brian :) GOT IT!
Thanks for all your help guys. Appreciate it.

Erik Wikström

unread,
Dec 14, 2007, 7:30:11 PM12/14/07
to

Do you see 'this' (or any other members) mentioned anywhere in
onlyInChild()? If you do not, why would it be dereferenced?

--
Erik Wikström

tragomaskhalos

unread,
Dec 15, 2007, 4:36:30 AM12/15/07
to
> function onlyInChild() ?- Hide quoted text -
>

OK, my original reply wasn't detailed enough.
Others have said that this is UB but you are quite correctly
still wondering why it happend to work with your compiler.
The key is that the compiler is likely producing code something akin
to this:

//SOURCE:
// void Student::onlyInChild() { cout << "Only in Child/Student" <<
endl; }
//COMPILER:
void __Student_onlyInChild(__this) {
/* ... stuff that NEVER USES __this */
}

//SOURCE s->onlyInChild(); // nb s is uninitialised ...
//COMPILER:
__Student_onlyInChild(s); // nb s is NOT dereferenced

HTH

Markus Moll

unread,
Dec 15, 2007, 6:20:01 AM12/15/07
to
Hi

amit.bolaka...@gmail.com wrote:

> Ok, so I understand that its undefined behavior. However, is the
> garbage this pointer dereferenced to call the member method?

That's just one of the things that is undefined.

Markus

Pete Becker

unread,
Dec 15, 2007, 8:57:53 AM12/15/07
to
On 2007-12-15 04:36:30 -0500, tragomaskhalos
<dave.du...@logicacmg.com> said:

>
> OK, my original reply wasn't detailed enough.
> Others have said that this is UB but you are quite correctly
> still wondering why it happend to work with your compiler.
> The key is that the compiler is likely producing code something akin
> to this:
>

Maybe. Speculating about why something whose behavior is undefined
happens to do what a naive programmer guessed it would do is generally
pointless. Undefined means undefined. Nothing more. The reason that it
"works" is pure accident. (That's the screed for beginning and
intermediate programmers.)

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

tragomaskhalos

unread,
Dec 15, 2007, 6:14:15 PM12/15/07
to
On 15 Dec, 13:57, Pete Becker <p...@versatilecoding.com> wrote:
> On 2007-12-15 04:36:30 -0500, tragomaskhalos
> <dave.du.verg...@logicacmg.com> said:
>
> > OK, my original reply wasn't detailed enough.
> > Others have said that this is UB but you are quite correctly
> > still wondering why it happend to work with your compiler.
> > The key is that the compiler is likely producing code something akin
> > to this:
>
> Maybe. Speculating about why something whose behavior is undefined
> happens to do what a naive programmer guessed it would do is generally
> pointless. Undefined means undefined. Nothing more. The reason that it
> "works" is pure accident. (That's the screed for beginning and
> intermediate programmers.)
>

Well that's probably a good philosophy in the general case, but here
the OP accepted the undefinedness of the code but was clearly baffled
as to how it *could ever* have worked; seeing how a compiler might
realistically have treated the code so as to produce the observed
effect
is often valuable - when taken with the usual caveats. Otherwise one
is
left with a helpless feeling that it's all magic, which can only
dishearten the questioner and deter experimentation.

Pete Becker

unread,
Dec 16, 2007, 8:40:00 AM12/16/07
to
On 2007-12-15 18:14:15 -0500, tragomaskhalos
<dave.du...@logicacmg.com> said:

Once you're in the realm of undefined behavior, it is all magic, unless
you're skills are advanced enough to analyze machine instructions.
That's not a useful exercise for beginners.

Reply all
Reply to author
Forward
0 new messages