Call by value in C

P

priyasmita_guha

C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?
 
M

Michael Mair

C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.

The latter is a misconception; C++ can use both.
Ask in comp.lang.c++ about call by value vs. call by reference.

>How is call by value advantageous and how is it
> implemented?

You can easier implement it as there are no conceptual problems
like "null-references"; how this is done is implementation
specific. It may be copying the values of the first N arguments
to certain registers and the rest into the stack. This has
nothing to do with standard C as it is discussed here, though.

Ask in a platform or implementation specific newsgroup to find
out how it is implemented.


Cheers
Michael
 
P

pemo

C uses call by value for passing of parameters in contrast to C++
which uses call by reference.How is call by value advantageous and
how is it implemented?


So, C++ has call by value and reference. Of course, a C++ programmer can
also 'fake' call by reference - as C does - using a pointer [the pointer is
passed by value, but by dereferencing it, one can access the original
'object'].

How it's done is not specified, but obviously, some 'copy' of actual
parameters is required.

Advantages: one obvious one is that a called function can only
alter/work-with 'copies' of any actual parameters passed to it - so, the
function can go crazy within its own little world [it's a bit like a self
contained program], but cannot affect the caller's original 'objects'.

An obvious disadvantage is that creating copies takes some amount of time -
usually very little of course. However, if a routine is called very many
times a second, this could affect performance. To help here, C has the const
keyword, e.g., char const * const c.
 
M

Malcolm

C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?
Say we've got this code.

double distsquared = 100000000;
double dist;
double force;
double radius = 8000.0;

dist = sqrt(distsquared);
if(dist > radius)
force = 1 / distsquared;

perfectly unexceptional code. We are calculating the force between two
planets, which is the reciprocal of the square of distance. However if the
distance is less than the radius, then the calculation breaks down.

Now the code works because we know that the variable distsquared hasn't been
changed by the call to sqrt().

If we had a call by reference semantic, we wouldn't know this.
distsquared could be prserved with its original value, it could be set to
the square root, it could be corrupted or set to zero. We don't know, so our
finger is in the back of the manual, looking for the convention the writer
of sqrt() has chosen.

In call be reference the fucntion sqrt() can "see" distsquared, and work on
it directly.
It is however harder to implement call by value.

The normal way is to maintain a "stack".
So when sqrt() is called, the value of distsquared is pushed onto the stack.
The sqrt() function can then corrupt it to its heart's content. When control
returns, the stack is popped, and the copy discarded.

Incidentally your C++ comment is only partly correct. See the comp.lang.c++
faq for some more infomration about C++ parameter passing.
 
M

Malcolm

pemo said:
An obvious disadvantage is that creating copies takes some amount of
time - usually very little of course. However, if a routine is called
very many times a second, this could affect performance. To help here, C
has the const keyword, e.g., char const * const c.
const doesn't really help with this.
The pointer is still copied, but the data it points to cannot be modified.
const is really there to help the human programmer know what he may and may
not modify, not to help the compiler optimise.
 
R

Richard G. Riley

const doesn't really help with this.
The pointer is still copied, but the data it points to cannot be modified.
const is really there to help the human programmer know what he may and may
not modify, not to help the compiler optimise.

If the data it points to can not be modified then surely it is a
fallacy to deny that this can be used to optimise the compiler. Unless
of course you are silly enough to cast an assignment of the CONST
pointer to a non const.
 
C

Chris Torek

If the data it points to can not be modified then surely it is a
fallacy to deny that this can be used to optimise the compiler.

The problem lies in the "if" part.
Unless of course you are silly enough to cast an assignment of the CONST
pointer to a non const.

This is not where the trouble occurs. Consider the following C
code fragment:

int a[100];

void f(const int *p, size_t n) {
size_t i;

for (i = 1; i < n; i++)
a += p[i - 1];
}

This adds each p[i-1] to a, 1 <= i < n. The items to which p
points are marked "const", so f() cannot change them. But they
can change!

int g(void) {
...
f(a, 100);
...
}

An optimizing compiler might "want" to preload the elements in p[],
to make this code go faster. If we assume that the elements in
p[] do not change, this optimization is valid. But p[i-1] names
the same array element as a[i-1], so each modification to a
modifies the value that will be summed next. The desired optimization
is *not* valid, despite the "const".

This is why C99 has "restrict", which actually means what most
people think they want when they write "const".
 
R

Richard G. Riley

This is not where the trouble occurs. Consider the following C
code fragment:

int a[100];

void f(const int *p, size_t n) {
size_t i;

for (i = 1; i < n; i++)
a += p[i - 1];
}

int g(void) {
...
f(a, 100);
...
}

This is why C99 has "restrict", which actually means what most
people think they want when they write "const".


Yuck! In this example you are, of course, right :)
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top