Search code examples
c++cprogramming-languages

Which way you prefer to send arguments?


Suppose you have a function:

void fun(int *a, int *b);

Which way you would prefer to send the arguments?

(1.)

int x, y;
fun(&x, &y);

(2.)

int *x, *y;
fun(x, y);

What are the problems with other way or both are same and their behavior will also be the same?


Solution

  • Usually you use the former if you want the variables on the stack, and the latter if you allocate memory for them on the heap. The stack is typically faster, whereas the heap is typically bigger. So in general, you use the first pattern quite often, and only use the latter when dealing with giant arrays or very large objects.