In Lesson 14 of Learn C With Me, we begin Chapter 12 — Manual Memory Allocation from Beej's Guide to C Programming.

This time, we explore how C programs can request memory dynamically, work with that memory through pointers, and release it when it's no longer needed.

We cover:

  • What manual memory allocation means
  • The difference between automatic and dynamically allocated storage
  • Introducing malloc()
  • Allocating memory for integers and arrays
  • Using sizeof to calculate allocation sizes
  • Why malloc() returns a pointer
  • Checking allocation failures against NULL
  • Accessing dynamically allocated memory through pointers
  • Using array notation with allocated memory
  • Understanding the relationship between pointers and arrays
  • Releasing allocated memory with free()
  • Why accessing freed memory causes undefined behavior
  • Understanding memory leaks
  • Introducing calloc()
  • The difference between malloc() and calloc()
  • Zero-initialized allocated memory
  • Understanding allocation sizes and element counts
  • A brief look at integer overflow in allocation calculations
  • Revisiting pointer declarations and dereferencing
  • Understanding memory addresses and pointer values
  • How passing pointers to functions allows modification of original data
  • A discussion about CPU registers and assembly language

One of the main challenges in this lesson is understanding how a pointer relates to the memory allocated by malloc(). We spend time experimenting with the examples, inspecting values, and revisiting earlier pointer concepts.

We also discuss why memory management is the programmer's responsibility in C. Allocated memory must eventually be released, and using memory after freeing it is undefined behavior.

Later, we explore calloc() and how it differs from malloc(), especially when it comes to initialization and allocation size calculations.

The lesson also takes an extended detour back into pointer fundamentals, including addresses, dereferencing, and how pointers relate to the actual contents of memory. We even explore some assembly language concepts to better understand what's happening at a lower level.

As usual, we read the book, experiment with code, ask questions, make mistakes, and work through difficult concepts together with help from the live chat.

This is Part One of Manual Memory Allocation. We'll return to the remaining material in the next lesson.

This is an interactive, follow-along live course. I am learning C alongside you, so expect questions, mistakes, experiments and plenty of discussion. No passive watching here — participate in the chat and work through the examples with us.

00:00:00 Starting the stream 00:12:48 Chapter 12 — Manual Memory Allocation 00:16:36 Automatic vs manual memory management 00:18:49 Memory leaks and freeing memory 00:20:40 Introducing malloc() 00:22:24 Allocating and freeing our first integer 00:27:09 Checking allocation failures 00:38:47 Experimenting with malloc() and pointers 01:13:39 Why understanding pointers matters 01:33:00 Allocating memory for arrays 01:38:14 Introducing calloc() 01:44:24 calloc(), zero initialization and allocation sizes 02:01:14 Changing allocation sizes — what's next? 02:10:00 Revisiting pointers from the beginning 02:35:45 Memory addresses and the address-of operator 02:44:40 Pointer declarations and types 02:50:14 Dereferencing pointers 02:55:00 Why are pointers useful? 03:19:06 Pointers and assembly language 03:25:00 CPU registers and low-level programming 03:40:00 Exploring generated assembly 03:55:00 Post-lesson discussion

📅 We go live every Sunday at 19:00 CEST — interactive, follow-along C programming sessions.

📚 Course materials: Linux Renaissance C Programming Course

💬 Homework & discussion: Linux Renaissance Forum

📺 All C Programming lessons: C Programming playlist

🔴 This stream is proudly sponsored by OPNsense — the open source firewall and routing platform trusted by professionals worldwide. OPNsense

Support Linux Renaissance

🛒 Shop: Linux Renaissance Shop ☕ Ko-fi: Ko-fi 🎖️ Patreon: Patreon ▶️ YouTube membership: YouTube Membership

Find me elsewhere

🌐 Blog: Linux Renaissance 🎥 PeerTube: Linux Renaissance on PeerTube 📺 Odysee: Linux Renaissance on Odysee 🎮 Twitch: Linux Renaissance on Twitch 💬 Matrix: Linux Renaissance Matrix space

no comments (yet)

sorted by: hot top controversial new old
there doesn't seem to be anything here