LearnFrenzy
Sign in Take a free test

Memory Allocation · Exercise 4 of 4

Memory Allocation Memory Programs

  • 8 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Programs that manage heap memory correctly – every one ran cleanly under AddressSanitizer, and the output shown is the real output. Write each one yourself first, then compare.

Question 1 Easy

Write concat() that returns a newly allocated string holding a followed by b. The caller frees it.

Question page & discussion
Question 2 Hard

Write read_line() that reads a whole line of any length from a stream into a heap buffer that grows as needed.

Question page & discussion
Question 3 Hard

Return a heap-allocated array of heap-allocated words, plus a function that frees all of it.

Question page & discussion
Question 4 Medium

Implement push and pop for a stack that doubles its capacity when full.

Question page & discussion
Question 5 Medium

Reverse a singly linked list by changing the links, without allocating new nodes, then free the list.

Question page & discussion
Question 6 Hard

Implement my_calloc() on top of malloc(): zeroed memory, and NULL when count × size overflows.

Question page & discussion
Question 7 Medium

Return a new heap array with each value of an unsorted array (values 0–99) once, in first-seen order.

Question page & discussion
Question 8 Hard

Build an arena: one big block, small aligned allocations handed out from it, and everything freed with a single call.

Question page & discussion
All exercises doneBack to Memory AllocationRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links