Jayesh
Jayesh

Reputation: 4893

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.

Upvotes: 3

Views: 2044

Answers (6)

Ajay Kumar Yadav
Ajay Kumar Yadav

Reputation: 1

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.

Upvotes: 0

M.Tamil
M.Tamil

Reputation: 74

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.

Upvotes: 2

Massimiliano Janes
Massimiliano Janes

Reputation: 5624

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

Upvotes: 1

Paul
Paul

Reputation: 442

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.

Upvotes: 3

Ragib Ahsan
Ragib Ahsan

Reputation: 57

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.

Upvotes: 2

Quentin
Quentin

Reputation: 63114

Because their lifetimes don't overlap, so the compiler is able to reuse their storage.

Upvotes: 13

Related Questions