473,699 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this strange behavior?

Following is my code:

include <iostream>

class Test{
public:
Test(){};
void print(){ std::cout << "OK" << std::endl ; };
};

int main( int argc , char ** argv ){
Test * test = new Test();
Test r = *test ;

delete test ;
r.print(); //this line , this line
}

I think the comment line will produce an error.
But it output OK normally, is this behavior right?
Nov 29 '06 #1
14 1284
Hi

Bo Yang wrote:
int main( int argc , char ** argv ){
Test * test = new Test();
Test r = *test ;

delete test ;
r.print(); //this line , this line
}

I think the comment line will produce an error.
But it output OK normally, is this behavior right?
Absolutely, you call print on a copy of test that is still existent.

Markus

Nov 29 '06 #2
Bo Yang <st******@mail. nankai.edu.cnwr ites:
int main( int argc , char ** argv ){
Test * test = new Test();
Test r = *test ;

delete test ;
r.print(); //this line , this line
}

I think the comment line will produce an error.
No.
But it output OK normally, is this behavior right?
Yes. r is a local variable (automatic), which is still in scope at
that line (actually in this block). It's not a copy of the pointer of
object test, but a copy of the value of test into its own memory.

Cheers,
Rudiger
Nov 29 '06 #3

"Bo Yang" <st******@mail. nankai.edu.cnwr ote in message
news:ek******** **@news.cn99.co m...
Following is my code:

include <iostream>

class Test{
public:
Test(){};
void print(){ std::cout << "OK" << std::endl ; };
};

int main( int argc , char ** argv ){
Test * test = new Test();
Test r = *test ;

delete test ;
r.print(); //this line , this line
}

I think the comment line will produce an error.
Why do you think so?

But it output OK normally, is this behavior right?
Yes, it is.

-Mike
Nov 29 '06 #4

Bo Yang wrote:
Following is my code:

include <iostream>

class Test{
public:
Test(){};
void print(){ std::cout << "OK" << std::endl ; };
};

int main( int argc , char ** argv ){
Test * test = new Test();
Test r = *test ;

delete test ;
r.print(); //this line , this line
}

I think the comment line will produce an error.
But it output OK normally, is this behavior right?
It IS correct .the compiler tries to make a copy constructor for every
class if you dont provide one.You did produce a default constructor but
u did not provide a copy constructor.

cheers
FM

Nov 29 '06 #5

Bo Yang wrote:
Following is my code:

include <iostream>

class Test{
public:
Test(){};
loose the extra semicolons - Test() { } is not a declaration only
void print(){ std::cout << "OK" << std::endl ; };
void print() const { std::cout << "OK" << std::endl ; }
};

int main( int argc , char ** argv ){
Test * test = new Test();
Test* test = new Test;
Test r = *test ;

delete test ;
r.print(); //this line , this line
}

I think the comment line will produce an error.
No it won't. The following statement:
Test r = *test ;
invokes a copy constructor.
The variable r is therefore allocated on the stack and survives until
the end of main's scope.
But it output OK normally, is this behavior right?
Yes, r is an independant variable.

Also, when you are testing for undefined behaviour, stick a dummy
variable in the class and test by attempting to access the dummy.

example:

#include <iostream>

class Test {
int n;
public:
Test() : n( 0 ) { }
int get() const {
return n;
}
void print() const {
std::cout << "OK\n";
}
};

int main() {
Test * p_test = 0; // pointer to a black hole
p_test->print(); // passes
// p_test->get(); // fails
}

Nov 29 '06 #6
On 2006-11-29 20:25, Salt_Peter wrote:
Also, when you are testing for undefined behaviour, stick a dummy
variable in the class and test by attempting to access the dummy.

example:

#include <iostream>

class Test {
int n;
public:
Test() : n( 0 ) { }
int get() const {
return n;
}
void print() const {
std::cout << "OK\n";
}
};

int main() {
Test * p_test = 0; // pointer to a black hole
p_test->print(); // passes
// p_test->get(); // fails
}
A question: I can understand why it works even if p_test == 0, but
should it work (is it legal)?
--
Erik Wikström
Nov 29 '06 #7

Salt_Peter skrev:
Bo Yang wrote:
[snip]
int main() {
Test * p_test = 0; // pointer to a black hole
p_test->print(); // passes
Well... it passes in some sense of the word, surely. But it is
undefined behaviour and the statement there is not required to do
anything sensible. It might "work", it might abort the program and it
might send a rude email to your boss.
/Peter
// p_test->get(); // fails
}
Nov 29 '06 #8
Salt_Peter wrote:
[snip]
Also, when you are testing for undefined behaviour, stick a dummy
variable in the class and test by attempting to access the dummy.

