What is difference between "passed by value-result", "passed by reference" and "passed by name"?
I have a program in C++:
#include <stdio.h>
#include <iostream>
using namespace std;
void swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
int main() {
int value = 2, list[5] = {1, 3, 5, 7, 9};
swap(value, list[0]);
swap(list[0], list[1]);
swap(value, list[value]);
return 0;
}
And this is the solution after call swap : http://imgur.com/1HkCAnm
I don't know the difference between them. Please help me explain it.
C++ uses call by value by default, and can use call by reference if the argument is correctly decorated. (Pointer arguments pass the value of the pointer.)
If you specify a reference argument (int& a) in the updated sample below), your swap function will work.
Call by value-result isn't supported by C++; it works by passing the value in at the start of the function and copying the value out at the end of the function.
Call by name is just weird. Instead of passing values, it passes bits of code (aka thunks) that evaluate the variable (in the calling scope). Array references are notorious for not being evaluated as one would expect using call by name.
void swap(int& a, int& b) {
int temp;
temp = a;
a = b;
b = temp;
}
Related
I know this is already a commonly asked question, but I'm more curious about how pointers and references behave at a lower level (like how compiler deals with them, and how they look like in memory), and I didn't find a solution, so here I am.
At first I was wondering if an array can be passed as a parameter without being cast (or decay) into a pointer. More specifically. I would like the following code:
void func(?? arr) {
cout << sizeof(arr) << "\n";
}
int main() {
int arr[4];
func(arr);
return 0;
}
to output 16 instead of 8, which is the size of a pointer.
First I tried
void func(int arr[4]);
Hoping that specifying the size can keep the property of an array, but arr is still treated as a pointer.
Then I found something that worked:
void func(int (&arr)[4]);
But it confused me.
In the past I was under the impression that although pointers and references had different meanings, they had the same behavior when the code was actually executed.
I got that idea from my own experiments:
void swap(int* a, int* b) {
int c = *a;
*a = *b;
*b = c;
}
int main() {
int a = 3, b = 5;
swap(&a, &b);
}
and
void swap(int& a, int& b) {
int c = a;
a = b;
b = c;
}
int main() {
int a = 3, b = 5;
swap(a, b);
}
were compiled into the same assembly code, and so did
int main() {
int a = 3;
int& b = a;
b = 127;
return 0;
}
and
int main() {
int a = 3;
int* b = &a;
*b = 127;
return 0;
}
I turned off optimization and both g++ and clang++ showed this result.
Also my thought on the first experiment:
when thinking in terms of memory, swap should have its own stack frame and local variables. Having a in swap directly mapped to the a in main didn't make much sense to me. It was as if the a in main magically appeared in the stack frame of swap, where it shouldn't belong. So it didn't surprise me that it got compiled into the same assembly as the pointer version. Maybe the magic of reference was achieved by a pointer underneath the hood. But now I'm not sure.
So how does a compiler handle references and how do references look like in memory? Are they variable that occupies space? And how can I explain the result of my experiments and the array problem?
The old C idiom is, that arrays get turned into pointers to the array type, when a function is called. And that is mostly still true für C++.
So for the details of it, let's look at two functions taking an integer argument, first:
void f(int x) { x = 42; } // function called with x by value
void g(int& x) { x = 42; } // function called with x by reference
As f() is called by value, a copy of the argument is made, and the assignment x = 42; inside the function has effectively no effect for the caller of the function. But for g(), the assignment of x becomes visible to the caller:
int a = 0, b = 0; // initialize both a and b to zero
f(a); // a is not changed, because f is called by value
g(b); // b is changed, because g is called by reference
std::cout << a << " " << b << std::endl; // prints 0 42
For arrays, the same rules should hold, but don't. So let's try it:
void farr(int x[4]) { x[0] = 42; } // hypothetical call by value
void garr(int (&x)[4]) { x[0] = 42; } // call by reference
And let's call these functions:
int c[4] = { 1, 2, 3, 4 }, d[4] = { 5, 6, 7, 8 };
farr(c); // call by value?
garr(d); // call by reference
for(unsigned i = 0; i < 4; i++)
std::cout << c[i] << (i < 3 ? ", " : "\n");
for(unsigned i = 0; i < 4; i++)
std::cout << d[i] << (i < 3 ? ", " : "\n");
The unexpected result is, that the farr() function (unlike the f function before) does modify its array, too. The reason is an old C idiom: Because arrays must not be copied directly by assignment, functions cannot be called with arrays by value. Therefore array declarations in parameter lists are automatically converted into a pointer to the first element of the array. So the following four function declarations are syntactically identical in C:
void farr1(int a[]) {}
void farr2(int a[4]) {}
void farr3(int a[40]) {}
void farr4(int *a) {}
Being compatible with C, C++ took over that property. So it is not a syntactical error (but likely causes undefined behaviour!) to call farr2() or farr3() with an array of different size. Furthermore, though, references come in in C++. And yes, as you already suspected, a reference to an array is internally represented as a pointer to the first array element. But, and that is the advantage of C++: If you call a function, that expects a reference to an array (and not just an array or a pointer), the size of array is actually validated!
So, calling farr() with an int-Array of size 5 is possible, calling garr() with the same leads to an compiler error. That gives you better type checking, so you should use it, whereever possible. And it even allows you to pass the array size to a function by using a template:
template<std::size_t N>
void harr(int (&x)[N]) { for(std::size_t i = 0; i < N; i++) x[i] = i*i; }
Why does the C/++ scanf function need referenced variables as arguments? I was wondering why scanf can't just take in the variable itself, since it gave me a weird warning whenever I didn't put in a referenced variable. It thought I had put in a pointer. Why is that? Thanks.
I don't see how they could modify the value
Here is an example
#include <stdio.h>
void changeNumber(int *n) {
*n = 12;
}
int main() {
int x = 5;
printf("X: %i\n", x);
changeNumber(&x);
printf("X: %i", x);
return 0;
}
Output
X: 5
X: 12
IIRC, scanf() functions are from C and required their arguments to be pointers because the original C did not have a pass-by-reference for parameters at all.
The programmer writing the function had to use pointers (which passed a memory address value) to achieve the result that pass-by-reference would have provided.
I learned to program with Pascal and this (along with the lack of array bounds-checking) was one of the most annoying things about C when I first learned it.
I believe C++ has pass-by-reference but the scanf functions are from the C language stdio so they pass parameters by value.
Think about the difference between two scanf function below:
#include <stdio.h>
void scanf_1(int a) {
a = 5;
}
void scanf_2(int *a) {
*a = 6;
}
int main()
{
int b = 1;
scanf_1(b);
printf("(1) b = %d\n", b);
scanf_2(&b);
printf("(2) b = %d\n", b);
return 0;
}
The output:
(1) b = 1
(2) b = 6
You can see, if you pass by value, the value of b is not changed. But when you use the pointer then pass by reference, the value of b is updated.
As a C++ learner, I came across the following piece of code from C++ Language Tutorial and got two questions, could any expert provide some guidance on those?
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int
(*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main () {
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
In function "operation" definition:
Question 1: for "g = (*functocall)(x,y)", does it deference the pointer that points to a function (ex.subtraction) and assign it to g?
Question 2: for "return(g)", I'm wondering why we put parenthesis for g?
And for "int (*minus)(int,int) = subtraction;" in the main function, is that okay if we write the following instead?
int (*minus)(int,int)
minus = subtraction
Question 1: for g = (*functocall)(x,y), does it deference the
pointer that points to a function (ex.subtraction) and assign it to g?
Answer: No.
(*functocall)(x,y);
is simply one allowable syntax for invoking the function from the pointer passed as a parameter. It is entirely equivalent to:
functocall(x,y);
Which simply eliminates the (*...) syntax which is allowable but unnecessary.
Question 2: for return(g), I'm wondering why we put parenthesis for
g?
The parenthesis too are discretionary and are superfluous to the return. Simply providing:
return g;
is all that is needed as there is no expression requiring evaluation priority provided by the enclosing (..).
Before I start, your code uses way more parenthesis than it needs.
Here is the same code, better formatted and reduced:
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return a+b; }
int subtraction (int a, int b)
{ return a-b; }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = functocall(x,y);
return g;
}
int main () {
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n<<"\n";
return 0;
}
for "g = (*functocall)(x,y)", does it deference the pointer that points to a function (ex.subtraction) and assign it to g?
In a nutshell - yes. When calling a function through it's pointer, it is not necessary to explicitly dereference it. I know, it's surprising and counter to the way pointers are typically used.
Think of it this way. Whenever you are just writing the name of a function, you are essentially getting the function's pointer. This is why you wrote as you did, rather than write:
m = operation (7, 5, &addition);
You do not need to explicitly dereference when using a function pointer for the same reason you do not need to explicitly use the address of operator when taking its address in the first place.
Question 2: for "return(g)", I'm wondering why we put parenthesis for g?
There is positively no good reason to. In fact, there is no reason to do it for any of your return statements.
And for "int (*minus)(int,int) = subtraction;" in the main function,
is that okay if we write the following instead?
int (*minus)(int,int);
minus = subtraction;
Personally, I recommend you use:
int (*minus)(int,int) = nullptr;
minus = subtraction;
so as not to leave uninitialized variables, but otherwise, yes, absolutely. A pointer to function variable is just like any other variable.
Here's the code:
#include<iostream>
using namespace std;
typedef struct ptrs
{
int (*addptr)(int a, int b);
}mems;
int add(int a, int b)
{
int result = a+b;
return result;
}
int main()
{
mems ptrtest;
ptrtest.addptr = &add;
int c = (*ptrtest.addptr)(3,4);
//int c = ptrtest.addptr(3,4);
cout << c << endl;
return 0;
}
if I replace the code int c = (*ptrtest.addptr)(3,4); with it's next line(annotated now), the result will be the same, why is that?
Of course, int c = (*ptrtest.addptr)(3,4); is the base case. However, in C++ (and in C as well), if you use the call (()) operator on a function pointer, it will do the "dereferencing" automatically. Just like when assigned to a variable of function pointer type, the name of a function decays into a function pointer, i. e.
int (*fptr)() = some_func;
is just as valid as
int (*fptr)() = &some_func;
albeit the type of func is int ()(void).
Functions and function pointers can be used interchangeably, presumably for convenience. In particular, section 5.2.2 of the C++11 standard specifies that a function call can occur using a function or a pointer to a function.
C++ will automatically cast a function name to a function pointer (and vise versa) if doing so will create correct syntax.
When I return pointer from the function, its value can be accessed individually. But when a loop is used to ouput the value of that pointer variable, wrong value is shown. Where I am making mistake, can't figure it out.
#include <iostream>
#include <conio.h>
int *cal(int *, int*);
using namespace std;
int main()
{
int a[]={5,6,7,8,9};
int b[]={0,3,5,2,1};
int *c;
c=cal(a,b);
//Wrong outpur here
/*for(int i=0;i<5;i++)
{
cout<<*(c+i);
}*/
//Correct output here
cout<<*(c+0);
cout<<*(c+1);
cout<<*(c+2);
cout<<*(c+3);
cout<<*(c+4);
return 0;
}
int *cal(int *d, int *e)
{
int k[5];
for(int j=0;j<5;j++)
{
*(k+j)=*(d+j)-*(e+j);
}
return k;
}
You are returning a pointer to a local variable.
k is created on the stack. When cal() exits the stack is unwound and that memory is free'd. Referencing that memory afterwards leads to undefined behaviour (as explained beautifully here: https://stackoverflow.com/a/6445794/78845).
Your C++ compiler should warn you about this and you should heed these warnings.
For what it's worth, here is how I'd implement this in C++:
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
int main()
{
int a[] = {5, 6, 7, 8, 9};
int b[] = {0, 3, 5, 2, 1};
int c[5];
std::transform (a, a + 5, b, c, std::minus<int>());
std::copy(c, c + 5, std::ostream_iterator<int>(std::cout, ", "));
}
See it run!
The int k[5] array is created on the stack. So it gets destroyed when it goes out of scope by returning from cal. You could use a third parameter as an output array:
void cal(int *d, int *e, int* k)
{
for(int j=0;j<5;j++)
{
*(k+j)=*(d+j)-*(e+j);
}
}
call cal like this:
int a[]={5,6,7,8,9};
int b[]={0,3,5,2,1};
int c[5];
cal (a, b, c); // after returning from cal, c will be populated with desired values
As others have pointed out, you're returning a pointer to a local
variable, which is undefined behavior. The real problem, however, is
that you need to return an array, and C style arrays are broken.
Replace your arrays with std::vector<int>, forget about the pointers
(because you're dealing with values), and the code will work.