免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2450 | 回复: 2
打印 上一主题 下一主题

请教realloc 返回值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-10 23:15 |只看该作者 |倒序浏览
本帖最后由 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者, 预先声明本段文字, 请自重。
  1. void* debug_realloc(void* ptr, size_t size, const char* func, int line)
  2. {
  3.     if (ptr == NULL) {
  4.         if (size == 0) {
  5. #ifdef REALLOC_ZERO_BYTES_FREES
  6.             return NULL;
  7. #else
  8.             return debug_malloc(1, func, line);
  9. #endif
  10.         }
  11.         return debug_malloc(size, func, line);
  12.     }

  13.     if (size == 0) {
  14.         debug_free(ptr);
  15.         return ptr;
  16.     }

  17.     struct memory_link* link = ((struct memory_link *) ptr) - 1;
  18.     size_t bytes = size + sizeof(struct memory_link);
  19.     if (bytes < sizeof(struct memory_link)) {
  20.         return NULL;
  21.     }

  22.     if (link->bytes >= bytes) {
  23.         return ptr;
  24.     }

  25.     void* newp = debug_malloc(size, func, line);
  26.     if (newp != NULL) {
  27.         debug_free(ptr);
  28.     }
  29.     return newp;
  30. }
复制代码
修改了一下, 增加
    if (bytes < sizeof(struct memory_link)) {
        return NULL;
    }
代码。

OK, thanks walleeee, 修改了

论坛徽章:
0
2 [报告]
发表于 2012-05-11 00:02 |只看该作者
本帖最后由 walleeee 于 2012-05-11 00:03 编辑

Returns a pointer to a chunk of size n that contains the same data
as does chunk p up to the minimum of (n, p's size) bytes, or null
if no space is available.

The returned pointer may or may not be the same as p. The algorithm
prefers extending p when possible, otherwise it employs the
equivalent of a malloc-copy-free sequence.

If p is null, realloc is equivalent to malloc.

If space is not available, realloc returns null, errno is set (if on
ANSI) and p is NOT freed.

if n is for fewer bytes than already held by p, the newly unused
space is lopped off and freed if possible.  Unless the #define
REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of
zero (re)allocates a minimum-sized chunk.


Large chunks that were internally obtained via mmap will always
be reallocated using malloc-copy-free sequences unless
the system supports MREMAP (currently only linux).

The old unix realloc convention of allowing the last-free'd chunk
to be used as an argument to realloc is not supported.


ps,代码标签不能加粗?

论坛徽章:
0
3 [报告]
发表于 2012-05-11 00:13 |只看该作者
The realloc() function changes the size of the memory block pointed to by ptr to size bytes.  The contents will be unchanged in the range from the start  of
       the  region  up  to the minimum of the old and new sizes.  If the new size is larger than the old size, the added memory will not be initialized.  If ptr is
       NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the  call  is  equivalent  to
       free(ptr)
.   Unless  ptr  is  NULL,  it  must have been returned by an earlier call to malloc(), calloc() or realloc().  If the area pointed to was moved, a
       free(ptr) is done.

貌似两个冲突?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP