mayorga.geek
BAN USER1.- you are adjusting the requested size instead of the memory address returned by malloc
2. why you pass void** in alignedFree instead of the same value that alignedMalloc returns?
void * aligned_alloc(size_t size, int alignment_bits) {
//reserve requested size plus: space for allocating a pointer plus spac$
size_t sz=size+sizeof(void*)+(1<<alignment_bits);
char *p0=(char*)malloc(sz);
if (p0==0) return 0;
//advance p to its maximum value
char* p=p0+sizeof(void*)+(1<<alignment_bits);
//cut less significant bits to achieve alignment
p=(char*)((uintptr_t)p&(uintptr_t)~((1<<alignment_bits)-1));
//store real addresss in the previous address
*(void**)(p-sizeof(void*))=p0;
return (void*)p;
}
void alignned_free(void* p) {
free((char*)p-sizeof(void*));
}
you're right malinbupt, thank you.
here is my correction:
- mayorga.geek September 22, 2015