ProgrammingModerate
A function returns a pointer to a node created with malloc inside it. Which description is correct?
- A. The caller must not free it because malloc memory frees itself at return
- B. The caller must free it with delete
- C. The pointer becomes invalid as soon as the function returns, so it cannot be used
- D. The caller must eventually call free on the returned pointer exactly once
Correct answer: D. The caller must eventually call free on the returned pointer exactly once
Heap memory outlives the function that allocated it. Ownership passes to the caller, who must free it once, and it is not released when the function returns.
More C Programming questions
- Which statement about calloc(n, size) is guaranteed by the C standard?
- A program calls free(p) and then reads p->v in the next line. What is the correct description?
- A hash chain delete on a singly linked bucket list is written with a pointer-to-pointer. Which description is the advantage?
- Building a list from n values by inserting each new node at the head is O(1) per insert. What is the order of the values when read from…
- Which C statement is correct for a node with an int field v and a next pointer, allocated and initialised to v = 7 with no next node?
- A doubly linked list stores prev pointers. How many pointer fields must change to insert a new node between two adjacent nodes A and B?