473,794 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

This code must have crashed....but works fine

Consider the below code snippet:

#include <iostream>
using namespace::std;

class myclass
{
public:
myclass() {
cout << "constructo r" << endl;
}
~myclass() {
cout << "destructor " << endl;
}
void func() {
cout << "func" << endl;
}
};

int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............m ust be a crash..... but works fine
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????

regards,
Harsha.

Aug 18 '06 #1
14 1592
hs******@gmail. com schrieb:
Consider the below code snippet:
[snip]
int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............m ust be a crash..... but works fine
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
Where did you get that compiler? MS C++ is at version 8.0.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/
Your code invokes undefined behaviour by dereferencing a null-pointer.
Undefined means, the compiler is free to do everything it wants: It might
appear to work this time, or it might format your hard disk.

--
Thomas
Aug 18 '06 #2

hshar...@gmail. com wrote:
Consider the below code snippet:

#include <iostream>
using namespace::std;

class myclass
{
public:
myclass() {
cout << "constructo r" << endl;
}
~myclass() {
cout << "destructor " << endl;
}
void func() {
cout << "func" << endl;
}
};

int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............m ust be a crash..... but works fine
Actually, that merely results in undefined behavior.
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????
When you have undefined behavior, that means anything could happen.
For example (1) the program might crash, (2) the program might work as
if you had a valid object (since your class has no members and
therefore the null pointer is never dereferenced), or (3) the program
might print "Harsha uses far too many question marks ('?') in his
postings" on your console over and over again. If it's undefined, you
just don't know.

Best regards,

Tom

Aug 18 '06 #3
hs******@gmail. com wrote:
Consider the below code snippet:

#include <iostream>
using namespace::std;

class myclass
{
public:
myclass() {
cout << "constructo r" << endl;
}
~myclass() {
cout << "destructor " << endl;
}
void func() {
cout << "func" << endl;
}
};

int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............m ust be a crash..... but works fine
The C++ standard doesn't require any code to 'crash'.
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/
What makes you think there is no other possible outcome than a 'crash'?
anybody has an idea about it ??????????
Try to repair your '?' key. It seems broken.
Aug 18 '06 #4
<hs******@gmail .comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????
Unfortunately, C++ doesn't have strict rules about what happens when you do
something naughty like this. When you call obj->func(); with obj==NULL, the
behaviour is undefined. That means that it may, fortuitously, work exactly
as expected; but this is no guarantee that it works on any other compiler.

You may get different results if you add a variable to myclass and access it
from func(); you may not. Either way, you are correct, this code is wrong,
but the compiler may not be able to warn you about it.

Philip

Aug 18 '06 #5

Philip Potter wrote:
<hs******@gmail .comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????

Unfortunately, C++ doesn't have strict rules about what happens when you do
something naughty like this. When you call obj->func(); with obj==NULL, the
behaviour is undefined. That means that it may, fortuitously, work exactly
as expected; but this is no guarantee that it works on any other compiler.

You may get different results if you add a variable to myclass and access it
from func(); you may not. Either way, you are correct, this code is wrong,
but the compiler may not be able to warn you about it.

Philip
i tried it in microsoft cl 6.0, 7.0 and g++ (suprisingly same
behaviour). all the three give the same result. if its undefined, then
there must be some variations in the output, but its the same. i am
really confused.
i even tried adding a variable to the class, but the result was the
same. u guys can try it on any compiler and let me know if possible. i
doubt something in this.

Aug 18 '06 #6

<hs******@gmail .comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
>
Philip Potter wrote:
><hs******@gmai l.comwrote in message
news:11******* *************** @i42g2000cwa.go oglegroups.com. ..
i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????

Unfortunatel y, C++ doesn't have strict rules about what happens when you
do
something naughty like this. When you call obj->func(); with obj==NULL,
the
behaviour is undefined. That means that it may, fortuitously, work
exactly
as expected; but this is no guarantee that it works on any other
compiler.

You may get different results if you add a variable to myclass and access
it
from func(); you may not. Either way, you are correct, this code is
wrong,
but the compiler may not be able to warn you about it.

Philip

i tried it in microsoft cl 6.0, 7.0 and g++ (suprisingly same
behaviour). all the three give the same result. if its undefined, then
there must be some variations in the output, but its the same. i am
really confused.
i even tried adding a variable to the class, but the result was the
same. u guys can try it on any compiler and let me know if possible. i
doubt something in this.
The term "undefined" means that the Standard for C++ does not define what
the results should be. It is simply up to to you to avoid undefined
behavior at all times. It makes no difference if it happens to do the exact
same thing on every compiler and every computer in existence. It's improper
to do that, and the results are not specified by the Standard. You should
not rely on it working, or crashing, or on any specific behavior at all.
Just don't do that! :-)

-Howard

Aug 18 '06 #7
hs******@gmail. com wrote:
><hs******@gmai l.comwrote in message
news:11******* *************** @i42g2000cwa.go oglegroups.com. ..
i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????

Unfortunatel y, C++ doesn't have strict rules about what happens when you
do something naughty like this. When you call obj->func(); with
obj==NULL, the behaviour is undefined. That means that it may,
fortuitously , work exactly as expected; but this is no guarantee that it
works on any other compiler.

