是否有其他方法可以释放 C 中动态分配的内存 - 不使用 free() 函数?
我正在学习测试,我想知道其中是否有任何一个相当于 free(ptr):
malloc(NULL);
calloc(ptr);
realloc(NULL, ptr);
calloc(ptr, 0);
realloc(ptr, 0);
据我所知,这些都不起作用,因为 free() 函数实际上告诉 C ptr 之后的内存再次可用供其使用。 抱歉,这是一个菜鸟问题,但我们将不胜感激。
I am studying for a test, and I was wondering if any of these are equivalent to free(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,最后一个相当于调用
free()
。 仔细阅读 realloc() 的规范,您会发现它可以重新分配数据,或者更改分配的大小(尤其是当新大小大于旧大小时,可能会移动周围的数据),它也可以释放内存。 事实上,您不需要其他功能; 它们都可以用realloc()
来编写。 并不是任何心智正常的人都会这样做……但这是可以做到的。请参阅 Steve Maguire 的“编写可靠的代码”,完整剖析
malloc()
函数系列。 请参阅 ACCU 网站,完整剖析阅读“编写可靠代码”的危险。 我不相信它像评论中所说的那么糟糕 - 尽管它完全缺乏对 const 的处理确实已经过时了(可以追溯到 90 年代初,当时 C89 还很新,并且未广泛全面实施)。D McKee 关于 MacOS X 10.5 (BSD) 的注释很有趣...
C99 标准说:
7.20.3.3 The malloc 函数
7.20.3.4 realloc 函数
除了因额外标头和函数而进行的编辑性更改外,ISO/IEC 9899:2011 标准与 C99 的表述相同,但在第 7.22.3 节中而不是在第 7.20.3 节中。
realloc 的 Solaris 10 (SPARC) 手册页显示:
这是一个非常明确的“它像 free() 一样工作”的声明。
然而,MacOS X 10.5 或 BSD 的说法有所不同,这再次证实了我第一段中“没有人头脑正常”的部分。
当然,还有 C99 基本原理...它说:
7.20.3 内存管理功能
[...]
7.20.3.4 realloc 函数
托马斯·帕德隆-麦卡锡 观察到:
是的,他们删除了该句子,因为它包含在开头句子中:
那里没有回旋余地; 旧对象被释放。 如果请求的大小为零,那么您将返回
malloc(0)
可能返回的任何内容,这通常是一个空指针,但也可能是一个非空指针,也可以返回到 < code>free() 但不能合法地取消引用。Actually, the last of those is equivalent to a call to
free()
. Read the specification ofrealloc()
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 ofrealloc()
. Not that anyone in their right mind would do so...but it could be done.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 ofconst
does date it (back to the early 90s, when C89 was still new and not widely implemented in full).D McKee's notes about MacOS X 10.5 (BSD) are interesting...
The C99 standard says:
7.20.3.3 The malloc function
7.20.3.4 The realloc function
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.
The Solaris 10 (SPARC) man page for realloc says:
That's a pretty explicit 'it works like free()' statement.
However, that MacOS X 10.5 or BSD says anything different reaffirms the "No-one in their right mind" part of my first paragraph.
There is, of course, the C99 Rationale...It says:
7.20.3 Memory management functions
[...]
7.20.3.4 The realloc function
Thomas Padron-McCarthy observed:
Yes, they have removed that sentence because it is subsumed by the opening sentence:
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 tofree()
but which cannot legitimately be dereferenced.相当于
free(ptr);
(尽管我不建议这样使用它!)另外:这两个调用彼此等效(但不等于 free):
is equivalent to
free(ptr);
(although I wouldn't recommended its use as such!)Also: these two calls are equivalent to each other (but not to free):
最后一个 -
realloc(ptr, 0)
- 很接近。 它将释放任何分配的块并用最小的分配替换它(我的 Mac OS X 10.5 联机帮助页)。 检查本地联机帮助页以了解它在您的系统上的作用。也就是说,如果
ptr
指向一个实体对象,您将取回其大部分内存。Debian Lenny 的手册页同意 米奇和Jonathan...BSD 在这方面真的与 Linux 不同吗?
从有问题的手册页:
Linux 和Solaris 手册页非常干净,并且'89 标准:
realloc(ptr,0)
的工作方式类似于free(ptr)
。 上面的 Mac OS 手册页和 Jonathan 引用的标准不太清楚,但似乎留下了打破等价性的空间。我一直想知道为什么会有这种差异:“表现得像自由”对我来说似乎很自然。 我有权访问的两种实现都包含一些环境变量驱动的可调性,但 BSD 版本接受更多选项一些示例:
和
在正常模式下,“最小尺寸的对象”可能什么都不是(即相当于
free
),而是带有一些防护装置的某物。 接受它的价值。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.That is, if
ptr
pointed at a substantial object, you'll get back most of its memory.The man page on Debian Lenny agrees with Mitch and Jonathan...does BSD really diverge from Linux on this?
From the offending man page:
The linux and solaris man pages are very clean, and the '89 standard:
realloc(ptr,0)
works likefree(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.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:
and
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.