Re:I had to give a wrong answer to get the job (2017)
by hnfong on Friday July 23, 2021 @02:21PM
(#27933985)
A typical C implementation would be using realloc():
The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc() creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory.
Worst case definitely 2x memory. But not necessarily always.
The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc() creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory.
Worst case definitely 2x memory. But not necessarily always.