example:

#include <iostream>

class Test {
int n;
public:
Test() : n( 0 ) { }
int get() const {
return n;
}
void print() const {
std::cout << "OK\n";
}
};

int main() {
Test * p_test = 0; // pointer to a black hole
p_test->print(); // passes
What does the standard say should happen when you do this?
Personally, I would make a serious effort never to do -on a
null pointer, even if it's to a function with no access to data.
// p_test->get(); // fails
}
Socks

Nov 29 '06 #9

Erik Wikström wrote:
On 2006-11-29 20:25, Salt_Peter wrote:
Also, when you are testing for undefined behaviour, stick a dummy
variable in the class and test by attempting to access the dummy.

example:

#include <iostream>

class Test {
int n;
public:
Test() : n( 0 ) { }
int get() const {
return n;
}
void print() const {
std::cout << "OK\n";
}
};

int main() {
Test * p_test = 0; // pointer to a black hole
p_test->print(); // passes
// p_test->get(); // fails
}

A question: I can understand why it works even if p_test == 0, but
should it work (is it legal)?
Its *not* legal. Even if it appears to work. Thats my point.
The code you presented earlier does not invoke undefined behaviour. To
prove it, use get() as described in above.
A runtime error will not occur unless you attempt to access the invalid
object. So you can use something like get() to test for UB while
print() will erroneously fail to generate any error.

Nov 29 '06 #10

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

Similar topics

2
3456
by: Marcus | last post by:
Hello, I recently converted all my existing MyISAM tables to InnoDB tables through phpmyadmin. I noticed some strange behavior whenever I would refresh the screen, as phpmyadmin would report different numbers for the cardinality of the primary key (i.e. one minute it would say cardinality 388, then 244 on refresh, then something else), and it would report different values for the number of rows when mousing over the table names on the...
36
3445
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested print 2.
0
1707
by: thulsey | last post by:
Hi all, I've got some strange behavior happening in Firefox and Safari (Khtml and Gecko) that displays *almost* fine in IE6.0 (still trying to get pixels to line up, anal anal anal...) To speed things up, and to avoid any problems with looking through the css file, I'll say first that I based the navigation menu on the "Suckerfish Dropdown menu" online demo over at htmldog. (http://www.htmldog.com/articles/suckerfish/dropdowns/). In...
1
1506
by: Alexander Inochkin | last post by:
Hi! I found same strange behavior of ASP.NET. It is possible this is the bug. Follow the steps:
0
3571
by: ivb | last post by:
Hi all, I am using DB2 8.1.11.1 on NT with ASP.NET 1.1 When application make connection to database (via ADO.NET), it set "Connection timeout" parameter to 30 seconds. After, when my webpage requests database, and query execution time exceeds 30 seconds, the following error reported: ===
6
2267
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to the same method (aka delegate?). I use the Tag property within this method to determine what user action is taking place. Very simple: When adding toolbar button: tbButton.Click += new...
3
1514
by: sara | last post by:
Very strange behavior, but I suspect some is A2K and some might be for me to correct. Just trying to see if anyone can help and advise. We have a database that's been running for a few years with no problems. We continuously add queries and reports. We're up to about 700 queries (no, not all are used and most are parameter queries - the business asks a LOT of questions!), and under 250 reports and fewer than 15 forms. Data is...
1
2974
by: Nicholas Palmer | last post by:
Hi all, Got a question about the AspCompat=true page property. First a little background. We have an ASP.NET app that uses two COM components. The first is the Microsoft OWC 11 components and the second is a custom VB6 COM component. So I was reading about AspCompat=true and it seemed like it would be a good fit for our app. From what I can tell both of the COM components that we are using are STA and we are creating the components in...
19
1746
by: david | last post by:
I took old code and decided to modify it a bit, and I just noticed that it does not compile at all and before server one of severs (main) crashed in the system it was working fine (I am really sure that I remember that). Now I am getting this error: $ make g++ -Wall -ansi -pedantic -c pirma_lib.cpp g++ -Wall -ansi -pedantic -c pirma.cpp In file included from pirma.cpp:2:
20
2224
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
9172
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
9032
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...
1
8908
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
8880
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
7745
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
4374
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...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
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.