2

when i run this program the memory address of variables x and y are the same.

my best guess would be that since the variables exist in functions that are called on they only exist when theyre called on so they can use the same memory location. does this make sense?

#include <stdio.h>

void foo1(int);
void foo2(int);

void foo1(int xval)
{
    int x;
    x = xval;
    int *ptr;
    ptr = &x;
    printf("address of x: %p\n", ptr);
    printf("value of x: %d\n", *ptr);
}

void foo2(int yval)
{
    int y;
    y = yval;
    int *ptr;
    ptr = &y;
    printf("address of y: %p\n", ptr);
    printf("value of y: %d\n", *ptr);
}

int main()
{
    foo1(7);
    foo2(11);
    return 0;
}
New contributor
soobinfan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4

2 Answers 2

0

Storage (memory) for an object declared int x; or int y; inside a block is reserved only during the portion of program execution from when execution of the block starts until it ends. Outside of that time, the storage may be reused for other objects or even other purposes.

So two objects whose storage is reserved at different times during program execution, without any overlap, may use the same storage and may have the same address.

A block includes the function body { … }. (There are often also sub-blocks inside functions, discussed below.)

You may often see people refer to these as “stack“ variables, because a hardware stack is a common way of implementing the variables defined inside a function, but the C standard does not require this, and they may be implemented by other means. The fact that their memory may be reused outside of the reserved time remains true whether they are implemented via a stack or via other means.

Supplement

Blocks include { … } inside a function and if, switch, for, while, and do statements and their immediate substatements.

0

The two functions have the same number and type of arguments and allocates local variables of the same type in the same order on the stack on your particular platform (see x86 calling convention, so the two variables end up in the same relative address. As you call the functions in sequence they end up with the absolute address, too, as a frame is pushed on the stack when the function is called, and popped upon return. To obtain a different address you could for example call foo1() from foo2():

#include <stdio.h>

void foo1(int xval)
{
    int x;
    x = xval;
    int *ptr;
    ptr = &x;
    printf("address of x: %p\n", ptr);
    printf("value of x: %d\n", *ptr);
}

void foo2(int yval)
{
    int y;
    y = yval;
    int *ptr;
    ptr = &y;
    printf("address of y: %p\n", ptr);
    printf("value of y: %d\n", *ptr);
    foo1(7);
}

int main()
{
    foo2(11);
}

and example run:

address of y: 0x7fffd3076f34
value of y: 11
address of x: 0x7fffd3076f04
value of x: 7

Not the answer you're looking for? Browse other questions tagged or ask your own question.