473,396 Members | 1,775 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,396 software developers and data experts.

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 1992

"Joe Hesse" <jo*******@actcx.com> wrote in message news:3f***********************@newsreader.visi.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*********************@news.newshosting.com. ..

"Joe Hesse" <jo*******@actcx.com> wrote in message news:3f***********************@newsreader.visi.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*******@actcx.com> wrote in message news:3f***********************@newsreader.visi.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.visi.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.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.200>...
Joe Hesse wrote in news:3f***********************@newsreader.visi.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.0310160201.29cb99d0
@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
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...
6
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...
3
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
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...
27
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
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: ...
11
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...
25
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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...
0
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...
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,...

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.