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;
}

出力:

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
4

1 に答える 1

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
于 2014-08-09T17:52:00.930 に答える