🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Dynamic Memory: all 10 questions

Quiz: Dynamic Memory · Question 8 of 10

A function returns a pointer to memory it got from malloc. Is that pointer still valid after the function returns?

A function returns a pointer to memory it got from malloc. Is that pointer still valid after the function returns?

  1. A.No, all memory of a function is freed when it returns
  2. B.Yes, heap memory stays allocated until free is called
  3. C.Only if the function is static
  4. D.Only until the next printf
Answer & explanation

Correct answer: Yes, heap memory stays allocated until free is called

Unlike a local array on the stack, malloc'd memory outlives the function; the caller must free it.

Can you get the rest right without peeking?

Take the timed Quiz: Dynamic Memory quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →