Skip to main content

Memory Management

Stack Allocation

int x = 42
Point p
p.x = 10
p.y = 20

Heap Allocation

int* ptr = new int
*ptr = 42

int[] arr = new int[1000]
arr[0] = 1
arr[999] = 1000

Pointer Operations

int x = 42
int* ptr = &x # Address-of
print(*ptr) # Dereference: 42
*ptr = 99 # Modify through pointer
print(x) # 99

Pointer Arithmetic

int[] arr = new int[10]
int* first = arr
int* third = first + 2 # Points to arr[2]

Pointer-to-Pointer

int x = 42
int* ptr = &x
int** pptr = &ptr

print(**pptr) # 42

64-bit Pointers

int64* large_ptr = new int64
*large_ptr = 9223372036854775807

uint64* unsigned_ptr = new uint64
*unsigned_ptr = 0xFFFFFFFFFFFFFFFF

Safety

  • Always initialize pointers before use
  • Check for null before dereferencing
  • Free heap memory when done
  • Avoid buffer overflows