Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Dynamic Memory – malloc | C Programming Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 17 of 50

·c

Dynamic Memory – malloc & free

12 mins

Course progress34%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • `malloc` returns a `void*` – cast to the appropriate pointer type.
  • If allocation fails, `malloc` returns `NULL`.
  • Every successful `malloc`/`calloc`/`realloc` must have a matching `free`.
  • Use tools like **Valgrind** or **AddressSanitizer** to detect leaks.

The heap provides memory at runtime via <stdlib.h> functions:

  • void *malloc(size_t size) – allocate uninitialized memory.
  • void *calloc(size_t count, size_t size) – allocate zero‑initialized memory.
  • void *realloc(void *ptr, size_t newSize) – resize an existing block.
  • void free(void *ptr) – return memory to the system.

Always check the pointer returned by malloc/calloc for NULL. Forgetting free causes memory leaks; double‑free leads to undefined behavior.

Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from C Programming FundamentalsBrowse activities
Previous lessonStrings – Character ArraysContinue to lesson 18Structures (`struct`)Back to C Programming Fundamentals curriculum