Pointers vs References

S

spekyuman

What processes occur during a function call? Specifically, how are
pointers and references treated during this time? What happens to
their memory and what they reference during this particular time? Here
are some example functions to help with the elaboration process:

Object naiveFunction(Object copy)
{
// sizeof (copy) => 750 x 2^20 bytes
return copy;
}

void pointerFunction(Object* ptr)
{
// sizeof (ptr) => 4 bytes
// sizeof (*ptr) => 750 x 2^20 bytes
}

void referenceFunction(Object& ref)
{
// sizeof (ref) => 750 x 2^20 bytes
}
 
V

Victor Bazarov

spekyuman said:
What processes occur during a function call? Specifically, how are
pointers and references treated during this time? What happens to
their memory and what they reference during this particular time? Here
are some example functions to help with the elaboration process:

Do your own homework, will you?
 
S

spekyuman

Do your own homework, will you?

Number one, do not mouth off on random posts in the illusion of acting
sophisticated. Why? It displays nothing more than your qualities as
human... Cough cough [you lack insight]... Elaborating further on the
main subject to illustrate the point of this post: A reference is an
alias, or reference [=-O], to another variable which allows you to
indirectly access its storage. A pointer allows you to do much the
same and yatta yatta... Since we now share a common knowledge, lets
talk about the sophisticated processes of sending data to a procedure
[=-O]. This is where the heart of my question lies:

[1] Sending data by value to a procedure requires data to be copied
from the caller to the routine.
[2] Sending a pointer to a procedure requires data to be copied from
the caller to the routine. Specifically, the pointer address is sent
by value and reports a size of 4-8 bytes on modern systems; only the
object data is being passed by reference.
[3] My question is, what exactly happens with a reference type during
a procedure call? Is there any overhead? What are the mechanisms at
work?
 
V

Victor Bazarov

spekyuman said:
[..] This is where the heart of my question lies:

[1] Sending data by value to a procedure requires data to be copied
from the caller to the routine.

From the factual argument into the formal argument. Or, simply put,
the local variable of the function has to be initialised with the same
value (copy-initialised, of course).
[2] Sending a pointer to a procedure requires data to be copied from
the caller to the routine. Specifically, the pointer address

The pointer address?
is sent
by value and reports a size of 4-8 bytes on modern systems; only the
object data is being passed by reference.

The pointer is passed by value: the local variable is initialised with
the value of the pointer that the caller supplies as the argument.
Nothing else is passed by no other method.
[3] My question is, what exactly happens with a reference type during
a procedure call?

The reference (the local variable of the function) is initialised by
the l-value passed to it by the caller.
Is there any overhead? What are the mechanisms at
work?

Some implementation-defined mechanisms. How references are implemented
and what (if any) overhead there can be observed, is not specified by
the language. If you'd like to discuss a particular implementation,
you're welcome to, just not here.

Initialising a reference argument (an argument that has a reference
type) is no different than initialising one without the interference
of a function call/invocation.

Object someObject;
Object & rObject = someObject;

what mechanisms are at work? Is there overhead? Implementation-defined.

V
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top