Computer Memory
Modified 2022-09-23
Computer Memory is hardware used for storing information.
Memory is slow
The memory is about 9-10 cm away from the CPU, which implies that the photon is not able to go to the memory and back to the CPU within 1 CPU cycle.
Making memory faster
- Reduce CPU latency by providing a small cache on the CPU itself
- Increase bandwidth to send as much data through the wires as possible per second
Stack
The stack is memory managed by the compiler or the OS. Everytime a function gets invoked a new stack is created iwth it's own environment (variables, arguments, ...).
Open Debug > Memory > Memory 1 in Visual Studio to see that the new stack is created close to the memory location of the previous stack.
Heap
The CPU has a table which secretly maps the virtual memory of the programs to the actual physical memory (for modern systems). The virtual memory prevents different programs from accessing and modifying the same piece of memory.
Pointers
The * (star) in front of a variable tells the compiler to free some memory to
store the memory location of another variable, effectivly it declares a
pointer. The size of the address is the same as an int (for x64). The address
can be initialized at the same time of the declaration or later.
The & (ampersand) in front of a variable is the address operator which gives
back the address of the variable stored in memory.
int main()
{
int a = 10; // variable declaration
int *b; // pointer variable declaration
b = &a; // store address of "a" in pointer "b"
return *b // return the value of "a" stored in pointer "b"
}