简体   繁体   中英

Local variables hold the same memory addresses

Following program prints the address of two different variables.

#include <iostream>
using namespace std;

void f1()
{
    int i,k;
    cout<<"f1 : "<<&i<<endl;
}

void f2()
{
    int j;
    cout<<"f2 : "<<&j<<endl;
}

int main() {
    f1();
    f2();
    return 0;
}

Live demo

When I run this program on GCC, I wondered, both variables have a same addresses.

Why both variable hold the addresses same?

Edit:

void f1()
{
    int i = 10;
    cout<<"f1 : "<<&i<<endl;
    cout<<"value of  : "<<i<<endl;
}

void f2()
{
    int j;
    cout<<"f2 : "<<&j<<endl;
    cout<<"value of j : "<<j<<endl;
}

int main() {
    f1();
    f2();
    return 0;
}

Live Demo 2

In this example, if I assigned value 10 to i then j also print 10 . I think it is not valid because j is uninitialized.

因为它们的生命周期不重叠,所以编译器能够重用它们的存储。

They are using the same stack memory block. After f1 call gets returned the stack memory is free. Then f2 gets the same stack memory again. So, if you call f2 inside another function f3 then you're likely to see a different address.

Each invocation of functions needs its own place to store the variable. But as soon as the function returns, the variable no longer exists. There's no reason the address can't be re-used. It doesn't have to be, but there's no reason it can't be.

The stack space is used to hold the information needed to return from a function and their local variables when a function is invoked. When the function returns, the local variables are removed from the stack and the return information removed from the stack, leaving the stack back where it was when the function was called. Since the two function invocations are similar they wind up with the stack the same in both cases, making the local variable have the same address.

In that example, if I assigned value 10 to i then j also print 10. I think it is not valid.

In this example you didn't use any memory allocation concepts of c++. So, the value you just stored in i will remain as its never been removed from that memory which is allocated for i, then when you calling fun2 the same memory is being allocated for j . That's why you got same value for both variables and also same address.

In that example, if I assigned value 10 to i then j also print 10. I think it is not valid.

regarding your second example (please, post question-related code in the question, do not link'em) :

void f2()
{
    int j;
    cout<<"f2 : "<<&j<<endl;
    cout<<"value of j : "<<j<<endl;
}

this is undefined behaviour , j has an indeterminate value and any(*) evaluation resulting in an indeterminate value ( the one occurring at cout<<j call ) gives undefined behaviour.

Practically, this includes having an arbitrary value, trap representations ( on platforms having them, admittedly not many nowadays as far as I know :) ) and worstly allowing the compiler to optimize as if j had literally any value ( resulting in possibly logic-defying behaviours ).

(*) there are exceptions, but not relevant in this case

You should know the working of the local and global variables here in different functions the variables are local.

Every time you call the function it will assign value and memory address to it because it is stored in stack memory and get cleaned up with the function ending. Memory get released and same memory can be used again n again.

But may be your compiler got mad. Put the full source code then figure it out carefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM