Dynamic Memory Allocation in C: malloc and free: 25 flashcards

All 25 flashcards from this kit, free and with no sign-up.

1. What is dynamic memory allocation?

Show the answer

A technique where a running C program requests memory from the heap at runtime instead of having the compiler fix the size beforehand.

2. Which header declares malloc, calloc, realloc, and free?

Show the answer

`<stdlib.h>`

3. What does `malloc(size)` return on success?

Show the answer

A `void*` pointer to a block of uninitialized memory of `size` bytes.

4. What does `malloc` return when allocation fails?

Show the answer

`NULL`

5. What is the state of memory returned by `malloc`?

Show the answer

Uninitialized — it contains garbage values from previous use.

6. What does `calloc(n, size)` do differently from `malloc`?

Show the answer

It allocates space for `n` items of `size` bytes each and initializes every byte to zero.

7. What does `realloc(ptr, new_size)` do?

Show the answer

Resizes a previously allocated block, preserving existing contents up to the smaller of old/new size; may move the block and return a new pointer.

8. What does `free(ptr)` actually do?

Show the answer

It releases the heap block back to the allocator; it does not erase data or set the pointer to `NULL`.

9. Why must you cast the `void*` from `malloc`?

Show the answer

Because `void*` has no type; casting to the target pointer type (e.g., `(int *)`) lets the compiler treat the block correctly.

10. What is the correct way to compute allocation size for an array of `n` integers?

Show the answer

`n * sizeof(int)` — always multiply the count by `sizeof(type)`.

11. What is a memory leak?

Show the answer

Memory that was allocated but never freed, so the heap shrinks over time until the program may crash.

12. What is a dangling pointer?

Show the answer

A pointer that still holds the address of a block after it has been freed; using it is undefined behavior.

13. What is a double free and why is it dangerous?

Show the answer

Calling `free` twice on the same block; it corrupts the heap and is undefined behavior.

14. Why assign `realloc`'s result to a temporary pointer first?

Show the answer

If `realloc` fails and returns `NULL`, the original pointer is still valid and must be freed; assigning directly would lose it and leak the old block.

15. What happens to the original block if `realloc` fails?

Show the answer

It remains valid and unchanged — you must still free it later.

16. Why set a pointer to `NULL` after `free`?

Show the answer

It prevents accidental reuse of the dangling pointer, which would be undefined behavior.

17. Can you `free` a pointer to a stack variable?

Show the answer

No — `free` only accepts pointers returned by `malloc`, `calloc`, or `realloc`.

18. Where does dynamically allocated memory live?

Show the answer

On the heap, which persists until explicitly freed or the program exits.

19. How does stack memory differ from heap memory in lifetime?

Show the answer

Stack memory is automatically reclaimed when a function returns; heap memory is not — it must be freed manually.

20. When is `calloc` preferred over `malloc`?

Show the answer

When you need zero-initialized memory, such as an array of counters that should start at 0.

21. When is `malloc` preferred over `calloc`?

Show the answer

When every byte will be overwritten immediately, so zeroing would be wasted work.

22. Name two tools that detect memory leaks and invalid accesses.

Show the answer

Valgrind and AddressSanitizer.

23. What real data structures rely on dynamic memory allocation?

Show the answer

Dynamic arrays, linked lists, trees, and graphs — anything whose size is unknown at compile time.

24. What is the complete rhythm for safe dynamic memory use?

Show the answer

Allocate, check for `NULL`, use, free, and set the pointer to `NULL`.

25. Why is `malloc(5)` wrong when you meant five integers?

Show the answer

It allocates only 5 bytes, not 20; writing `arr[3]` then corrupts memory beyond the block.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages