C: pointers and memory management
You use pointers and dynamic memory correctly and find and prevent errors such as overruns and memory leaks using tooling.
Level 201 · 22 lessons · 21 practice labs
Lessons
Pointers
- The memory model: stack, heap and addresses: You see how a program’s memory is divided and print variable addresses to observe it.
- Pointer basics: the & and * operators: You take addresses, reach values through pointers and learn why NULL pointers must be checked.
- Pointers and arrays: address arithmetic: You grasp the relationship between arrays and pointers and the overrun risk of advancing a pointer.
- Passing addresses to functions and const correctness: You let a function modify a variable by passing its address and prevent unwanted changes with const.
- Pointers to pointers and argv
- void pointers and general-purpose functions
- Function pointers and callbacks: You pass a function to another function as a parameter and use it with library calls such as qsort.
- Short task: a table sortable through a function pointer
Dynamic memory
- malloc and free: requesting and returning memory: You allocate memory at run time, check the return value and give it back with free when done.
- Growing data with calloc and realloc: You allocate zeroed memory, grow arrays and avoid data loss when realloc fails.
- Ownership rules and memory leaks: You prevent leaks by following the principle that every allocation has exactly one owner.
- Dangling pointers and double free: causes and protection: You understand why touching freed memory is dangerous and build the habit of setting pointers to NULL after free.
- A linked list: a data structure built on pointers: You write a list that chains nodes in dynamic memory, adding, removing and freeing all of them.
- Short task: a leak-free growing line buffer
Secure C and tooling
- Buffer bounds: snprintf instead of strcpy and strncpy traps: You understand why unbounded copy functions lead to overruns and use bounds-aware functions correctly.
- Reading input safely: fgets and length checks: You read user input into a fixed buffer with fgets and handle the line ending and excess data.
- Integer overflow and sign errors: You see how overflow in size calculations leads to wrong allocations and add checks before calculating.
- Format string safety: never passing user data as the format: You understand why the format string of the printf family must stay constant and use the safe call form.
- Finding memory errors: Valgrind and AddressSanitizer: You run programs under Valgrind and -fsanitize=address and read the reports on leaks, overruns and invalid access.
- A secure C coding checklist (CERT C): You review your linked list and input reader against CERT C guidance and get them running clean under the sanitizers.
- Short task: find and fix three memory bugs
Capstone
- Capstone: a leak-free, bounds-checked record book
Practice labs
- Returning min and max through pointers: Write a function that returns an array's minimum and maximum through output pointers.
- Adding const correctness to an API: Add const to functions that should not modify their inputs and let the compiler catch the one that secretly does.
- Walking a buffer with pointer arithmetic: Compute a byte buffer's checksum with begin and end pointers instead of indexes, working correctly on an empty buffer.
- Parsing options from argv: Walk char **argv to parse -v and -o file options and print a usage message when a value is missing.
- Writing a comparison function: Write a qsort comparison function that sorts employee records by salary in descending order.
- Routing commands with a function pointer table: Turn a long if-else chain into a table of command names and function pointers, returning an error for unknown commands.
- A generic search function with void*: Write a generic find function taking an element size and an equality callback, tested with int and struct arrays.
- Creating a copy of a string on the heap: Write a function that copies a string with malloc, handling malloc failure and documenting who must free it.
- Using realloc without losing memory: Fix code that assigns realloc's result straight to the same pointer so the old block does not leak on failure.
- Finding leaks in a report generator: Use Valgrind output to find three leaks in a report generator and make sure free is also called on error paths.
- Removing a double free in a cache: Fix the double free in a cache's delete and clear paths by setting an ownership rule so ASan reports clean.
- Inserting into a sorted linked list: Write a linked-list function that inserts a new patient into a waiting list in priority order.
- Removing nodes that match a condition: Write a function that removes expired sessions from a linked list using a pointer-to-pointer, including the head node.
- Reversing a list and freeing it without leaks: Write code that reverses a linked list in place and then frees it completely, with Valgrind showing zero leaks.
- Building a file path with snprintf: Replace strcpy and strcat path joining with snprintf and return an error when the result would be truncated.
- Reading long lines safely with fgets: In code that reads a user name with fgets, strip the newline and reject input longer than the limit.
- Preventing overflow in a size multiplication: Add an overflow check to a function that allocates count * size so huge counts cannot produce a tiny buffer.
- A format string bug in a log function: Fix a log function that passes user messages to printf as the format and test it with input containing %x.
- Repairing a signed length comparison: Fix a copy function whose bounds check is bypassed by a negative length using size_t and explicit checks.
- A generic stack with void*: Write a stack that can hold any type and manages element size itself, passing the push and pop tests.
- Counting a NULL-terminated string array: Write a function that returns the element count and the longest entry of a NULL-terminated string array given as char **.
About this course
You use pointers and dynamic memory correctly and find and prevent errors such as overruns and memory leaks using tooling.
What you will learn
- Pointers
- Dynamic memory
- Secure C and tooling
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.