0

What's the difference between:

int i = 0;
foo(i);

int foo(int i){
...
}

and:

int i = 0;
foo(&i);

int foo(int *iPtr){
...
}

I mean, is passing by reference faster and better for memory/performance? Why should I use reference?

Also, my professor said that when we pass by value we are actually passing for reference.. so what's the real difference between them?

I thought that pointers and passing by reference were used to optimize performance and memory..

Mnkisd
  • 504
  • 2
  • 12
  • 1
    `foo(&i);` is still expressly passing by value: the value of the pointer. – chux - Reinstate Monica May 22 '20 at 17:07
  • It is well explained in a previous question. – Weather Vane May 22 '20 at 17:08
  • 2
    There is a *functional* difference between them, which is a more important reason to use one over the other. – Jongware May 22 '20 at 17:08
  • "... when we pass by value we are actually passing for reference" over-simplifies. – chux - Reinstate Monica May 22 '20 at 17:10
  • @usr2564301 so when should I use one and when should I use the other? – Mnkisd May 22 '20 at 17:14
  • The given reference is actually a pretty bad example. Most of the answers are too long for this topic. Look at this answer https://stackoverflow.com/a/36208432/1741542, if you don't find any other examples on the net. – Olaf Dietsche May 22 '20 at 17:15
  • In some cases you *have to* pass a pointer, for example when processing a string. Otherwise, as a generality, you pass a pointer when you want the function to modify something outside of its scope. Otherwise you are passing a *copy* of a variable. – Weather Vane May 22 '20 at 17:17
  • @WeatherVane so there's not any improvements in performance/memory? – Mnkisd May 22 '20 at 17:18
  • 1
    Don't pass a pointer unless you *need* to pass a pointer. To access the data, the processor must first load the pointer and then load what it is pointing to. It is called *indirection*. But it's a frequent newb mistake to modify the value passed and think it modifies the *original* value, of which this is just a copy, and gets forgotten on function return. – Weather Vane May 22 '20 at 17:20
  • Maybe this one [References - cppreferences.com](https://en.cppreference.com/book/intro/reference) helps. – Olaf Dietsche May 22 '20 at 17:23
  • You would use the appropriate form for whatever your purpose is. So, it depends. I am assuming you know what the difference is between these two – do you? – Jongware May 22 '20 at 17:24
  • @OlafDietsche I agree the duplicate could be overload, but it has an almost identical title and goes thoroughly into the topic - TL;DR! – Weather Vane May 22 '20 at 17:25
  • @WeatherVane Actually, I looked into this question and its answers. This is why I suggested the fourth (top-voted) answer as the least bad answer. The very first sentence (and its footnote) of the accepted answer is simply wrong. In so far, I won't call it thorough, but rather excessive, and bad. – Olaf Dietsche May 23 '20 at 09:30
  • I must agree, the title of this question is badly chosen. The OP seems to know the difference between call by value and call by reference. And there seems to be confusion about what the professor said or maybe worded ambiguously. – Olaf Dietsche May 23 '20 at 09:38
  • @Mnkisd Maybe this answer [Pass by value faster than pass by reference](https://stackoverflow.com/a/22841752/1741542) comes closer to your question. – Olaf Dietsche May 23 '20 at 09:58
  • @OlafDietsche thanks you, you really helped me! – Mnkisd May 23 '20 at 11:29

0 Answers0