Problem understanding automatic objects

J

Joe Hesse

/*****
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;
}
 
R

Ron Natalie

Joe Hesse said:
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().
 
J

Joe Hesse

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
 
E

Erik

/*****
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
 
R

Ron Natalie

Joe Hesse said:
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.
 
R

Rob Williscroft

Joe Hesse wrote in
/*****
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.
 
S

stelios xanthakis

Rob Williscroft said:
Joe Hesse wrote in news:[email protected]:

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


Here "a" (the named return value) has been put into the
return-value-location (see below).


Here "&b" is passed as the return-value-location to f() and f()
constructs "b" directly.


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
 
R

Rob Williscroft

stelios xanthakis wrote in @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.
 
J

Joe Hesse

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
 
K

Karl Heinz Buchegger

Joe said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top