ProgrammingModerate
A program keeps 10^5 small objects in a free list of pointers. Which operation is O(1) per object?
- A. Search the free list for a specific pointer before each push
- B. Rebuild the whole free list for every allocation
- C. Sort the free list after each free
- D. Push a freed object onto the free list, and pop one to reuse it
Correct answer: D. Push a freed object onto the free list, and pop one to reuse it
Pushing and popping at the head of the free list touches only one node, which makes both operations constant time.
More C Programming questions
- int arr = malloc(5 sizeof arr); for (int i = 0; i < 5; i++) arr[i] = i i; int q = arr + 2; printf("%d", q[1]); What is printed?
- struct N { int v; struct N next; } a = NULL; for (int i = 1; i <= 3; i++) { struct N n = malloc(sizeof n); n->v = i; n->next = a; a = n; }…
- Two pointers p and q point into the same block allocated with malloc(10 sizeof(int)). Which subtraction is defined?
- A linked list of length n is reversed in place with a loop. How much extra memory is used?
- unsigned int h = 0; for (const char p = "ab"; p; p++) h = h 31 + p; printf("%u", h); What is printed (ASCII, 32-bit unsigned)?
- unsigned int x = 0b11011010; printf("%u", (x >> 3) & 7); What is printed?