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

Runtime error expected but not encountered

P: n/a
Consider this piece of code:

#include <iostream>

using namespace std;

class y
{
public:
void draw()
{
cout << "Drawing Y" << endl;
}
void Yguy()
{
cout << "Y Guy Called" << endl;
}
};

int main()
{
int x1;
y* yPtr = (y*) &x1;
yPtr->draw();
yPtr->Yguy();
}
I was expecting a runtime error in the calls to draw() and Yguy() but
it doesn't happen! I tried it with VC++ .Net and GCC 3.2.2 and both
gave the output:

Drawing Y
Y Guy Called

Any explanations?

--
Tahir Hashmi (VSE, NCST)
tahir AT ncst.ernet.in
http://www.codemartial.org
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 18 Nov 2003 05:54:05 -0800, ta*******@flashmail.com (Tahir Hashmi)
wrote:
Consider this piece of code:

#include <iostream>

using namespace std;

class y
{
public:
void draw()
{
cout << "Drawing Y" << endl;
}
void Yguy()
{
cout << "Y Guy Called" << endl;
}
};

int main()
{
int x1;
y* yPtr = (y*) &x1;
yPtr->draw();
Undefined behaviour.
yPtr->Yguy();
}
I was expecting a runtime error in the calls to draw() and Yguy() but
it doesn't happen!
What is a runtime error in this context? A crash? Why were you
expecting one?
I tried it with VC++ .Net and GCC 3.2.2 and both
gave the output:

Drawing Y
Y Guy Called

Any explanations?


Undefined behaviour is just that - anything can happen. In this case,
the bug was unfortunately masked by the fact that the program did the
same as if yPtr had been a valid y value. This was probably because
the member functions called didn't attempt to access the "this"
pointer, but the code is illegal nevertheless, and it might crash on
some other platforms.

Tom
Jul 22 '05 #2

P: n/a

"Tahir Hashmi" <ta*******@flashmail.com> a écrit dans le message de
news:ad*************************@posting.google.co m...
Consider this piece of code:

#include <iostream>

using namespace std;

class y
{
public:
void draw()
{
cout << "Drawing Y" << endl;
}
void Yguy()
{
cout << "Y Guy Called" << endl;
}
};

int main()
{
int x1;
y* yPtr = (y*) &x1;
yPtr->draw();
yPtr->Yguy();
}
I was expecting a runtime error in the calls to draw() and Yguy() but
it doesn't happen! I tried it with VC++ .Net and GCC 3.2.2 and both
gave the output:

Drawing Y
Y Guy Called

Any explanations?

--
Tahir Hashmi (VSE, NCST)
tahir AT ncst.ernet.in
http://www.codemartial.org


I might be, and certainly am, wrong, but to me, since your class does not
contain any data, there's an equivalence between an y instance and any other
primitive type, like the int you use for your test...
Jul 22 '05 #3

P: n/a
..oO LGV Oo. wrote:
"Tahir Hashmi" <ta*******@flashmail.com> a écrit dans le message de
news:ad*************************@posting.google.co m...
Consider this piece of code:

#include <iostream>

using namespace std;

class y
{
public:
void draw()
{
cout << "Drawing Y" << endl;
}
void Yguy()
{
cout << "Y Guy Called" << endl;
}
};

int main()
{
int x1;
y* yPtr = (y*) &x1;
yPtr->draw();
yPtr->Yguy();
}
I was expecting a runtime error in the calls to draw() and Yguy() but
it doesn't happen! I tried it with VC++ .Net and GCC 3.2.2 and both
gave the output:

Drawing Y
Y Guy Called

Any explanations?

--
Tahir Hashmi (VSE, NCST)
tahir AT ncst.ernet.in
http://www.codemartial.org

I might be, and certainly am, wrong, but to me, since your class does not
contain any data, there's an equivalence between an y instance and any other
primitive type, like the int you use for your test...


No there is no such equivalence.

However, it happens that since none the two methods of class y use the
"this" pointer, there is nothing to trigger the runtime error that was
expected.

Jul 22 '05 #4

P: n/a

"Raphaël Poss" <fn***@raphael.poss.name> a écrit dans le message de
news:3f***********************@news.free.fr...
.oO LGV Oo. wrote:
"Tahir Hashmi" <ta*******@flashmail.com> a écrit dans le message de
news:ad*************************@posting.google.co m...
Consider this piece of code:

#include <iostream>

using namespace std;

class y
{
public:
void draw()
{
cout << "Drawing Y" << endl;
}
void Yguy()
{
cout << "Y Guy Called" << endl;
}
};

int main()
{
int x1;
y* yPtr = (y*) &x1;
yPtr->draw();
yPtr->Yguy();
}
I was expecting a runtime error in the calls to draw() and Yguy() but
it doesn't happen! I tried it with VC++ .Net and GCC 3.2.2 and both
gave the output:

Drawing Y
Y Guy Called

Any explanations?

--
Tahir Hashmi (VSE, NCST)
tahir AT ncst.ernet.in
http://www.codemartial.org

I might be, and certainly am, wrong, but to me, since your class does not contain any data, there's an equivalence between an y instance and any other primitive type, like the int you use for your test...


No there is no such equivalence.

