About memory management with references

S

Scott Danzig

I'm curious.. let's say you had:
int val = 5;

int a()
{
return val;
}

int &b()
{
return val;
}

void c()
{
int i;
int &j;

i = a(); // Line 1
i = b(); // Line 2
j = a(); // Line 3
j = b(); // Line 4
}

I'm wanting to know the details of what's copied when in each of the 4
lines. Here's my guess:

Line 1: (2 int copies, 0 address copies)
val is copied onto the stack as a return value, then copied again into
i's storage space.

Line 2: (1 int copy, 1 address copy)
an address of val is copied onto the stack as a return value, then the
value it points to is copied into i's storage space.

Line 3: (1 int copy, 1 address copy)
I'm not sure about this one. It looks like val is copied onto the
stack as a return value, but then the address of that return value is
stored as a reference. In this case, I'd think the value should be
undefined, no?

Line 4: (0 int copies, 2 address copies)
an address to val is copied onto the stack as a return value, then the
address is copied to be i's new address.


Can someone confirm/deny/clarify this? Thanks.

- Scott
 
G

Gianni Mariani

Scott said:
I'm curious.. let's say you had:
int val = 5;

int a()
{
return val;
}

int &b()
{
return val;
}

void c()
{
int i;
int &j;
^^^^^^^^^^^^^^^^^^ this is not valid C++
reference.cpp:16: error: `j' declared as reference but not initialized

int &j = b(); // this would bind j to val
i = a(); // Line 1
i = b(); // Line 2

i would be assigned the value in val;
j = a(); // Line 3
j = b(); // Line 4
}

I'm wanting to know the details of what's copied when in each of the 4
lines. Here's my guess:

The compiler *may* inline some of these so your answers here are mostly
worst case.
Line 1: (2 int copies, 0 address copies)
val is copied onto the stack as a return value, then copied again into
i's storage space.
Yes.


Line 2: (1 int copy, 1 address copy)
an address of val is copied onto the stack as a return value, then the
value it points to is copied into i's storage space.
Yes.


Line 3: (1 int copy, 1 address copy)
I'm not sure about this one. It looks like val is copied onto the
stack as a return value, but then the address of that return value is
stored as a reference. In this case, I'd think the value should be
undefined, no?

Implementation specific. A reference (or in this case address) may not
be copied at all.
Line 4: (0 int copies, 2 address copies)
an address to val is copied onto the stack as a return value, then the
address is copied to be i's new address.


No 1 - same as line 3.

Can someone confirm/deny/clarify this? Thanks.

References can only be bound at initialization. They're kind of special
in that way.
 
R

Rolf Magnus

Scott said:
I'm curious.. let's say you had:
int val = 5;

int a()
{
return val;
}

int &b()
{
return val;
}

void c()
{
int i;
int &j;

You can't create a reference without initializing it.
i = a(); // Line 1
i = b(); // Line 2
j = a(); // Line 3
j = b(); // Line 4
}

I'm wanting to know the details of what's copied when in each of the 4
lines. Here's my guess:

Line 1: (2 int copies, 0 address copies)
val is copied onto the stack as a return value, then copied again into
i's storage space.
Yes.

Line 2: (1 int copy, 1 address copy)
an address of val is copied onto the stack as a return value, then the
value it points to is copied into i's storage space.

Most compilers will do this internally, but there is no requirement in
C++ that a reference works like a pointer behind the scenes.
Line 3: (1 int copy, 1 address copy)
I'm not sure about this one. It looks like val is copied onto the
stack as a return value, but then the address of that return value is
stored as a reference.

No. The return value is stored in the variable that j refers to. You
cannot change a reference.
In this case, I'd think the value should be undefined, no?

Since it shouldn't be possible to define an uninitialized reference in
the first place, this has not really a point.
Line 4: (0 int copies, 2 address copies)
an address to val is copied onto the stack as a return value, then the
address is copied to be i's new address.

No. The value of val is copied into the variable that j refers to.
 
S

Scott Danzig

Gianni Mariani said:
^^^^^^^^^^^^^^^^^^ this is not valid C++
reference.cpp:16: error: `j' declared as reference but not initialized

int &j = b(); // this would bind j to val


i would be assigned the value in val;


The compiler *may* inline some of these so your answers here are mostly
worst case.


Implementation specific. A reference (or in this case address) may not
be copied at all.



No 1 - same as line 3.



References can only be bound at initialization. They're kind of special
in that way.

Yep, it's tough to type without errors when you're doing a newsgroup post.
I know about needing to bind references at initialization (just like
defining consts). Your help is enlightening and appreciated.

- Scott
 
S

Scott Danzig

Rolf Magnus said:
You can't create a reference without initializing it.


Most compilers will do this internally, but there is no requirement in
C++ that a reference works like a pointer behind the scenes.


No. The return value is stored in the variable that j refers to. You
cannot change a reference.


Since it shouldn't be possible to define an uninitialized reference in
the first place, this has not really a point.


No. The value of val is copied into the variable that j refers to.

Thanks for your help. Very clear. Cool name, by the way. :)

- Scott
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top