473,414 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Runtime error expected but not encountered

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
8 2264
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

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

"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

".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
".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
#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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aaron W. LaFramboise | last post by:
I'm seeking a solution to my C++-life long dilemma of how to deal with errors when exceptions aren't appropriate. Below I highlight two error cases, which mainly occur when trying to handle...
10
by: MLH | last post by:
I am concerned. I have recently moved to A97 from Access 2.0 where I rolled out runtime apps with the ADT. Now, the Office Pro Developer's Edition with Access 97 is what I'm using. I find some...
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
1
by: Aleks A. | last post by:
Greetings all, I'm getting the following 2 errors, back to back, ever couple of days. Anyone encountered anything of the sort? After these 2 errors occur, the asp.net application stops...
10
by: steve | last post by:
Hi All I would like to be able to change the cell borders on certain cells to none at runtime to make a group of cells appear to be merged I have tried the following in the cellformatting...
3
by: =?Utf-8?B?R3JhaGFt?= | last post by:
I've added 2 tracking services to the wf runtime; one is the standard SqlTrackingService: trackingService = new SqlTrackingService(<trackingConnectionString>); <workflow...
10
RMWChaos
by: RMWChaos | last post by:
WinVista/IE7 I am getting some weird errors only in IE7, but not in FF2.0.0.8 or NN9. It even happens on this website when I click "Sign In". The error is: "A Runtime Error has occurred."...
6
by: samsneelam | last post by:
Hi.. This is samuel, while doing a program, i encountered this problem.. Let me give you clarity regarding my prob.. I am having two files .. one is mpcplib.h it contains the follwing...
1
by: BL3WC | last post by:
Hi, I'd created a MDE under Access 2003. It is now under testing stage. Some of the users will use Access 2003 runtime and some will use Access 2007 runtime to run this MDE. I installed the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.