You may get different results if you add a variable to myclass and access
it from func(); you may not. Either way, you are correct, this code is
wrong, but the compiler may not be able to warn you about it.

Philip

i tried it in microsoft cl 6.0, 7.0 and g++ (suprisingly same
behaviour). all the three give the same result. if its undefined, then
there must be some variations in the output,
No. "Undefined" means that nothing is required. Not even that every compiler
behaves differently.
but its the same. i am really confused.
Why? It's simple. If you invoke undefined behavior, don't expect any specifc
or unspecific behavior. Just don't expect anything.

Aug 18 '06 #8

"Thomas J. Gritzan" <Ph************ *@gmx.deskrev i meddelandet
news:ec******** **@newsreader2. netcologne.de.. .
hs******@gmail. com schrieb:
>Consider the below code snippet:
[snip]
>int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............m ust be a crash..... but works fine
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.

Where did you get that compiler? MS C++ is at version 8.0.
No, it's the Visual Studio that is at version 8.0 (or 2005), using the
interesting scheme 1, 2, oops, 4, etc.

The current C++ compiler is at version 14.0, counting its ancestry all
the way back to the very first (non-Visual) MS C compiler, version 3.0
(don't ask :-).
To be somewhat on topic, I must also add that undefined behaviour
doesn't at all mean "must crash", as other posters have pointed out.
The really nasty result is "seems to work".
Bo Persson


Aug 18 '06 #9

hs******@gmail. com wrote:
Consider the below code snippet:

#include <iostream>
using namespace::std;

class myclass
{
public:
myclass() {
cout << "constructo r" << endl;
}
~myclass() {
cout << "destructor " << endl;
}
void func() {
cout << "func" << endl;
}
};

int main(void) {
myclass *obj = new myclass();
....should be...
myclass* obj = new myclass;
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............m ust be a crash..... but works fine
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/
Its undefined behaviour. Meaning that the result can't be guarenteed.
Undefined behaviour is often not a crash. Yet if you understand and
apply the concepts - UB is an important subject because
just_because_it _works does not mean its valid.

The only reason you never saw the "crash" is because the func() member
function isn't doing any work. Don't forget that the 'this' parameter
is passed to all non-const member functions. The fact that func() fails
to generate an access diagnostic is only because the obj's internals
are not being accessed.
Consider:

class myclass
{
int m_n;
public:
myclass() : m_n( 0 )
{
std::cout << "constructo r" << std::endl;
}
~myclass()
{
std::cout << "destructor " << std::endl;
}
void func()
{
std::cout << "func where m_n = " << m_n << std::endl;
}
};

The above class now does generate a crash when the zapped obj's access
is attempted...
The fact that the original did not crash is irrelevent, its still UB.
Undefined behaviour is about concepts, not results.

Aug 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2570
by: Subhash | last post by:
Hello, I am trying to generate dynamic graphics using the GD library and TTF fonts. The resulting image shows up fine. But in the apache error_log file the following lines are appended: child pid 29141 exit signal Segmentation fault (11) MMCACHE: PHP crashed on opline 14 of imagettfbbox() at test.php:13 Seems like it might be a GD library problem but I am not sure. Any
0
1036
by: Russell | last post by:
Hello everybody, I'm the administrator of our network and we have a problem with our Windows server 2003 standard. Recently I've installed the server on our network and promoted it to domain controller. Everything works fine. Our R&D department is developing a program that's created with c-sharp and runs in .NET (I hope my explanation is correct, because I'm not familiair with programming!)
6
1195
by: mandir_wahin_banayenge | last post by:
Any suggestions welcome. Sorry if google groups screws up the formatting. TIA, MWB #include <iostream> #include <list> using namespace std;
0
947
by: Ludovic Launer | last post by:
Just received a new computer. VS2003 works fine with my application, but when trying to create a new asp.net web application ti jsut crahsed without any error message ! I tried to: uninstall framework 1.1 / reinstall uninstall vs2003 / reinstall aspnet_regiis.exe -i No success It says "creating web application" and then you can see the animation with
8
3882
by: The Natural Philosopher | last post by:
This is so weird. What I am trying to do is to upload files and stuff them in a mysql database. Everything works except the file content is zero. using the load_file command from mysql command line as 'root' works and i can download the inserted file ok.
3
3747
by: Adhal | last post by:
Hi, I am using remoting to pass in parameters from new launches of an application. So basically I have only one instance of the application and I am using remoting to pass the parameters. The problem is as follows: I can remote call the first few times, but then I get the "No connection could be made because target machine actively refused it 127.0.0.1:8084"
0
2010
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ((string)(this.GetPropertyValue("Address1")));
8
1518
by: Pallav singh | last post by:
#include<iostream.h> #include<memory.h> #include<string.h> // product class Pizza { private : std::string Topping;
1
1977
by: raghulvarma | last post by:
Hai friends, I need to know what would happen when my IIS gets crashed? what would happen to all the projects present inside it?How should I rectify that?What should I do when I am asked to rectify the IIS which is crashed and the web page only specifies "Server Crashed" and no other information is provided? Could you plz specify a good link where I could get more information on IIS? Thanks Raghul
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10433
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...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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
9035
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...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
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();...
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
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.