I am quite eager to learn how C++ or any programming language determines the memory addresses to use for its variables and instructions. Are these hexadecimal values completely random or are they calculated? I do know that instructions are stored in memory contiguously and that there is a program counter that increments over it to provide the address to instructions. But why are the variables stored randomly, why not just store them linearly?
-
5They aren't stored randomly, though the specifics are implementation details that vary between compilers and platforms. – François Andrieux Feb 27 '18 at 17:24
-
2FWIW, Operating systems randomize the process memory: https://en.wikipedia.org/wiki/Address_space_layout_randomization – NathanOliver Feb 27 '18 at 17:26
-
stack variables vs. heap variable addresses may also appear quite different. Again, this is based on how a platform/compiler implements their stacks vs. their heaps. – franji1 Feb 27 '18 at 17:26
-
2Please show your program and its output, and ask a specific question about that specific output. – n. m. could be an AI Feb 27 '18 at 18:04
-
Possible duplicate of [How is heap and stack memories managed, implemented, allocated?](https://stackoverflow.com/questions/1212797/how-is-heap-and-stack-memories-managed-implemented-allocated) – Raedwald Feb 27 '18 at 18:41
-
@n.m. The questioner is not asking why a particular program operates in a particular way, so asking for program text is wrong. This is a perfectly good (although a duplicate) question about the runtime environment of program's. – Raedwald Feb 27 '18 at 18:44
-
@Raedwald *The questioner is not asking why a particular program operates in a particular way* too bad, that's exactly what they should have asked. Understanding a specific program is a necessary step to understanding the general behaviour. – n. m. could be an AI Feb 27 '18 at 21:54
2 Answers
The location of variables is at the discretion of the compiler relative to your program.
The compiler is not required to allocate memory for variables. Variables may be placed in registers.
If the compiler is using a stack, the Operating System may pass a pointer to the stack memory to the executable. Although the compiler may be ordering the location of the variables, their exact addresses would be determined by the Operating System, if one is present. In many embedded systems, variables can be placed in segments and the segments placed at fixed locations by the compiler.
For dynamic memory, most implementations call an Operating System function to allocate the memory. Embedded systems may be different and have a fixed location for the dynamic memory.
There are many attributes that affect the location of variables: task placement, memory availability, and virtual memory, to name a few. Many operating systems will allocate memory to a task to use and all variables and code reside in this area. Some OS may swap out memory to another device, such as a hard drive, (also known as paging or virtual memory).
The OS usually determines the starting location of your program's variables (and memory space). The actual location has many determining factors, such as other tasks running (using memory), system memory capacity, virtual memory and the executable code size. The compiler may order variables within the program's memory area.

- 56,849
- 17
- 98
- 154
-
There's also address space layout randomization on some OSes, which explicitly randomizes certain addresses to help make it harder for malicious clients to exploit vulnerabilities. – Daniel Schepler Feb 27 '18 at 18:12
When you call new
or malloc
it uses system-dependent function that returns mapped memory page (AFAIK usually 4K size). System takes care to map memory pages and memory management "algorithm" (such as new
or malloc
) makes sure to properly distribute addresses. When you use whole page, it asks OS for another one, etc.
AFAIK, OS functions are: VirtualAlloc()
on Windows and sbrk()
on unix systems.

- 1,120
- 8
- 16
-
1It would mostly be `mmap` on recent Unix. `sbrk` is almost obsolete (and could certainly be avoided: it is possible to implement `malloc` with `mmap` and without `sbrk`) – Basile Starynkevitch Feb 27 '18 at 18:14