TRT
Sign in

Foundations · Module 3

Arrays, Strings and Pointers

Store many values in arrays, vectors and strings, use pointers to work with addresses directly, and learn where the stack ends and the heap begins.

Start the moduleFree10 lessons · about 82 minutes

Real programs hold collections: scores, names, lines of text. This module shows how arrays, vectors and strings lay their elements out in memory, how pointers give direct access to addresses, and how the heap holds data that outlives the function that created it. It also covers the mistakes that live here, such as out-of-bounds access, leaks and dangling pointers, so that you can recognize and avoid them.

  1. 1Arrays: Contiguous Elements and IndexingStore a fixed number of values side by side in memory, see how an index becomes an address calculation, and learn why arrays start at zero, cannot grow and know nothing about their own size.8 min
  2. 2Out-of-Bounds Access and Undefined BehaviorSee what happens in memory when an index leaves its array, learn what undefined behavior means and why compilers may exploit it, and use warnings and sanitizers to catch these bugs.8 min
  3. 3std::array and std::vectorReplace raw arrays with containers that know their size, copy as values and offer checked access: std::array as a zero-overhead wrapper, and std::vector as a small object that owns a contiguous heap buffer.8 min
  4. 4How a Vector Grows: Size, Capacity and ReallocationWatch push_back fill a buffer, reallocate and double its capacity, see why that makes appending cheap on average, and learn which pointers and references a growing vector invalidates.8 min
  5. 5Strings: C-Strings and std::stringSee how text is stored as numbers in memory, how a C-string marks its end with a zero byte, and how std::string and std::string_view take over the length, memory and safety work.9 min
  6. 6Pointers: Variables That Hold AddressesTreat an address as a value: take it with the ampersand, store it in a pointer, follow it with the star, and see what that costs, why it makes data structures possible, and how uninitialized or null pointers crash programs.8 min
  7. 7Pointer Arithmetic and the Link to ArraysSee why adding one to a pointer moves it by a whole element, how a[i] is defined as pointer arithmetic, why arrays decay into pointers, and what the one-past-the-end rule means for loops and iterators.8 min
  8. 8Pointers Versus References, and nullptrCompare references and pointers by what assignment does to them and by which of them can be null or re-aimed, and see how nullptr and null checks let a pointer say that there is no object.8 min
  9. 9The Heap: new, delete and Memory LeaksLearn where objects can live beyond a function call, how new and delete work with the allocator, and how leaks and mismatched deletes arise, so that you see why modern C++ lets containers and smart pointers do the work.9 min
  10. 10Dangling Pointers and Use-After-FreeSee how a pointer can outlive its object, how a freed block gets reused so that a stale pointer corrupts unrelated data, and which ownership designs and tools prevent or catch these bugs.8 min