← 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?
- A.No, all memory of a function is freed when it returns
- B.Yes, heap memory stays allocated until free is called
- C.Only if the function is static
- 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 →