lundi 31 juillet 2017

Constant casting in C++ gives 2 different values for the same address [duplicate]

int main()
{
    const int  i = 9;
    const int* p = &i;

    int *c = const_cast<int*>(p);
    *c = 3;
    cout<<"i = "<<i<<endl;
    cout<<"p = "<<p<<endl;
    cout<<"*p = "<<*p<<endl;
    cout<<"c = "<<c<<endl;
    cout<<"*c = "<<*c<<endl;
    cout<<"&i = "<<&i<<endl;


    return 0;
}

Hello all, the previously stated code has an output that doesn't make any sense to me, the output looks like the following C++ code output. My question is, how can you have I have 2 different values in the same memory address? Is it possible? Is there any other case that this might happen in C++? and Can the opposite happen such that compiler moves a value from a specific place in memory and assign it to different place?

Aucun commentaire:

Enregistrer un commentaire