是否有另一种方法可以在C中释放动态分配的内存 - 不使用free()函数?

时间:2022-06-07 21:18:10

I am studying for a test, and I was wondering if any of these are equivalent to free(ptr):

我正在研究测试,我想知道这些是否等同于免费(ptr):

 malloc(NULL); 

 calloc(ptr); 

 realloc(NULL, ptr); 

 calloc(ptr, 0); 

 realloc(ptr, 0);

From what I understand, none of these will work because the free() function actually tells C that the memory after ptr is available again for it to use. Sorry that this is kind of a noob question, but help would be appreciated.

根据我的理解,这些都不会起作用,因为free()函数实际上告诉C ptr之后的内存再次可供它使用。对不起,这是一个菜鸟问题,但是将不胜感激。

3 个解决方案

#1


Actually, the last of those is equivalent to a call to free(). Read the specification of realloc() very carefully, and you will find it can allocate data anew, or change the size of an allocation (which, especially if the new size is larger than the old, might move the data around), and it can release memory too. In fact, you don't need the other functions; they can all be written in terms of realloc(). Not that anyone in their right mind would do so...but it could be done.

实际上,最后一个相当于对free()的调用。非常仔细地阅读realloc()的规范,你会发现它可以重新分配数据,或者改变分配的大小(特别是如果新的大小比旧的大,可能会移动数据),它也可以释放记忆。实际上,你不需要其他功能;它们都可以用realloc()来编写。并不是说心智正常的人会这样做......但是可以做到。

See Steve Maguire's "Writing Solid Code" for a complete dissection of the perils of the malloc() family of functions. See the ACCU web site for a complete dissection of the perils of reading "Writing Solid Code". I'm not convinced it is as bad as the reviews make it out to be - though its complete lack of a treatment of const does date it (back to the early 90s, when C89 was still new and not widely implemented in full).

请参阅Steve Maguire的“编写实体代码”,以全面剖析malloc()函数族的危险。请参阅ACCU网站,以全面剖析阅读“编写固体代码”的危险。我不相信它与评论的结果一样糟糕 - 尽管它完全缺乏对const的处理确实可以追溯到它(回到90年代早期,当时C89仍然是新的并且没有全面实施)。


D McKee's notes about MacOS X 10.5 (BSD) are interesting...

D McKee关于MacOS X 10.5(BSD)的说明很有意思......

The C99 standard says:

C99标准说:

7.20.3.3 The malloc function

Synopsis

#include <stdlib.h>
void *malloc(size_t size);

Description

The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

malloc函数为一个对象分配空间,该对象的大小由size指定,其值是不确定的。

Returns

The malloc function returns either a null pointer or a pointer to the allocated space.

malloc函数返回空指针或指向已分配空间的指针。

7.20.3.4 The realloc function

Synopsis

#include <stdlib.h>
void *realloc(void *ptr, size_t size);

Description

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.

realloc函数解除分配ptr指向的旧对象,并返回指向具有size指定大小的新对象的指针。新对象的内容应与解除分配之前的旧对象的内容相同,直到新旧大小中的较小者为止。新对象中超出旧对象大小的任何字节都具有不确定的值。

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.

如果ptr是空指针,则realloc函数的行为类似于指定大小的malloc函数。否则,如果ptr与之前由calloc,malloc或realloc函数返回的指针不匹配,或者如果通过调用free或realloc函数释放了空间,则行为未定义。如果无法分配新对象的内存,则不会释放旧对象,并且其值不会更改。

Returns

The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.

realloc函数返回指向新对象的指针(可能与指向旧对象的指针具有相同的值),如果无法分配新对象,则返回空指针。


Apart from editorial changes because of extra headers and functions, the ISO/IEC 9899:2011 standard says the same as C99, but in section 7.22.3 instead of 7.20.3.

除了由于额外的标题和功能而进行的编辑更改外,ISO / IEC 9899:2011标准与C99相同,但在第7.22.3节而不是7.20.3中。


The Solaris 10 (SPARC) man page for realloc says:

realloc的Solaris 10(SPARC)手册页说:

The realloc() function changes the size of the block pointer to by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If the new size of the block requires movement of the block, the space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the block are unspecified. If ptr is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer, the space pointed to is freed.

