dimanche 17 décembre 2017

Same Memory Address After Initialization

I was messing around trying to create random numbers without rand() (just cuz I like to try stuff). Using an online compiler, this works great:

#include <iostream>

int randNum()
{
    unsigned int x;
    size_t y = reinterpret_cast<size_t>(&x);
    return ((y >> 16) & 0x0000FFFF);
}

int main()
{
    unsigned int x = randNum();
    std::cout << x;
    return 0;
}

Compiling it locally on my computer, though...not so much. Every time I compile and run this, it assigns x to the same memory address.

I can understand why the online compiler would give me a new memory address every time, but why not on my local machine?




Aucun commentaire:

Enregistrer un commentaire