C philosophy: “the abstract memory machine.”

  • C is an imperative language that is typically compiled and requires manual memory management.
  • We can think of stack and heap memory as an array.
  • We access this memory using pointers.
  • We use malloc and free to help us manage memory.

Asking for memory: arrays

int* arr;
arr = malloc(sizeof(int) * 5);
arr[i] = 5;

Asking for memory: structs

struct pair* pp = malloc(sizeof(struct pair));
(*pp).first = 2;
pp->second = 3;

GDB: The GNU Project Debugger

Valgrind: a memory profiling tool

GCC

-Wall: Shows all warnings.

-pg: Compile with profiling information.

Makefile

CC=gcc
CFLAGS=-Wall
main: main.o hello_fn.o
clean:
 rm -f main main.o hello_fn.o

gprof

Data Structure Visualizations