NVIDIA Interview Question
Software Engineer / DevelopersYou need to store the offset. Could do something like this in memalign above:
ptr[-1] = align - (uint)ptr % align;
Then, in the free function, you could do something like this:
free(ptr - ptr[-1];
There is a function called posix_memalign(void** memptr, size_t alignment, size_t size) that can do this job. But I think this is not what the interviewer wanted to hear. Also posix_memalign requires that alignment be a power of 2. Somebody could claim this to be a limitation, specially in an interview. ;)
- chandan.jc November 06, 2012