473,796 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2281
On 18 Nov 2003 05:54:05 -0800, ta*******@flash mail.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*******@flas hmail.com> a écrit dans le message de
news:ad******** *************** **@posting.goog le.com...
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*******@flas hmail.com> a écrit dans le message de
news:ad******** *************** **@posting.goog le.com...
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*******@flas hmail.com> a écrit dans le message de
news:ad******** *************** **@posting.goog le.com...
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.f r> 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*******@flas hmail.com> a écrit dans le message de
news:ad******** *************** **@posting.goog le.com...

>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.f r> wrote in news:bpda8l$ef4 $1
@news.tiscali.f r:

[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*******@flas hmail.com> wrote in message
news:ad******** *************** ***@posting.goo gle.com
#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
1741
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 "unexpected" errors on system interfaces. I am an experienced modern C++ programmer, and I'm mainly interested in the case where the error is in a generic, reusable component where invasive control over the user is unacceptable. Case 1: An error...
10
2218
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 unfriendly behavior. I want (need) to find out what the pro's (like you guys) are doing to circumvent some of the difficulties encountered when rolling out A97 runtime apps into environments that either have or may have in the future MS Access 97...
7
5029
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. I created the websetup and built the MSI, have the bundled version. Copied to webserver and ran Websetup.msi. Said I had to remove old version, which I did, then reran WebSetup.msi and keeps giving me this error. "The installer was interrupted...
1
6955
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 responding. I have to restart the service to get it up and running again. Any way to trace the problem to its root? Any help would be apreciated! Event Type: Error
10
45216
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 event..., to no avail Any ideas Regards
3
2519
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 Runtime>.AddService(trackingService); trackingService.IsTransactional = false; trackingService.UseDefaultProfile = true; This works just fine.
10
13325
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." "Line:xxx" "Error: Object expected" and Debug says: "Microsoft JScript runtime error: Object expected."
6
14729
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 declerations.... #include <queue> #include <vector> #include <string> class database { queue<delayTP> delayThrouput;
1
4088
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 Access 2007 runtime on a test machine and place the MDE in the Desktop for user's initial testing. I did not create a trust area for this MDE in the test machine as well as in my development machine. When I tested the MDE using /runtime switch in my...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.