如果我重新分配并且新大小为 0,会发生什么情况。这与释放等效吗?

发布于 2024-08-26 23:19:35 字数 268 浏览 9 评论 0原文

给出以下代码:

int *a = NULL;
a = calloc(1, sizeof(*a));
printf("%d\n", a);
a = realloc(a, 0);

printf("%d\n", a);
return (0);

它返回:

4078904
0

this realloc 相当于 free 吗?

笔记: 我在WindowsXP下使用MinGW。

Given the following code:

int *a = NULL;
a = calloc(1, sizeof(*a));
printf("%d\n", a);
a = realloc(a, 0);

printf("%d\n", a);
return (0);

It returns:

4078904
0

Is this realloc equivalent to a free ?

NOTE:
I am using MinGW under WindowsXP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小猫一只 2024-09-02 23:19:35

它可能等同于也可能不等同于在指针上调用free;结果是实现定义的。

来自 C99 标准 (§7.20.3/1):

如果请求的空间大小为零,则行为是实现定义的:要么返回空指针,要么行为就好像大小是某个非零值,但返回的指针不得用于访问对象。

这适用于所有内存管理函数,包括realloc。

It may or may not be equivalent to calling free on the pointer; the result is implementation-defined.

From the C99 standard (§7.20.3/1):

If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

That applies to all of the memory management functions, including realloc.

瞳孔里扚悲伤 2024-09-02 23:19:35

未必。

它通常与 munissor 发布的链接,但 Mac OS 10.5 手册页显示:

如果 size 为零且 ptr 不为 NULL,则分配一个新的最小大小的对象并释放原始对象。

什么是“最小尺寸的物体”?好吧,任何分配器都会存储一些有关分配的信息,这会占用除了为用户保留的空间之外通常分配的空间。据推测,“最小大小的对象”只是这些标头之一加上为用户保留的零字节空间。

我猜想这个规定的存在是为了支持标准化时存在的实现,并且这些实现对于调试分配行为很有用。


解决Jonathan 的评论

之间的区别

for (int i=0; i<VERY_BIG_NUMBER; ++i){
  char *p = malloc(sizeof(char[10]));
  free(p);
}

考虑和

for (int i=0; i<VERY_BIG_NUMBER; ++i){
  char *p = malloc(sizeof(char[10]));
  realloc(p,0);
}

通过 mallocfree 的合理实现,第一个剪辑会不无限制地消耗内存。但如果 realloc 实现返回那些“最小大小的对象”,它可能会返回。

当然,这个例子是人为的,它依赖于理解“最小尺寸的对象”的含义,但我认为文本允许这样做。

简而言之,如果您意思免费,您应该说免费

Not necessarily.

It often does as with the link that munissor posted, but the Mac OS 10.5 man page says:

If size is zero and ptr is not NULL, a new, minimum sized object is allocated and the original object is freed.

What is a "minimum sized object"? Well, any allocator stores some information about the allocations, and that takes space which is often allotted in addition to the space reserved for the user. Presumably a "minimum sized object" is just one of these headers plus zero bytes of space reserved for the user.

I would guess that this provision is present to support implementations that existed at the time of standardization, and that those implementations are useful for debugging allocation behavior.


To address Jonathan's comments

Consider the difference between

for (int i=0; i<VERY_BIG_NUMBER; ++i){
  char *p = malloc(sizeof(char[10]));
  free(p);
}

and

for (int i=0; i<VERY_BIG_NUMBER; ++i){
  char *p = malloc(sizeof(char[10]));
  realloc(p,0);
}

With a sane implementation of malloc and free the first clip does not consume memory without bound. But if the realloc implementation returns those "minimum sized objects" it might.

Certainly this example is contrived and it relies on understanding what is meant by "minimum sized object", but I think that text allows it.

In short, if you mean free you should say free.

风吹过旳痕迹 2024-09-02 23:19:35

C99 标准 §7.20.3.4 (realloc) 说:

realloc函数释放ptr指向的旧对象并返回一个
指向具有 size 指定大小的新对象的指针。新内容
对象应与释放前的旧对象相同,最多为以下较小者
新旧尺寸。新对象中超出旧对象大小的任何字节都具有
不确定的值。

如果 ptr 是空指针,则 realloc 函数的行为类似于 malloc 函数
指定尺寸。否则,如果 ptr 与之前返回的指针不匹配
calloc、malloc 或 realloc 函数,或者空间已通过调用释放
对于 free 或 realloc 函数,行为是未定义的。如果记忆是新的
对象无法分配,旧对象没有被释放,它的值没有改变。

这清楚地表明旧对象已被释放(释放)。返回值可能是空指针,也可能是第 7.20.3 节一般注释中指定的值:

如果请求的空间大小为零,则行为是实现定义的:
要么返回空指针,要么行为就像大小是某个
非零值,但返回的指针不得用于访问对象。

无论哪种方式,您都不能取消引用返回的值:它可以用作 free() 的参数,或者传递给其他函数,只要它们不引用它即可。

Yes

The C99 standard §7.20.3.4 (realloc) says:

The realloc function deallocates the old object pointed to by ptr and returns a
pointer to a new object that has the size specified by size. The contents of the new
object shall be the same as that of the old object prior to deallocation, up to the lesser of
the new and old sizes. Any bytes in the new object beyond the size of the old object have
indeterminate values.

If ptr is a null pointer, the realloc function behaves like the malloc function for the
specified size. Otherwise, if ptr does not match a pointer earlier returned by the
calloc, malloc, or realloc function, or if the space has been deallocated by a call
to the free or realloc function, the behavior is undefined. If memory for the new
object cannot be allocated, the old object is not deallocated and its value is unchanged.

This clearly states that the old object is deallocated (freed). The return value might be a null pointer, or it might be a value as specified in the general notes for §7.20.3:

If the size of the space requested is zero, the behavior is implementation defined:
either a null pointer is returned, or the behavior is as if the size were some
nonzero value, except that the returned pointer shall not be used to access an object.

Either way, you cannot dereference the value returned: it could be used as an argument to free(), or passed to other functions as long as they in turn do not reference it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文