CS50-Memory

tan21098
2 min readSep 13, 2020

--

Heap is where malloc gets bytes from.

Stack is where local variables(call function) go (bottom to top) — When we call a function, we give a slice memory( stack frame).

Buffer Overflows: This design might cause a crash if we malloc too many memories and call too many functions.

Heap Overflow: e.g. too much malloc

Stack Overflow: e.g. recursion without converging to the base case

Suppose we want to swap values of x and y

Swap by value does not work:

Main:

x = 1, y = 2

Swap function:

a = 1, b = 2

to

a = 2, b = 1

Swap function stack is freed after it return

Swap by pointer does work

Even if Swap function is freed,

The values of x, y have been swaped

--

--

No responses yet