你有一個操作系統,它有2個功能處理內存分配的:鑄造地址
void *malloc(int sz) // allocates a memory block sz bytes long
void free(void *addr) // frees a memory block starting at addr
// (previously allocated by malloc)
使用這些功能,實現以下兩個功能:
void *malloc_aligned(int sz) // allocates a memory block sz bytes long,
// aligned to an address divisible by 16
void free_aligned(void *addr) // frees a memory block starting at addr
// (previously allocated by malloc_aligned)
在溶液中有是以下部分:
void * aligned_malloc(size_t size){
unsigned char *res=malloc(size+16);
unsigned char offest=16-((long)res%16);
我不明白的是:爲什麼我們需要使用無符號char以及爲什麼和我們使用16-((long)res%16);
實現了什麼以及在這種情況下(long)res
的用途是什麼?
這是Jango的已知問題嗎? – 2012-01-08 12:12:59
是的,這是來自jungo的問題 – mary 2012-01-08 12:20:22
A.祝你好運(B:下面的答案是好的 – 2012-01-08 12:33:35