Runtime error expected but not encountered

T

Tahir Hashmi

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?
 
O

.oO LGV Oo.

Tahir Hashmi said:
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?

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...
 
T

tom_usenet

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
 
?

=?ISO-8859-1?Q?Rapha=EBl_Poss?=

..oO LGV Oo. said:
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.
 
O

.oO LGV Oo.

Raphaël Poss said:
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 :)
 
C

Chris Theis

.oO LGV Oo. said:
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
 
A

Andre Kostur

@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]
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".
 
T

Tahir Hashmi

#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!
 
J

John Carson

Tahir Hashmi said:
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top