Loading...
Engaged Employer
Asked me to write 8 bytes aligned malloc and corresponding free function
Anonymous
one int = 4 bytes.
void *aligned_malloc(int size, int align) { void *p = malloc(sizeof(void *) + size + align); size_t mask = ~(align - 1); size_t p2 = ((size_t)p + align + sizeof(void *)) & mask; *((void **)p2 - 1) = p; return (void *)p2; } void aligned_free(void *p) { void *p1 = *((void **)p - 1); free(p1); }
Check out your Company Bowl for anonymous work chats.
Get actionable career advice tailored to you by joining more bowls.
Stay ahead in opportunities and insider tips by following your dream companies.
Get personalized job recommendations and updates by starting your searches.