1

Suppose there is a one cpp file with global functions.

As far as i know instruction set is copy to memory when I run the program and stay there until program ends. Each time I called a function, virtual adresses of variables are same but I can't understand it allocates memory first and do not free until program ends or it allocates memory for each time I call function.

EDIT : I call functions in other functions than main, adresses are changed. So question is wrong. Sorry.

  • 2
    "Each time I called a function, virtual adresses of variables are same" - what do you mean by this? What variables? Do you mean variables local to the function? What makes you think they are always at the same address every time you call the function? – davmac Oct 29 '15 at 17:16
  • Yes I mean local variables of function. I wrote a function which prints adresses of local variables and I called it 3 times in a long code, about 600 line (start of main, middle of main and end of main.) It prints same adresses. –  Oct 29 '15 at 17:18
  • 1
    @thoron Well, if the functions are called from within the same scope, stack variable addresses should be the same. You should consider to clarify your question by adding a small code sample, that demonstrates what you're asking about. E.g. `virtual` is usually used for something completely different, as you meant to say. – πάντα ῥεῖ Oct 29 '15 at 17:20
  • @πάνταῥεῖ but understanding why that is the case would require understanding stack-based allocation, which OP clearly doesn't. – davmac Oct 29 '15 at 17:22
  • @davmac Sure, but the question definitely needs improvement. – πάντα ῥεῖ Oct 29 '15 at 17:24
  • This is somewhat vague. What is your specific question? Is it regarding the difference between a virtual address and a physical address? – Jason Oct 29 '15 at 17:36

2 Answers2

2

Local variables usually end up on something called a stack that is statically allocated when the program (or thread) starts. If you always call the function with the same call stack, local variables will tend to end up in the same place in the stack, and thus have the same virtual addresses assigned for different invocations.

Note that technically none of this behavior is specified in the or language specs, it's just the way most modern platforms end up implementing things these days.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43
2

In typical implementations of C and C++ storage for local variables is allocated on the stack (see: Stack-based memory allocation) when the function is entered, and cleared from the stack when the function returns.

That you have seen a function use the same address for its variables on different calls is actually a coincedence. Try nesting your function calls within other calls and see what happens:

#include <stdio.h>

int funcTwo()
{
    int nn = 5;
    printf("funcTwo, &nn = %p\n", &nn);
}

int funcOne(int n)
{
    int nn = n;
    printf("funcOne, &nn = %p\n", &nn);
    funcTwo();
}

int main(int argc, char **argv)
{
    funcOne(5);
    funcTwo();
    return 0;
}

Sample output:

funcOne, &nn = 0x7fff96726b7c
funcTwo, &nn = 0x7fff96726b4c
funcTwo, &nn = 0x7fff96726b7c

Observe that &nn is slightly different inside the two calls to funcTwo. That's because one instance is called from inside funcOne, which has its own local variable on the stack (as well as its parameter, return address and any additional space used to save processor register values for example). The additional stack usage pushes the address of variables in funcTwo further up the stack (to a lower address, since the stack grows downwards on this architecture).

davmac
  • 20,150
  • 1
  • 40
  • 68