pass-by-value

X

xdevel

Hi, I need your help because I don't understand very well this:

in C arguments are passed by-value. The function parameters get a copy
of the argument values.
But if I pass a pointer what really is happening? also a copy is passed
?

in C++ there is a pass-by-reference too... and in that case the
paramter can be considered as an
alias of the argument...

but now in C when we pass a pointer can we think of it as a
pass-by-address mechanism to manipulate
variables?

Thanks
 
R

Richard Heathfield

xdevel said:
Hi, I need your help because I don't understand very well this:

in C arguments are passed by-value.
Correct.

The function parameters get a copy of the argument values.

More precisely, arguments are *expressions*. The argument expressions are
evaluated, and those values are stored in the parameters to the function.
But if I pass a pointer what really is happening?

The pointer is evaluated, and that pointer's value is stored in the
parameter to the function, just the same as any other argument expression.
 
R

robertwessel2

xdevel said:
Hi, I need your help because I don't understand very well this:

in C arguments are passed by-value. The function parameters get a copy
of the argument values.


That's correct.

But if I pass a pointer what really is happening? also a copy is passed
?


A copy of the *pointer* is passed. The thing it's pointing to is *not*
copied.

in C++ there is a pass-by-reference too... and in that case the
paramter can be considered as an
alias of the argument...


While OT here, C++ references are really just a different way to refer
to pointers. It's just syntactic sugar that hides the "&" (at the
call) and "*()" (inside the function) operators from you.

but now in C when we pass a pointer can we think of it as a
pass-by-address mechanism to manipulate
variables?


If I'm interpreting your question correctly, yes. The passed pointer
will point to the original object, and the original object may be
accessed via that pointer.
 
X

xdevel

(e-mail address removed) ha scritto:
While OT here, C++ references are really just a different way to refer
to pointers. It's just syntactic sugar that hides the "&" (at the
call) and "*()" (inside the function) operators from you.

ok but if I write a swap function with reference I can do this:
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;

tmp = a;
a = b;
b = tmp;
}
here I can swap the objects directly

and instead with pointer I can do this:
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
I can't swap directly the pointers...

and so my question is why if pass-by-reference can be considerd as a
pass-by-pointer
is the code different?

what type of pointer is a reference?
 
X

xdevel

Richard Heathfield ha scritto:
The pointer is evaluated, and that pointer's value is stored in the
parameter to the function, just the same as any other argument expression.

so in C exists only a pass-by-value mechanism in fact also if we pass a
pointer we are passing a copy of it while in C++ exists a pass-by-value
and a pass-by-reference mechanism
 
R

Roland Pibinger

xdevel said:

The pointer is evaluated, and that pointer's value is stored in the
parameter to the function, just the same as any other argument expression.

Sometimes called 'pass by address'.

Best regards,
Roland Pibinger
 
R

robertwessel2

xdevel said:
(e-mail address removed) ha scritto:


ok but if I write a swap function with reference I can do this:
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;

tmp = a;
a = b;
b = tmp;
}
here I can swap the objects directly


Why not just:

void swap(int &a, int &b)
{
int tmp;

tmp = a;
a = b;
b = tmp;
}

This at least matches the pointer based variant below.


and instead with pointer I can do this:
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
I can't swap directly the pointers...

and so my question is why if pass-by-reference can be considerd as a
pass-by-pointer
is the code different?

what type of pointer is a reference?


Typically a reference parameter in a function call works internally
just like a pointer. I'd expect the generated code to generally be
identical. Using the swap example above, calling the reference version
with swap(x, y) would likely generate the identical code as calling the
pointer version with swap(&x, &y). Inside the reference version of
swap, the line a=b will essentially be transformed into *(A)=*(B) by
the compiler, where A and B are the internal pointers representing the
a and b parameters.

Of course the actual code generated by any particular compiler is
entirely up to the whim of the developers, and there's no reason they
cannot generate different code for the two cases.

References add no new functions to C++, but do allow a consistent way
to access member data and functions. That makes it possible to define
new types that have similar syntactic characteristics as the basic
types.
 