realloc()函数将块指针的大小更改为ptr到size字节,并返回指向(可能已移动)块的指针。内容将保持不变,直到新旧尺寸中的较小者。如果块的新大小需要移动块,则释放用于块的先前实例化的空间。如果新大小较大,则未指定块的新分配部分的内容。如果ptr为NULL,则realloc()的行为类似于指定大小的malloc()。如果size为0且ptr不是空指针,则释放指向的空间。

That's a pretty explicit 'it works like free()' statement.

这是一个非常明确的'它就像free()'声明一样。

However, that MacOS X 10.5 or BSD says anything different reaffirms the "No-one in their right mind" part of my first paragraph.

然而,MacOS X 10.5或者BSD说不同的东西重申了我的第一段中的“他们正确的思想中没有人”的一部分。


There is, of course, the C99 Rationale...It says:

当然,还有C99理由......它说:

7.20.3 Memory management functions

The treatment of null pointers and zero-length allocation requests in the definition of these functions was in part guided by a desire to support this paradigm:

在这些函数的定义中处理空指针和零长度分配请求部分是由支持这种范例的愿望指导的:

OBJ * p; // pointer to a variable list of OBJs
    /* initial allocation */
p = (OBJ *) calloc(0, sizeof(OBJ));
     /* ... */
     /* reallocations until size settles */
 while(1) {
    p = (OBJ *) realloc((void *)p, c * sizeof(OBJ));
         /* change value of c or break out of loop */
 }

This coding style, not necessarily endorsed by the Committee, is reported to be in widespread use.

据报道,这种编码方式未得到委员会的认可,因此得到广泛使用。

Some implementations have returned non-null values for allocation requests of zero bytes. Although this strategy has the theoretical advantage of distinguishing between “nothing” and “zero” (an unallocated pointer vs. a pointer to zero-length space), it has the more compelling theoretical disadvantage of requiring the concept of a zero-length object. Since such objects cannot be declared, the only way they could come into existence would be through such allocation requests.

某些实现已为零字节的分配请求返回了非空值。尽管这种策略具有区分“无”和“零”(未分配指针与指向零长度空间的指针)的理论优势,但它具有更强大的理论缺点,即需要零长度对象的概念。由于无法声明此类对象,因此它们可能存在的唯一方法是通过此类分配请求。

The C89 Committee decided not to accept the idea of zero-length objects. The allocation functions may therefore return a null pointer for an allocation request of zero bytes. Note that this treatment does not preclude the paradigm outlined above.

C89委员会决定不接受零长度物体的想法。因此,分配函数可以返回零指针分配请求的空指针。请注意,这种处理方法并不排除上述范例。

QUIET CHANGE IN C89

C89的安静改变

A program which relies on size-zero allocation requests returning a non-null pointer will behave differently.

依赖于返回非空指针的大小为零的分配请求的程序将表现不同。

[...]

7.20.3.4 The realloc function

A null first argument is permissible. If the first argument is not null, and the second argument is 0, then the call frees the memory pointed to by the first argument, and a null argument may be returned; C99 is consistent with the policy of not allowing zero-sized objects.

null第一个参数是允许的。如果第一个参数不为null,并且第二个参数为0,则调用将释放第一个参数指向的内存,并且可能返回null参数; C99与不允许零大小对象的策略一致。

A new feature of C99: the realloc function was changed to make it clear that the pointed-to object is deallocated, a new object is allocated, and the content of the new object is the same as that of the old object up to the lesser of the two sizes. C89 attempted to specify that the new object was the same object as the old object but might have a different address. This conflicts with other parts of the Standard that assume that the address of an object is constant during its lifetime. Also, implementations that support an actual allocation when the size is zero do not necessarily return a null pointer for this case. C89 appeared to require a null return value, and the Committee felt that this was too restrictive.

C99的一个新功能:更改了realloc函数,以清楚地指出了指向的对象被释放,分配了一个新对象,并且新对象的内容与旧对象的内容相同,直到较小的对象这两种尺寸。 C89尝试指定新对象与旧对象是同一个对象,但可能具有不同的地址。这与标准的其他部分冲突,这些部分假定对象的地址在其生命周期内是不变的。此外,当大小为零时支持实际分配的实现不一定返回此情况的空指针。 C89似乎要求无效返回值,委员会认为这个限制性太强。


