NVIDIA Interview Question

Write and aligned malloc() that gets memory size needed and an alignment mask as its arguements. Write a free() function to go with the aligned malloc that takes only the pointer to the allocated chunk of memory.

Interview Answers

Anonymous

Jan 12, 2011

You basically need to allocated some extra memory with the memory segment that is requested so that you have enough space to align the said chunk and also save a pointer onto the original segment that you get from regular malloc. The free() method via some pointer math will get the "original pointer" and free it.

Anonymous

Feb 13, 2012

For malloc() you can use the concept of segregated list and do sbrk() to get that much chunk of memory from the kernel. It might lead to some internal fragmentation but thats fine...