Structures and dynamic memory

Grouping related values, naming constants, asking for memory at run time, and building data structures from nodes.

About 14 hours of study

Learning objectives

Lessons

  1. 1

    Structures Intermediate

    Group related values with struct and typedef, arrays of structs, and the -> operator.

  2. 2

    Constants, enums and macros Intermediate

    Give values names with const, enum and #define, write safe macros, and understand what the preprocessor does before compiling.

  3. 3

    Dynamic memory: malloc and free Intermediate

    Ask for memory at run time on the heap, give it back with free, and avoid leaks and dangling pointers.

  4. 4

    Linked lists Intermediate

    Nodes on the heap joined by next pointers: build, walk, insert at the front, and free.

Module practice

Module quiz

1. p points to a struct with field x. Which expression reads x?
2. Why must #define SQUARE(x) use brackets around x?
3. Which call is right for an array of n ints?
4. What is a memory leak?
5. When freeing a linked list, why save next before calling free?
6. What does Point b = a; do when a is a struct?

‹ Functions and pointersFiles and reliable programs ›