Thomas Padron-McCarthy observed:

Thomas Padron-McCarthy观察到:

C89 explicitly says: "If size is zero and ptr is not a null pointer, the object it points to is freed." So they seem to have removed that sentence in C99?

C89明确地说:“如果size为零且ptr不是空指针,则它指向的对象将被释放。”所以他们似乎已经删除了C99中的那个句子?

Yes, they have removed that sentence because it is subsumed by the opening sentence:

是的,他们删除了这句话,因为它被开头的句子包含在内:

The realloc function deallocates the old object pointed to by ptr

realloc函数释放ptr指向的旧对象

There's no wriggle room there; the old object is deallocated. If the requested size is zero, then you get back whatever malloc(0) might return, which is often (usually) a null pointer but might be a non-null pointer that can also be returned to free() but which cannot legitimately be dereferenced.

那里没有蠕动的空间;旧对象被取消分配。如果请求的大小为零,那么你可以返回malloc(0)可能返回的任何内容,这通常(通常)是一个空指针,但可能是一个非空指针,也可以返回到free()但是不能合法地返回解引用。

#2


realloc(ptr, 0);

is equivalent to free(ptr); (although I wouldn't recommended its use as such!)

相当于免费(ptr); (虽然我不建议使用它!)

Also: these two calls are equivalent to each other (but not to free):

另外:这两个调用彼此相同(但不是免费的):

realloc(NULL,size)
malloc(size)

#3


The last one--realloc(ptr, 0)--comes close. It will free any allocated block and replace it with a minimal allocation (says my Mac OS X 10.5 manpage). Check your local manpage to see what it does on your system.

最后一个 - realloc(ptr,0) - 接近尾声。它将释放任何已分配的块并用最小的分配替换它(我的Mac OS X 10.5联机帮助页)。检查您的本地联机帮助页以查看它在您的系统上执行的操作。

That is, if ptr pointed at a substantial object, you'll get back most of its memory.

也就是说,如果ptr指向一个实质性的对象,你将获得大部分内存。


The man page on Debian Lenny agrees with Mitch and Jonathan...does BSD really diverge from Linux on this?

Debian Lenny的手册页与Mitch和Jonathan一致...... BSD真的与Linux有分歧吗?


From the offending man page:

从违规手册页:

The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. [...] If size is zero and ptr is not NULL, a new, minimum sized object is allocated and the original object is freed.

realloc()函数尝试将ptr指向的分配大小更改为size,并返回ptr。 [...]如果size为零且ptr不为NULL,则会分配一个新的最小大小的对象,并释放原始对象。


The linux and solaris man pages are very clean, and the '89 standard: realloc(ptr,0) works like free(ptr). The Mac OS manpage above, and the standard as quoted by Jonathan are less clear but seems to leave room to break the equivalence.

linux和solaris手册页非常干净,'89标准:realloc(ptr,0)就像free(ptr)一样工作。上面的Mac OS手册页和Jonathan引用的标准不太清楚,但似乎留下了打破等价的空间。

I've been wondering why the difference: the "act like free" interpretation seems very natural to me. Both of the implementations I have access to include some environment variable driven tunablity, but the BSD version accepts many more options Some examples:

我一直想知道为什么会有这样的区别:“像免费一样”的解释对我来说似乎很自然。我可以访问的两个实现包括一些环境变量驱动的可调性,但BSD版本接受更多选项一些示例:

 MallocGuardEdges             If set, add a guard page before and after
                              each large block.  
 MallocDoNotProtectPrelude    If set, do not add a guard page before large
                              blocks, even if the MallocGuardEdges envi-
                              ronment variable is set.

 MallocDoNotProtectPostlude   If set, do not add a guard page after large
                              blocks, even if the MallocGuardEdges envi-
                              ronment variable is set.

and

 MallocPreScribble            If set, fill memory that has been allocated
                              with 0xaa bytes.  This increases the likeli-
                              hood that a program making assumptions about
                              the contents of freshly allocated memory
                              will fail.
 MallocScribble               If set, fill memory that has been deallo-
                              cated with 0x55 bytes.  This increases the
                              likelihood that a program will fail due to
                              accessing memory that is no longer allo-
                              cated.

Possibly the "minimum sized object" is nothing (i.e. equivalent to free) in the normal modes, but something with some of the guards in place. Take that for what it's worth.

在正常模式下,“最小尺寸的物体”可能不是(即相当于*),而是某些防护装置就位。把它拿走它的价值。

#1


Actually, the last of those is equivalent to a call to free(). Read the specification of realloc() very carefully, and you will find it can allocate data anew, or change the size of an allocation (which, especially if the new size is larger than the old, might move the data around), and it can release memory too. In fact, you don't need the other functions; they can all be written in terms of realloc(). Not that anyone in their right mind would do so...but it could be done.

实际上,最后一个相当于对free()的调用。非常仔细地阅读realloc()的规范,你会发现它可以重新分配数据,或者改变分配的大小(特别是如果新的大小比旧的大,可能会移动数据),它也可以释放记忆。实际上,你不需要其他功能;它们都可以用realloc()来编写。并不是说心智正常的人会这样做......但是可以做到。

See Steve Maguire's "Writing Solid Code" for a complete dissection of the perils of the malloc() family of functions. See the ACCU web site for a complete dissection of the perils of reading "Writing Solid Code". I'm not convinced it is as bad as the reviews make it out to be - though its complete lack of a treatment of const does date it (back to the early 90s, when C89 was still new and not widely implemented in full).

请参阅Steve Maguire的“编写实体代码”,以全面剖析malloc()函数族的危险。请参阅ACCU网站,以全面剖析阅读“编写固体代码”的危险。我不相信它与评论的结果一样糟糕 - 尽管它完全缺乏对const的处理确实可以追溯到它(回到90年代早期,当时C89仍然是新的并且没有全面实施)。


D McKee's notes about MacOS X 10.5 (BSD) are interesting...

D McKee关于MacOS X 10.5(BSD)的说明很有意思......

The C99 standard says:

C99标准说:

7.20.3.3 The malloc function

Synopsis

#include <stdlib.h>
void *malloc(size_t size);

Description

The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

malloc函数为一个对象分配空间,该对象的大小由size指定,其值是不确定的。

Returns

The malloc function returns either a null pointer or a pointer to the allocated space.

malloc函数返回空指针或指向已分配空间的指针。

7.20.3.4 The realloc function

Synopsis

#include <stdlib.h>
void *realloc(void *ptr, size_t size);

Description

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.

realloc函数解除分配ptr指向的旧对象,并返回指向具有size指定大小的新对象的指针。新对象的内容应与解除分配之前的旧对象的内容相同,直到新旧大小中的较小者为止。新对象中超出旧对象大小的任何字节都具有不确定的值。

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.

如果ptr是空指针,则realloc函数的行为类似于指定大小的malloc函数。否则,如果ptr与之前由calloc,malloc或realloc函数返回的指针不匹配,或者如果通过调用free或realloc函数释放了空间,则行为未定义。如果无法分配新对象的内存,则不会释放旧对象,并且其值不会更改。

Returns

The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.

realloc函数返回指向新对象的指针(可能与指向旧对象的指针具有相同的值),如果无法分配新对象,则返回空指针。


Apart from editorial changes because of extra headers and functions, the ISO/IEC 9899:2011 standard says the same as C99, but in section 7.22.3 instead of 7.20.3.

除了由于额外的标题和功能而进行的编辑更改外,ISO / IEC 9899:2011标准与C99相同,但在第7.22.3节而不是7.20.3中。


The Solaris 10 (SPARC) man page for realloc says:

realloc的Solaris 10(SPARC)手册页说:

The realloc() function changes the size of the block pointer to by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If the new size of the block requires movement of the block, the space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the block are unspecified. If ptr is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer, the space pointed to is freed.

realloc()函数将块指针的大小更改为ptr到size字节,并返回指向(可能已移动)块的指针。内容将保持不变,直到新旧尺寸中的较小者。如果块的新大小需要移动块,则释放用于块的先前实例化的空间。如果新大小较大,则未指定块的新分配部分的内容。如果ptr为NULL,则realloc()的行为类似于指定大小的malloc()。如果size为0且ptr不是空指针,则释放指向的空间。

That's a pretty explicit 'it works like free()' statement.

这是一个非常明确的'它就像free()'声明一样。

However, that MacOS X 10.5 or BSD says anything different reaffirms the "No-one in their right mind" part of my first paragraph.

然而,MacOS X 10.5或者BSD说不同的东西重申了我的第一段中的“他们正确的思想中没有人”的一部分。


There is, of course, the C99 Rationale...It says:

当然,还有C99理由......它说:

7.20.3 Memory management functions

The treatment of null pointers and zero-length allocation requests in the definition of these functions was in part guided by a desire to support this paradigm:

在这些函数的定义中处理空指针和零长度分配请求部分是由支持这种范例的愿望指导的:

OBJ * p; // pointer to a variable list of OBJs
    /* initial allocation */
p = (OBJ *) calloc(0, sizeof(OBJ));
     /* ... */
     /* reallocations until size settles */
 while(1) {
    p = (OBJ *) realloc((void *)p, c * sizeof(OBJ));
         /* change value of c or break out of loop */
 }

This coding style, not necessarily endorsed by the Committee, is reported to be in widespread use.

据报道,这种编码方式未得到委员会的认可,因此得到广泛使用。

Some implementations have returned non-null values for allocation requests of zero bytes. Although this strategy has the theoretical advantage of distinguishing between “nothing” and “zero” (an unallocated pointer vs. a pointer to zero-length space), it has the more compelling theoretical disadvantage of requiring the concept of a zero-length object. Since such objects cannot be declared, the only way they could come into existence would be through such allocation requests.

某些实现已为零字节的分配请求返回了非空值。尽管这种策略具有区分“无”和“零”(未分配指针与指向零长度空间的指针)的理论优势,但它具有更强大的理论缺点,即需要零长度对象的概念。由于无法声明此类对象,因此它们可能存在的唯一方法是通过此类分配请求。

The C89 Committee decided not to accept the idea of zero-length objects. The allocation functions may therefore return a null pointer for an allocation request of zero bytes. Note that this treatment does not preclude the paradigm outlined above.

C89委员会决定不接受零长度物体的想法。因此,分配函数可以返回零指针分配请求的空指针。请注意,这种处理方法并不排除上述范例。

QUIET CHANGE IN C89

C89的安静改变

A program which relies on size-zero allocation requests returning a non-null pointer will behave differently.

依赖于返回非空指针的大小为零的分配请求的程序将表现不同。

[...]

7.20.3.4 The realloc function

A null first argument is permissible. If the first argument is not null, and the second argument is 0, then the call frees the memory pointed to by the first argument, and a null argument may be returned; C99 is consistent with the policy of not allowing zero-sized objects.

null第一个参数是允许的。如果第一个参数不为null,并且第二个参数为0,则调用将释放第一个参数指向的内存,并且可能返回null参数; C99与不允许零大小对象的策略一致。

A new feature of C99: the realloc function was changed to make it clear that the pointed-to object is deallocated, a new object is allocated, and the content of the new object is the same as that of the old object up to the lesser of the two sizes. C89 attempted to specify that the new object was the same object as the old object but might have a different address. This conflicts with other parts of the Standard that assume that the address of an object is constant during its lifetime. Also, implementations that support an actual allocation when the size is zero do not necessarily return a null pointer for this case. C89 appeared to require a null return value, and the Committee felt that this was too restrictive.

C99的一个新功能:更改了realloc函数,以清楚地指出了指向的对象被释放,分配了一个新对象,并且新对象的内容与旧对象的内容相同,直到较小的对象这两种尺寸。 C89尝试指定新对象与旧对象是同一个对象,但可能具有不同的地址。这与标准的其他部分冲突,这些部分假定对象的地址在其生命周期内是不变的。此外,当大小为零时支持实际分配的实现不一定返回此情况的空指针。 C89似乎要求无效返回值,委员会认为这个限制性太强。


Thomas Padron-McCarthy observed:

Thomas Padron-McCarthy观察到:

C89 explicitly says: "If size is zero and ptr is not a null pointer, the object it points to is freed." So they seem to have removed that sentence in C99?

C89明确地说:“如果size为零且ptr不是空指针,则它指向的对象将被释放。”所以他们似乎已经删除了C99中的那个句子?

Yes, they have removed that sentence because it is subsumed by the opening sentence:

是的,他们删除了这句话,因为它被开头的句子包含在内:

The realloc function deallocates the old object pointed to by ptr

realloc函数释放ptr指向的旧对象

There's no wriggle room there; the old object is deallocated. If the requested size is zero, then you get back whatever malloc(0) might return, which is often (usually) a null pointer but might be a non-null pointer that can also be returned to free() but which cannot legitimately be dereferenced.

那里没有蠕动的空间;旧对象被取消分配。如果请求的大小为零,那么你可以返回malloc(0)可能返回的任何内容,这通常(通常)是一个空指针,但可能是一个非空指针,也可以返回到free()但是不能合法地返回解引用。

#2


realloc(ptr, 0);

is equivalent to free(ptr); (although I wouldn't recommended its use as such!)

相当于免费(ptr); (虽然我不建议使用它!)

Also: these two calls are equivalent to each other (but not to free):

另外:这两个调用彼此相同(但不是免费的):

realloc(NULL,size)
malloc(size)

#3


The last one--realloc(ptr, 0)--comes close. It will free any allocated block and replace it with a minimal allocation (says my Mac OS X 10.5 manpage). Check your local manpage to see what it does on your system.

最后一个 - realloc(ptr,0) - 接近尾声。它将释放任何已分配的块并用最小的分配替换它(我的Mac OS X 10.5联机帮助页)。检查您的本地联机帮助页以查看它在您的系统上执行的操作。

That is, if ptr pointed at a substantial object, you'll get back most of its memory.

也就是说,如果ptr指向一个实质性的对象,你将获得大部分内存。


The man page on Debian Lenny agrees with Mitch and Jonathan...does BSD really diverge from Linux on this?

Debian Lenny的手册页与Mitch和Jonathan一致...... BSD真的与Linux有分歧吗?


From the offending man page:

从违规手册页:

The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. [...] If size is zero and ptr is not NULL, a new, minimum sized object is allocated and the original object is freed.

realloc()函数尝试将ptr指向的分配大小更改为size,并返回ptr。 [...]如果size为零且ptr不为NULL,则会分配一个新的最小大小的对象,并释放原始对象。


The linux and solaris man pages are very clean, and the '89 standard: realloc(ptr,0) works like free(ptr). The Mac OS manpage above, and the standard as quoted by Jonathan are less clear but seems to leave room to break the equivalence.

linux和solaris手册页非常干净,'89标准:realloc(ptr,0)就像free(ptr)一样工作。上面的Mac OS手册页和Jonathan引用的标准不太清楚,但似乎留下了打破等价的空间。

I've been wondering why the difference: the "act like free" interpretation seems very natural to me. Both of the implementations I have access to include some environment variable driven tunablity, but the BSD version accepts many more options Some examples:

我一直想知道为什么会有这样的区别:“像免费一样”的解释对我来说似乎很自然。我可以访问的两个实现包括一些环境变量驱动的可调性,但BSD版本接受更多选项一些示例:

 MallocGuardEdges             If set, add a guard page before and after
                              each large block.  
 MallocDoNotProtectPrelude    If set, do not add a guard page before large
                              blocks, even if the MallocGuardEdges envi-
                              ronment variable is set.

 MallocDoNotProtectPostlude   If set, do not add a guard page after large
                              blocks, even if the MallocGuardEdges envi-
                              ronment variable is set.

and

 MallocPreScribble            If set, fill memory that has been allocated
                              with 0xaa bytes.  This increases the likeli-
                              hood that a program making assumptions about
                              the contents of freshly allocated memory
                              will fail.
 MallocScribble               If set, fill memory that has been deallo-
                              cated with 0x55 bytes.  This increases the
                              likelihood that a program will fail due to
                              accessing memory that is no longer allo-
                              cated.

Possibly the "minimum sized object" is nothing (i.e. equivalent to free) in the normal modes, but something with some of the guards in place. Take that for what it's worth.

在正常模式下,“最小尺寸的物体”可能不是(即相当于*),而是某些防护装置就位。把它拿走它的价值。