However, it happens that since none the two methods of class y use the
"this" pointer, there is nothing to trigger the runtime error that was
expected.


so... you mean that since the methods don't need any specific data of the
class, they can be called without trouble... hmmm, interesting :)
Jul 22 '05 #5

P: n/a

".oO LGV Oo." <_N*****************@tiscali.fr> wrote in message
news:bp**********@news.tiscali.fr...

"Raphaël Poss" <fn***@raphael.poss.name> a écrit dans le message de
news:3f***********************@news.free.fr...
.oO LGV Oo. wrote:
"Tahir Hashmi" <ta*******@flashmail.com> a écrit dans le message de
news:ad*************************@posting.google.co m...

>Consider this piece of code:
>
>#include <iostream>
>
>using namespace std;
>
>class y
>{
>public:
> void draw()
> {
> cout << "Drawing Y" << endl;
> }
> void Yguy()
> {
> cout << "Y Guy Called" << endl;
> }
>};
>
>int main()
>{
> int x1;
> y* yPtr = (y*) &x1;
> yPtr->draw();
> yPtr->Yguy();
>}
>
>
>I was expecting a runtime error in the calls to draw() and Yguy() but
>it doesn't happen! I tried it with VC++ .Net and GCC 3.2.2 and both
>gave the output:
>
>Drawing Y
>Y Guy Called
>
>Any explanations?
>
>--
>Tahir Hashmi (VSE, NCST)
>tahir AT ncst.ernet.in
>http://www.codemartial.org
I might be, and certainly am, wrong, but to me, since your class does not contain any data, there's an equivalence between an y instance and any other primitive type, like the int you use for your test...


No there is no such equivalence.

However, it happens that since none the two methods of class y use the
"this" pointer, there is nothing to trigger the runtime error that was
expected.


so... you mean that since the methods don't need any specific data of the
class, they can be called without trouble... hmmm, interesting :)


Well, "without trouble" is certainly phrased too strongly! In this case
undefined behavior occurs which means that anything can happen. Due to the
fact that no access to the this pointer has been performed the OP was just
lucky, as this is invalid code from the standard's point of view!

Cheers
Chris
Jul 22 '05 #6

P: n/a
".oO LGV Oo." <_N*****************@tiscali.fr> wrote in news:bpda8l$ef4$1
@news.tiscali.fr:

[snip of example code showing a pointer to int being reinterpreted to a
pointer to object, then methods called through that pointer]
> I might be, and certainly am, wrong, but to me, since your class does not > contain any data, there's an equivalence between an y instance and any other > primitive type, like the int you use for your test...


No there is no such equivalence.

However, it happens that since none the two methods of class y use the
"this" pointer, there is nothing to trigger the runtime error that was
expected.


so... you mean that since the methods don't need any specific data of the
class, they can be called without trouble... hmmm, interesting :)


Only for certain definitions of "can". It is undefined behaviour. The
program could do _anything_ at that point. What is likely happening is
that your particular implementation of C++ won't trigger a crash since the
code doesn't require dereferencing of the this pointer. However, you
cannot _rely_ on this behaviour. It is an accident that it "works".
Jul 22 '05 #7

P: n/a
#include <iostream>
#include <fstream>
using namespace std;

class y
{
public:
void draw()
{
cout << "Drawing Y" << endl;
}
void Yguy()
{
cout << "Y Guy Called" << endl;
this->i = 0;
}
private: int i;
};

int main()
{
int x1;
y* yPtr = (y*) &x1;
yPtr->draw();
yPtr->Yguy();
}

Note the addition of a private data member to class y and
dereferencing of this in Yguy(). It still doesn't crash or anything
with GCC 3.2.2. What I'm really unable to get is, where does the
compiler/linker find the definition of Yguy() and the private data
member in an int?

My guess is that since sizeof(y) = sizeof(int) on my platform, the
compiler tries to modify the bits held by the int and that doesn't
lead to invalid memory access. Code for function definitions is anyway
generated and stored separately so the access to the function... well,
works!

--
Tahir Hashmi (VSE, NCST)
tahir AT ncst.ernet.in
http://www.codemartial.org
Jul 22 '05 #8

P: n/a
"Tahir Hashmi" <ta*******@flashmail.com> wrote in message
news:ad**************************@posting.google.c om
#include <iostream>
#include <fstream>
using namespace std;

class y
{
public:
void draw()
{
cout << "Drawing Y" << endl;
}
void Yguy()
{
cout << "Y Guy Called" << endl;
this->i = 0;
}
private: int i;
};

int main()
{
int x1;
y* yPtr = (y*) &x1;
yPtr->draw();
yPtr->Yguy();
}

Note the addition of a private data member to class y and
dereferencing of this in Yguy(). It still doesn't crash or anything
with GCC 3.2.2. What I'm really unable to get is, where does the
compiler/linker find the definition of Yguy() and the private data
member in an int?

My guess is that since sizeof(y) = sizeof(int) on my platform, the
compiler tries to modify the bits held by the int and that doesn't
lead to invalid memory access. Code for function definitions is anyway
generated and stored separately so the access to the function... well,
works!


I think your guess is correct. Using VC++, if you change x1 from an int to a
char you get a crash.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.