realloc会覆盖旧内容吗?

发布于 2024-09-25 21:50:17 字数 121 浏览 0 评论 0原文

当我们通过realloc()重新分配内存时,之前的内容会被覆盖吗?我正在尝试制作一个程序,每次我们将数据输入其中时都会重新分配内存。

请告诉我有关通过 realloc 进行内存分配的信息,例如它是否依赖于编译器?

When we reallocate memory via realloc(), are the previous contents over-written? I am trying to make a program which reallocates memory each time we enter the data into it.

Please tell me about memory allocation via realloc, is it compiler dependent for example?

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

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

发布评论

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

评论(4

月牙弯弯 2024-10-02 21:50:17

不用担心旧内容。

使用realloc的正确方法是使用特定的指针进行重新分配,测试该指针,如果一切正常,则更改旧指针

int *oldpointer = malloc(100);

/* ... */

int *newpointer = realloc(oldpointer, 1000);
if (newpointer == NULL) {
    /* problems!!!!                                 */
    /* tell the user to stop playing DOOM and retry */
    /* or free(oldpointer) and abort, or whatever   */
} else {
    /* everything ok                                                                 */
    /* `newpointer` now points to a new memory block with the contents of oldpointer */
    /* `oldpointer` points to an invalid address                                     */
    oldpointer = newpointer;
    /* oldpointer points to the correct address                                */
    /* the contents at oldpointer have been copied while realloc did its thing */
    /* if the new size is smaller than the old size, some data was lost        */
}

/* ... */

/* don't forget to `free(oldpointer);` at some time */

Don't worry about the old contents.

The correct way to use realloc is to use a specific pointer for the reallocation, test that pointer and, if everything worked out ok, change the old pointer

int *oldpointer = malloc(100);

/* ... */

int *newpointer = realloc(oldpointer, 1000);
if (newpointer == NULL) {
    /* problems!!!!                                 */
    /* tell the user to stop playing DOOM and retry */
    /* or free(oldpointer) and abort, or whatever   */
} else {
    /* everything ok                                                                 */
    /* `newpointer` now points to a new memory block with the contents of oldpointer */
    /* `oldpointer` points to an invalid address                                     */
    oldpointer = newpointer;
    /* oldpointer points to the correct address                                */
    /* the contents at oldpointer have been copied while realloc did its thing */
    /* if the new size is smaller than the old size, some data was lost        */
}

/* ... */

/* don't forget to `free(oldpointer);` at some time */
慕巷 2024-10-02 21:50:17

它会增长已分配的内存而不覆盖现有内容,或者(如果无法增长)它会在不同位置分配新的更大内存,并将现有内容从以前的内存复制到新内存中。

It grows already-allocated memory without overwriting existing content, or (if it's unable to grow) it allocates new larger memory at a different location and copies existing contents from previous memory into new memory.

九公里浅绿 2024-10-02 21:50:17

您应该像旧指针被覆盖一样进行编程,是的。旧内存不再分配,因此可以由程序的另一部分(例如系统线程)重新分配,并在调用 realloc 之后随时重写。

新内存将始终包含旧内存中存在的相同数据(如果需要,会为您复制),但仅限于旧块的大小,最后分配的任何额外空间都将不会被初始化。

如果你想要一个副本,那么做一个新的 malloc 并使用 memcpy。

在实现方面,当您调用 realloc 来增加大小时,可能会发生以下情况之一:

  • 分配新块并复制旧内存的内容、释放旧块、释放新指针被返回。
  • 如果块之后的区域未分配,则可以扩展现有块并返回相同的指针。

由于您无法知道发生了什么,或者即使使用了与上面建议的完全不同的实现,您也应该始终根据 realloc 的规范进行编码,即您不能再使用旧指针,并且您必须使用新的。

You should program as if the old pointer is overwritten, yes. The old memory is no longer allocated so can be re-allocated by another part of your program (or a system thread for example) and written over at any time after you call realloc.

The new memory will always contain the same data that was present in the old memory though (it is copied for you if necessary), but only up to the size of the old block, any extra space allocated at the end will be uninitialised.

If you want a copy then do a new malloc and use memcpy.

Implementation-wise, when you call realloc to increase the size, one of these things might happen:

  • A new block is allocated and the contents of the old memory copied, the old block is freed, the new pointer is returned.
  • If the area after the block is not allocated, the existing block may be extended and the same pointer returned.

Since you have no way of knowing which has happened, or even if a completely different implementation to that suggested above is used, you should always code according to the spec of realloc, which is that you must not use the old pointer any more and you must use the new one.

笑饮青盏花 2024-10-02 21:50:17

很难说你在问什么,但如果你问是否可以在传递给 realloc 的旧地址处读取“旧内容”,答案是。在某些情况下,您可能会在那里找到部分或全部旧内容,但除非 realloc 返回您传递给它的相同指针,否则对旧指针的任何使用都是未定义行为

如果您只是询问旧内容是否会保留在 realloc 返回的新地址中,答案是肯定的(最多为旧大小和新大小中的最小值)。

It's hard to tell what you're asking, but if you're asking whether you can read the "old contents" at the old address passed to realloc, the answer is no. In some cases, you may find part or all of the old contents there, but unless realloc returned the same pointer you passed to it, any use of the old pointer is undefined behavior.

If you're merely asking whether the old contents will be preserved at the new address returned by realloc, the answer is yes (up to the minimum of the old size and the new size).

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