Pointers and manual memory management
โ Report an issue with this lessonint value = 42;
int *ptr = &value;
printf("%d\n", *ptr); // 42, dereferencing the pointer
int *numbers = malloc(5 * sizeof(int));
numbers[0] = 10;
free(numbers); // you are responsible for freeing what you allocate
Every malloc needs exactly one matching free.
Forget it, and you leak memory; free it twice, or use it after freeing,
and you get undefined behavior โ this is the exact category of bug that
languages like Rust were designed to eliminate at compile time.
Try it yourself
Exercise: Use a pointer to double the value of x, then print x.
Expected output:
42
Run your code and get it working before marking this lesson complete.
// that was the last free lesson
8 more lessons โ including Capstone project: a linked list library โ plus a certificate are waiting.
Unlock the full course โ $149.99