473,608 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem understanding automatic objects

/*****
When I run the following program (gnu C++) the address of the automatic
object 'a'
in f() is the same as the address of the automatic object 'b' in main().

I conclude that 'a' in f() is the same object as 'b' in main().

Is this an error or an optimization?

Thank you,
Joe Hesse
*****/

#include <iostream>
using namespace std;

class X {
private:
int x;
public:
X(int a = 0) : x(a) {};
};

// function returns an X object by value
X f() {
X a;
cout << "&a in f() is " << &a << endl;
return a;
}

int main() {
X b = f();
cout << "&b in main() is " << &b << endl;
return 0;
}
Jul 19 '05 #1
9 2003

"Joe Hesse" <jo*******@actc x.com> wrote in message news:3f******** *************** @newsreader.vis i.com...
When I run the following program (gnu C++) the address of the automatic
object 'a'
in f() is the same as the address of the automatic object 'b' in main().
Thats fine.

I conclude that 'a' in f() is the same object as 'b' in main().
You conclude wrong.
// function returns an X object by value
X f() {
X a;
cout << "&a in f() is " << &a << endl;
return a;
}

int main() {
X b = f();
cout << "&b in main() is " << &b << endl;
return 0;
}


b doesn't necessarily exist in main until after f() is called . a in f()
doesn't exist after f() returns. It is therefore quite possible that the
same memory is used for both. However, they are NOT the same
object. The destructor for f()'s a has been run prior to the construction
of b in main().
Jul 19 '05 #2
Thanks for the reply.
I created a destructor for class X which prints a message so you know it is
called.
The destructor was only called once, not twice.
I therefore again conclude that they are the same object.
Thank you,
Joe Hesse

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************@n ews.newshosting .com...

"Joe Hesse" <jo*******@actc x.com> wrote in message news:3f******** *************** @newsreader.vis i.com...
When I run the following program (gnu C++) the address of the automatic
object 'a'
in f() is the same as the address of the automatic object 'b' in main().
Thats fine.

I conclude that 'a' in f() is the same object as 'b' in main().


You conclude wrong.
// function returns an X object by value
X f() {
X a;
cout << "&a in f() is " << &a << endl;
return a;
}

int main() {
X b = f();
cout << "&b in main() is " << &b << endl;
return 0;
}


b doesn't necessarily exist in main until after f() is called . a in f()
doesn't exist after f() returns. It is therefore quite possible that the
same memory is used for both. However, they are NOT the same
object. The destructor for f()'s a has been run prior to the

construction of b in main().

Jul 19 '05 #3
> /*****
When I run the following program (gnu C++) the address of the automatic
object 'a'
in f() is the same as the address of the automatic object 'b' in main().

I conclude that 'a' in f() is the same object as 'b' in main().

Is this an error or an optimization?
It's an optimization known as Return Value Optimization (RVO), explicitly
allowed by the standard

Thank you,
Joe Hesse
*****/

#include <iostream>
using namespace std;

class X {
private:
int x;
public:
X(int a = 0) : x(a) {};
};

// function returns an X object by value
X f() {
X a;
cout << "&a in f() is " << &a << endl;
return a;
}

int main() {
X b = f();
cout << "&b in main() is " << &b << endl;
return 0;
}

Jul 19 '05 #4

"Joe Hesse" <jo*******@actc x.com> wrote in message news:3f******** *************** @newsreader.vis i.com...
I created a destructor for class X which prints a message so you know it is
called.
The destructor was only called once, not twice.
The G++ is defective. Or your destructor bugging is wrong.
I therefore again conclude that they are the same object.


Your conclusions are still INCORRECT.
Jul 19 '05 #5
Joe Hesse wrote in news:3f******** *************** @newsreader.vis i.com:
/*****
When I run the following program (gnu C++) the address of the
automatic object 'a'
in f() is the same as the address of the automatic object 'b' in
main().

I conclude that 'a' in f() is the same object as 'b' in main().

Is this an error or an optimization?

Its called Named Return Value Optimisation (NRVO). Some details inline.

Thank you,
Joe Hesse
*****/

#include <iostream>
using namespace std;

class X {
private:
int x;
public:
X(int a = 0) : x(a) {};
};

// function returns an X object by value
X f() {
X a;
Here "a" (the named return value) has been put into the
return-value-location (see below).

cout << "&a in f() is " << &a << endl;
return a;
}

int main() {
X b = f();
Here "&b" is passed as the return-value-location to f() and f()
constructs "b" directly.
cout << "&b in main() is " << &b << endl;
return 0;
}


Note also that NRVO is a special optimization as it iss allowed
to bypass a copy-constructor even if that constructor has side
effects (but the copy-ctor must be accesable).

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #6
Rob Williscroft <rt*@freenet.RE MOVE.co.uk> wrote in message news:<Xn******* *************** ************@19 5.129.110.200>. ..
Joe Hesse wrote in news:3f******** *************** @newsreader.vis i.com:

I conclude that 'a' in f() is the same object as 'b' in main().

Is this an error or an optimization?


Its called Named Return Value Optimisation (NRVO). Some details inline.
X f() {
X a;


Here "a" (the named return value) has been put into the
return-value-location (see below).
cout << "&a in f() is " << &a << endl;
return a;
}

int main() {
X b = f();


Here "&b" is passed as the return-value-location to f() and f()
constructs "b" directly.
cout << "&b in main() is " << &b << endl;
return 0;
}


