0
void ShowValue(int **value)
{
    std::cout<<"Value 0 = "<<**value<<"\t"<<*value<<"\t"<<value<<"\t"<<&value<<"\n";
}
void ShowValue(int *value)
{
    std::cout<<"Value 1 = "<<*value<<"\t"<<value<<"\t"<<&value<<"\n";
}
void ShowValue(int &value)
{
    std::cout<<"Value 2 = "<<value<<"\t"<<&value<<"\n";
}
void ShowValues(int value)
{
    std::cout<<"Value 3 = "<<value<<"\t"<<&value<<"\n";
}

int main()
{
    int *vl = new int(428);
    int vl1=420;

    std::cout<<*vl<<"\n";
    std::cout<<vl<<"\n";
    std::cout<<&vl<<"\n\n";

    std::cout<<vl1<<"\n";
    std::cout<<&vl1<<"\n\n";

    ShowValue(&vl);
    ShowValue(vl);
    ShowValue(*vl);
    ShowValues(*vl);

    std::cout<<"\n";

    ShowValue(&vl1);
    ShowValue(vl1);
    ShowValues(vl1);

    return 0;
}

Output:

428
0x100200060
0x7fff5fbff860

420
0x7fff5fbff85c

Value 0 = 428   0x100200060 0x7fff5fbff860  0x7fff5fbff808
Value 1 = 428   0x100200060 0x7fff5fbff808
Value 2 = 428   0x100200060
Value 3 = 428   0x7fff5fbff80c

Value 1 = 420   0x7fff5fbff85c  0x7fff5fbff808
Value 2 = 420   0x7fff5fbff85c
Value 3 = 420   0x7fff5fbff80c
bolov
  • 72,283
  • 15
  • 145
  • 224
  • 5
    `&value` is the address of something local to the function. Knowing why it happens to be the same address across calls is meaningless. – chris Aug 09 '14 at 17:40

1 Answers1

3

The both functions have local variables that corresponds to their parameters.

void ShowValue(int **value);
void ShowValue(int *value);

here are two local variables one has type int ** and other has type int * but the both have the same size.

As the both functions allocate their parameters in the stack and use the same stack then the addresses of these local variables are the same.

That it would be more clear let's consider figures (assuming that the size of a pointer is equal to 4)

Before a function call           Stack
                              =============   <= 0x7fff5fbff80C
                             |             |  <= 0x7fff5fbff808

calling function                 Stack
void ShowValue(int **value); =============   <= 0x7fff5fbff80C
                             |   value    |  <= 0x7fff5fbff808


after the call of                Stack
void ShowValue(int **value);  =============   <= 0x7fff5fbff80C
                             |             |  <= 0x7fff5fbff808

calling function                 Stack
void ShowValue(int *value);  =============   <= 0x7fff5fbff80C
                             |   value    |  <= 0x7fff5fbff808


after the call of                Stack
void ShowValue(int *value);   =============   <= 0x7fff5fbff80C
                             |             |  <= 0x7fff5fbff808
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335