Home > Software design >  Why are the addresses of two variables in separate functions stored at the same address?
Why are the addresses of two variables in separate functions stored at the same address?

Time:11-04

I have defined two functions foo1, foo2, and foo3 each function receives one argument (xval),(yval) and I also declared only one variable in each function (x),(y). why when printing x address, and y address. I find that the address is constant. And the same reason is given with xval, yval.

Here is the code:

#include <stdio.h>

void foo1(int xval) {
  int x;
  x = xval;
  printf("X Address == 0x%x, Value of x == %d\n", &xval, x);
}

void foo2(int dummy) {
  int y = dummy;
  printf("Y Address == 0x%x, Value of y == %d\n", &dummy, y);
}

int main() {
  foo1(7);
  foo2(11);

  return 0;
}

CodePudding user response:

In foo1 and foo2 you are printing out the addresses of local variables (xval and dummy respectively) that are placed on the stack.

If I remember correctly, the C specification does not require these local variables to have different addresses. This should not concern you either.

The address of those variables has meaning only within the function in which the variable is defined, so comparing the addresses accross two different functions is meaningless.

What technically happens under the hood is, the compiler has placed 7 on the stack in your main() function before calling foo1(). Then foo1() prints out the address of that variable in the stack.

When foo1() is finished, the variable is popped from the stack and then 11 is placed on the stack before calling foo2(). This accidentally has used the same memory address but you should not count on this. For example, if you call foo2() from within foo1() this would affect the stack and the address would be different. Try the following:

#include <stdio.h>
extern void foo2(int dummy);
void foo1(int xval)
{
    int x;
    x = xval;
    printf("X Address == 0x%x, Value of x == %d\n", &xval, x);
    
    foo2(11);
}
void foo2(int dummy)
{
    int y = dummy;
    printf("Y Address == 0x%x, Value of y == %d\n", &dummy, y);
}

int main()
{

    foo1(7);

    return 0;
}

In summary, do not try to analyze what the concrete address value is. Simply use the address without looking into it. This is platform and compiler dependent.

CodePudding user response:

xval and dummy are temporary variables. ("automatic variables" is the C term for them.) They're only guaranteed to exist while the function is executing. So there's no reason they couldn't be at the same address.

On your standard home computer, they would be found on the stack. Given they are both the first argument to the function, and given that the functions are called in sequence, I would be surprised if they didn't have the same address.

In fact, if you didn't take the address of the parameter, it's entirely possible for the argument to be passed to the function using a register so that no memory would be used at all.

  • Related