- 论坛徽章:
- 0
|
本帖最后由 fubth 于 2012-05-11 00:17 编辑
正在写一个自己的realloc, 看不懂man里面关于realloc返回值的描述, 主要是代码中的
if (size == 0) {
debug_free(ptr);
return ptr;
}
一句, 不知是返回 NULL 还是 ptr.
ps: 欢迎提出不符合标准或者程序内的bug, 但请不要在效率及代码风格, 或者其他无关紧要方面指指点点。
之前发帖两篇, 某专家在主题为循环展开上面装鬼脸说两段代码不等价; 在另一个关于 static, extern 关键字标准是如何规定的帖子上装B说没包含 stdlib.h; 因此为了防止装B者, 预先声明本段文字, 请自重。- void* debug_realloc(void* ptr, size_t size, const char* func, int line)
- {
- if (ptr == NULL) {
- if (size == 0) {
- #ifdef REALLOC_ZERO_BYTES_FREES
- return NULL;
- #else
- return debug_malloc(1, func, line);
- #endif
- }
- return debug_malloc(size, func, line);
- }
- if (size == 0) {
- debug_free(ptr);
- return ptr;
- }
- struct memory_link* link = ((struct memory_link *) ptr) - 1;
- size_t bytes = size + sizeof(struct memory_link);
- if (bytes < sizeof(struct memory_link)) {
- return NULL;
- }
- if (link->bytes >= bytes) {
- return ptr;
- }
- void* newp = debug_malloc(size, func, line);
- if (newp != NULL) {
- debug_free(ptr);
- }
- return newp;
- }
复制代码 修改了一下, 增加
if (bytes < sizeof(struct memory_link)) {
return NULL;
}
代码。
OK, thanks walleeee, 修改了 |
|