R

Richard Heathfield

xdevel said:
Richard Heathfield ha scritto:


so in C exists only a pass-by-value mechanism
Correct.

in fact also if we pass a pointer we are passing a copy of it

More precisely, we are passing its value.

<off-topic stuff snipped>
 
R

Richard Heathfield

Roland Pibinger said:
Sometimes called 'pass by address'.

Why confuse the issue? The pointer's value is passed. In this respect,
expressions involving pointers and used as arguments are no different to
any other expressions that are used as arguments. There is no need to
invent a special terminology for pointers-as-arguments.
 
C

Chris Dollin

Roland said:
Sometimes called 'pass by address'.

I've never heard pass-by-value of a pointer value be called
`pass by address`. I /have/ heard `pass [or call] by reference`
be called `pass by address`.

C's evaluation rules for arguments are the same as its evaluation
rules for expressions: it doesn't have special magic for pointer
expressions. (The nearest it has to a special pointer magic is that
array names evaluate to the address of their first element.)
 
M

Mark McIntyre

and so my question is why if pass-by-reference can be considerd as a
pass-by-pointer is the code different?

Since References are C++, you need to ask over in comp.lang.c++.

By the way, pass by reference CANT be considered as by pointer since,
as you observed, they have different semantics and effects. Anyone who
says they're equivalent has not thought it through.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

jmcgill

xdevel said:
Hi, I need your help because I don't understand very well this:

in C arguments are passed by-value. The function parameters get a copy
of the argument values.
But if I pass a pointer what really is happening? also a copy is passed
?

C function parameters are passed by value. There are *no* exceptions.

When you pass a pointer, the pointer is passed by value.

If you want to think of your design in terms of pointers being
references to some data, that is acceptable, but the mechanics of the
function cal are still, strictly, pass-by-value.

Say you have a pointer to a struct. You can call that a reference to
the struct, if it helps you. But when you pass that reference, the
reference itself is passed by value.
 
C

Chris Torek

(e-mail address removed) ha scritto:
But this is exactly what "pass by reference" *is*: a method by
which the language hides the "implicit pointers", so that the
programmer does not have to write them out (and, in general, *cannot*
omit them either).

ok but if I write a [C++] swap function with reference I can do this:
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;

You do not need this extra reference -- you can just write "int tmp;".
tmp = a;
a = b;
b = tmp;
}
here I can swap the objects directly

Not only that, you can*not* swap the pointers that the implementation
secretly uses internally.
and instead with pointer I can do this:
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}

and in fact, if all you have are pointers that you pass by value,
you *must* write this.
I can't swap directly the pointers...

You *can*; it just has no effect on the caller, because the
pointer values in swapP() are copies of the values supplied
by the caller.
and so my question is why if pass-by-reference can be considerd as a
pass-by-pointer
is the code different?

Might as well ask "why, if horses can be considered as a zebra,
is the code different?" They may be related, but they are not
the same thing. (In fact, "by-pointer" is not a Term of Art in
compiler circles, while "by-reference" is. The three common
mechanisms available to Fortran compilers, for instance, are
called "value", "reference", and "value-result". There is also
a "call by name" mechanism, using "thunks", for Algol.)
 
A

av

Why not just:

void swap(int &a, int &b)
{

i like to write "swap(&a, &b);" because so
i see that swap() function change "a" and "b"

in what i see reference in c++ is useful for class operators functions
for write something like c=a+b; (a, b, are reference in "+" operator
the same for "=" operator; evenif not change a and b
there is not overead for pass pointers (like reference) to stucts in
"+" operator (otherwise it seems will be &c=&a+&b or a, b, c have to
be pointers for write "c=a+b;" ) )
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

xdevel said:
(e-mail address removed) ha scritto:


ok but if I write a swap function with reference I can do this:
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;

tmp = a;
a = b;
b = tmp;
}
here I can swap the objects directly

and instead with pointer I can do this:
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
I can't swap directly the pointers...
You could if you do:

void swapP(int **a, int **b)
{
int *tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top