I am always confused by static variables
In the global realm, static means that when linked, it will not be visible to other files.
How is memory allocated for a, b and c?
All of them will live in an executable file (for example, the __DATA segment), which will be displayed in RAM at runtime. If the compiler is good, b and c will live in a read-only data area (for example, the __TEXT segment) or even be eliminated in optimization.
What is the difference (in terms of memory) if I call foo (a), foo (b) and foo (c)?
foo(b) and foo(c) will be a compiler error, since const int& cannot be converted to int& .
Otherwise there is no difference. Passing by reference is equivalent to passing a pointer in the sense of the CPU. Therefore, the address of each memory is taken and foo is called.
source share