Address of temporary objects

A

Ariel

When passing an object by value to some function foo(), the copy
constructor gets called and inside it i see that the address of the
temporary obect is X.
but when entering to foo() i see that the address of the temporary
object is Y.
By looking at the assembly i think that the copiler copies the
temporary object to a different location after calling the copy
constructor.
Is it legal?
thanks ahead,
Ariel Erenberg
 
P

Phlip

Ariel said:
When passing an object by value to some function foo(), the copy
constructor gets called and inside it i see that the address of the
temporary obect is X.
but when entering to foo() i see that the address of the temporary
object is Y.
By looking at the assembly i think that the copiler copies the
temporary object to a different location after calling the copy
constructor.
Is it legal?

You are looking at the "as if" rule.

C++ is defined in terms of an arbitrary CPU's opcodes, so passing a
reference is defined to work like, roughly, pushing an object's address onto
a stack. However, C++ has an over-arching rule called "as if", which means a
real compiler may emit opcodes to do anything, so long as the resulting
program works "as if" it had followed the rules.

For example, this is illegal:

#define int float
#include <iostream>

I suspect it's "undefined behavior". The compiler permits #include to behave
"as if" it had imported a real text file. Implementors are allowed to treat
#include <iostream> as a single command that inserts all those Standard
Library identifiers. So because the act of redefining a keyword and then
importing a Standard Library header is undefined, the compiler needn't
compile any file when it #includes.

In your case, because legal code cannot use the address of a temporary, the
compiler is quietly copying the object as it passes.
 
A

Ariel

Phlip said:
You are looking at the "as if" rule.

C++ is defined in terms of an arbitrary CPU's opcodes, so passing a
reference is defined to work like, roughly, pushing an object's address onto
a stack. However, C++ has an over-arching rule called "as if", which means a
real compiler may emit opcodes to do anything, so long as the resulting
program works "as if" it had followed the rules.

For example, this is illegal:

#define int float
#include <iostream>

I suspect it's "undefined behavior". The compiler permits #include to behave
"as if" it had imported a real text file. Implementors are allowed to treat
#include <iostream> as a single command that inserts all those Standard
Library identifiers. So because the act of redefining a keyword and then
importing a Standard Library header is undefined, the compiler needn't
compile any file when it #includes.

In your case, because legal code cannot use the address of a temporary, the
compiler is quietly copying the object as it passes.

But i can think of legal cases like if one field in my class is a
pointer to another field and in the copy constructor i'm initializing
that pointer.
so in the calling function the pointer will not point to the other
member.

Actually the problem arises in a smart pointer class that saves the
offset from "this" to the real pointer. and of course it builds on the
fact that "this" will stay the same after it has been initialized.
Thanks
Ariel
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top