Note also that NRVO is a special optimization as it iss allowed
to bypass a copy-constructor even if that constructor has side
effects (but the copy-ctor must be accesable).


That is interesting. So "f()", or any other function which
uses RVO is actually used as a constructor. Right? Right?

Except from the case of operator overloading, the only application
of RVO functions is to be used for chosing an alternative function
to do the initialization of members 'n stuff. Yes?

I was thinking: what's the difference between making f() a member
function instead of an RVO non-member? It seems that the RVO case has
the advantage of bypassing the normal constructor calls -- an
authority given by the standard.

Any other application of RVO, I'm missing?

Thanks,

stelios
Jul 19 '05 #7
stelios xanthakis wrote in news:8a018872.0 310160201.29cb9 9d0
@posting.google .com:

That is interesting. So "f()", or any other function which
uses RVO is actually used as a constructor. Right? Right?
Well that one way of looking at it, though note the word optimization,
I understand this as meaning optional feature. i.e. a compiler doesn't
have to do it, but good ones will.

IIUC optimization also means the compiler doesn't have to do it
every time, i.e. it isn't required to be consistant about when
it does the optimization.

Except from the case of operator overloading, the only application
of RVO functions is to be used for chosing an alternative function
to do the initialization of members 'n stuff. Yes?

No, any function that returns by value can benifit. Typicaly:
class_type a;
a = f();
will require f() to return a temporary which is then assigned with
operator =( class_type const & ) to a. With RVO/NRVO this temp can
be constructed directly by f(), an optimization. Also useful in
expresion's like g(f()); g{}'s temp argument can again be constructed
directly by f().
I was thinking: what's the difference between making f() a member
function instead of an RVO non-member? It seems that the RVO case has
the advantage of bypassing the normal constructor calls -- an
authority given by the standard.
It does, but as I noted previously the copy-ctor needs to be accesible
even though it doesn't get called, also because its an optimisation you
can't use (N)RVO as a way of changing the meaning of your code.

Any other application of RVO, I'm missing?


Its just a Good Thing(tm).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #8
Even though my class X has no copy constructor, it seems like RVO is
bypassing a copy constructor.
If the copy constructor had a side effect, like turning on a motor, then the
optimization prevents the motor from being turned on, and, therefore,
the optimization changes the semantics of the program.
Since RVO is allowed by the standard, is there a place in the standard that
says that observable side effects should not be placed in copy constructors?
Thank you,
Joe Hesse

"Erik" <no@spam.com> wrote in message news:bm******** **@news.lth.se. ..
/*****
When I run the following program (gnu C++) the address of the automatic
object 'a'
in f() is the same as the address of the automatic object 'b' in main().

I conclude that 'a' in f() is the same object as 'b' in main().

Is this an error or an optimization?


It's an optimization known as Return Value Optimization (RVO), explicitly
allowed by the standard

Thank you,
Joe Hesse
*****/

#include <iostream>
using namespace std;

class X {
private:
int x;
public:
X(int a = 0) : x(a) {};
};

// function returns an X object by value
X f() {
X a;
cout << "&a in f() is " << &a << endl;
return a;
}

int main() {
X b = f();
cout << "&b in main() is " << &b << endl;
return 0;
}


Jul 19 '05 #9


Joe Hesse wrote:

Even though my class X has no copy constructor, it seems like RVO is
bypassing a copy constructor.
If the copy constructor had a side effect, like turning on a motor, then the
optimization prevents the motor from being turned on, and, therefore,
the optimization changes the semantics of the program.
Since RVO is allowed by the standard, is there a place in the standard that
says that observable side effects should not be placed in copy constructors?
Thank you,


No. That's up to you to know.
If the compiler is allowed to optimize the cctor away, even if there are
side-effects, then it's clear that you should not do this, because the
compiler is allowed to silently change the semantics of your program.
The standard only says what a compiler is allowed to do and what it is
not allowed to do. Which conclusions you draw from that is up to you.

From your given example, it seems you are not 100% sure what should
and what should not go into a cctor. You first have to ask yourself
one thing: What is the intended purpose of a copy constructor?
And the answer is: To create an exact copy of a given object.
So if your cctor turns on a motor in an object, then the copy is
not a 100% copy of the original object (which had the motor turned off),
which is against the intention of a cctor. You may get away with it, but
you have to be careful.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #10

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

Similar topics

0
2039
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
6
1405
by: Olaf Martens | last post by:
Greetings! Please consider the following piece of program code (note that I have stripped quite a lot of code here): int foo(void) { unsigned short l_valbuf; // address of this goes to another function Objtype *l_item; // I want to get this one's data!
3
1735
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
17
16371
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee part of Manager is copied. ".... and gives the code above as .....
27
2297
by: onkar | last post by:
#include<stdio.h> int main(void){ char a="abcde"; char *p=a; p++; p++; p='z'; printf("%s\n",p); return 0; }
13
3007
by: Chris Carlen | last post by:
Hi: I have begun learning Python by experimenting with the code snippets here: http://hetland.org/writing/instant-python.html In the section on functions, Magnus Lie Hetland writes: -------------------------------------------------------------------- For those of you who understand it: When you pass a parameter to a
11
3341
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
25
2644
by: sidd | last post by:
In the following code: int i = 5; ---it goes to .data segment int j; ---it goes to bss segment int main() { int c; int i = 5; ---stack
25
2340
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
8063
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
8003
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
8498
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
8152
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
8341
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
6817
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
6014
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...
1
1598
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1331
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.