-1

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 */
       // EDIT: I purposely made it like this to get some random value from the memory whenever it is executed
}

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.

Someone
  • 126
  • 8
  • 4
    This is an Undefined behavior and it depends on the OS Kernel how to allocate memory for your process. Maybe your system memory is not busy. – Ghasem Ramezani Aug 15 '21 at 15:50
  • 1
    `*b` invokes undefined behavior because `b` isn't pointing to valid memory – phuclv Aug 15 '21 at 16:09
  • "And If this should return a random value then why is it not working on Win10." Technically this is no C++ question: C++ has absolutely no idea how the underlying memory looks like, if it's randomized, ... – dtell Aug 15 '21 at 23:23

1 Answers1

2

This is dependent on the kernel. The exact thing is Address space layout randomisation (ASLR), and is used to stop a buffer overflow attack. I do not know the specifics of Win10, but linux generally has ASLR enabled by default

In a very basic overview, what happens is that when the program is loaded it will have several sections. .text where code is, sections for dynamic libraries, the heap and stack (there are others as well). Now these were originally placed in the same place in the virtual address space in memory (i.e. where pointers in your program point). This allowed people to potentially overflow buffers, and trigger possibly malicious side effects. ASLR randomises where exactly things are in loaded into memory, so it is much harder to trigger those malicious side-effects.

Apart from ASLR however, there are other safeguards (compiler warns of dangerous function calls like gets, stack cookies, non-executable stack etc.), so missing ASLR shouldn't be an issue. This suggests that Windows only enables ASLR for applications explicitly compiled that way, but wikipedia can be a bit unreliable and this looks like quite old information.

Lala5th
  • 1,137
  • 7
  • 18