Why is the memory address the same for these two variables? [duplicate]

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: %pn", ptr);
    printf("value of x: %dn", *ptr);
}

void foo2(int yval)
{
    int y;
    y = yval;
    int *ptr;
    ptr = &y;
    printf("address of y: %pn", ptr);
    printf("value of y: %dn", *ptr);
}

int main()
{
    foo1(7);
    foo2(11);
    return 0;
}

3

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.

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: %pn", ptr);
    printf("value of x: %dn", *ptr);
}

void foo2(int yval)
{
    int y;
    y = yval;
    int *ptr;
    ptr = &y;
    printf("address of y: %pn", ptr);
    printf("value of y: %dn", *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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật