实现下面二个函数 void * aligned_malloc(size_t bytes, size_t alignment); void aligned_free(void * p); 要求: may only use the C runtime functions malloc and free in their implementation and cannot use any static memory. aligned_malloc takes the size of the buffer you would like to allocate and also alignment which is a power of two that will force the starting address of the buffer you return to the user to start on an alignment boundary. aligned_free frees the buffer returned from aligned_malloc. 我是这么写的,不
s*d
3 楼
遇到一个问题: 用我得来的一个模板编辑文本,结果存完退出之后再次打开,之前写 的example/remark/note 之类的具体内容都不见了,只留下一个tex field indicating “example/remark”. 如何展开呢
d*z
4 楼
两处错误: i)分配内存空间偏小 ii)free()传入的参数不是malloc产生的结果
implementation two
【在 h****a 的大作中提到】 : 实现下面二个函数 : void * aligned_malloc(size_t bytes, size_t alignment); : void aligned_free(void * p); : 要求: : may only use the C runtime functions malloc and free in their implementation : and cannot use any static memory. aligned_malloc takes the size of the : buffer you would like to allocate and also alignment which is a power of two : that will force the starting address of the buffer you return to the user : to start on an alignment boundary. aligned_free frees the buffer returned : from aligned_malloc.
I guess this may work, oh well not sure if I understand the problem correctly. void * aligned_malloc(size_t bytes, size_t alignment) { int total_allocated_size = bytes; // make # of allocated bytes up to aligned boundary total_allocated_size += bytes % alignment; return malloc(total_allocated_size);
}; void aligned_free(void * p) { free(p); };
j*r
12 楼
I was wrong on total_allocated_size calculation. LOL
j*g
13 楼
while (1) { if ((buf =(char*) malloc(byte)) == NULL) exit 1; if (buff % alignment) break; else free(buf); }
【在 j********r 的大作中提到】 : I guess this may work, oh well not sure if I understand the problem : correctly. : void * aligned_malloc(size_t bytes, size_t alignment) : { : int total_allocated_size = bytes; : // make # of allocated bytes up to aligned boundary : total_allocated_size += bytes % alignment; : return malloc(total_allocated_size); : : };