dimanche 15 août 2021

Pointer always shows the same value C++

This ain't any problem but, I have a C++ program and I want to print the memory address of a variable (the pointer) but it always shows the same values.

Here's a snippet of my code

#include <iostream>

int main() {
        int a = 90; // any random value returns the exact same pointer
        int* b = &a;
        std::cout << b++ << std::endl;
        // returns only one and the same value even on recompilation, which is 0x61ff08 neither does setprecision do any thing
        std::cout << *b << std::endl; 
       /* trying to get some random values from memory but it also shows the same value which is 6422284 */
}

Tried to recompile and execute again but it gave the same output (yes even on recompilation).

The fact is when I tried this on my linux virtual machine, it always gave different and random values on each execution.
I'm on Win10 in my real machine, used g++ as compiler without any optimization flags and am fairly new to C++ programming.

Just wanted to know if this is a Windows specific thing or it was due to the virtual machine that it showed different values and pointers always have same value? And If this should return a random value then why is it not working on Win10.

Aucun commentaire:

Enregistrer un commentaire