mardi 8 septembre 2020

Why variables' address on stack are not sequential

(I do this test on Windows Visual Studio 2019 Version16.7)

    const char a = 'a';
    const char b = 'b';
    const char c = 'a';

    intptr_t ptr_a = (intptr_t)&a;
    intptr_t ptr_b = (intptr_t)&b;
    intptr_t ptr_c = (intptr_t)&c;

    std::cout << ptr_b - ptr_a << "  " << ptr_c - ptr_b << std::endl;

if I run this piece of code under Debug, the result is: "32 32"

if I run this piece of code under release, the reuslt is: "-1 2"

So, the question is: sizeof(char) is 1, but why we get "32 32", the address on stack should be continuous,right? And, under release mode, why ptr_b is less than ptr_a?

Aucun commentaire:

Enregistrer un commentaire