1

I'm new to C++ and I don't know well how the stack and heap work.

Imagine defining a function and passing a variable to that function by value. When a copy of that value is generated, does it get removed from memory after using the function, or it will remain? So when you pass a large object by value, how long it will occupy memory?

jotik
  • 14,982
  • 9
  • 48
  • 106
  • A common rule of thumb is to pass large objects by reference so they don't occupy *stack* memory. – Thomas Matthews Jun 21 '16 at 18:04
  • Variable passing to functions is compiler dependent. Some compilers may choose to pass variables in registers, while other compilers may decide to pass on the stack. I've seen compilers that may use a special memory area (not stack or heap) for passing variables. – Thomas Matthews Jun 21 '16 at 18:06
  • Possible duplicate of [Why should C++ programmers minimize use of 'new'?](http://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – Slava Jun 21 '16 at 18:15

2 Answers2

1

The value of the variable that you pass to the function is copied on the stack, so there is no "direct memory usage", like when you allocate something dinamically on the heap.

After the function finishes, the stack pointer will be modified, so you don't have to care about memory usage of that variable.

Of course some variables like ints and chars may be passed in registers.

Mattia F.
  • 1,590
  • 8
  • 17
0

Passing of variables to functions depends on the signature of the function, the size of the variable, the compiler, the platform and the optimization settings.

Passing of Variables

  • By the language syntax, variables can be passed to functions:
  • By value (or copy)
  • By reference (passing a link to the original variable)
  • By constant reference (same as reference, but the function can't modify the variable)
  • By pointer (passing the address of a variable), including all the const variations.

This is a high level view. In this view, we don't know if the variable is stored "in" memory or not. We don't know if the variable resides in registers. All we know is that we want to pass information to a function.

Implementation

A compiler has a lot of freedom when implementing the passing of variables.

Many compilers may pass values in registers, depending on how many registers are available, the size of the variable and the quantity of variables. In this case, the variable doesn't live in memory.

The compiler may not even "pass" the variable. The compiler may "inline" a function. In this case, the compiler continues emitting code without changing registers.

Some compilers use a stack-based memory area for passing variables. This is convenient because the compiler "pushes" the variables before calling the function and "pops" values after the function executes. Pushing and popping usually involve adjusting the value in a dedicated register; thus and easy and optimal technique.

The compiler may decide not to pass the variable, but pass a pointer to the variable. For example, when passing by reference, the compiler may decide to pass a pointer to the variable (address of the variable). In this case, the contents of the variable are not copied, but accessed directly. For example, if the variable (data) is in read only memory, the compiler may decide to pass a pointer to the data rather than waste time copying the data.

Variable Lifetimes

Variables that are passed by copy have a life time for the duration of execution within the function.

Variables that are passed by reference last until they are destroyed by the caller.

There are forms of variables that can have a longer lifetime. This set includes, but is not limited to, global variables, static variables, and variables allocated from dynamic memory (a.k.a. the heap).

Optimizations

There is a possibility that the compiler will not pass variables. For example, if the function doesn't use a parameter, the compiler may not pass one.

Compilers have been known to eliminate empty functions as an optimization. Thus no variables will be passed.

As stated earlier, when functions are "inlined", the calling overhead (including variable passing) is reduced or eliminated.

As an optimization, the compiler may remove variables or not call functions. For example, if calling a function results in intializing a variable, the compiler may prefer to perform an assignment instead.

So there are a lot of "it depends" for your question.
Hope this broadens your understanding of variable passing.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
  • All of this does not always depend on the compiler, but might also depend on the ABI, which I think should also be mentioned in this answer. – jotik Jun 22 '16 at 11:43