C语言 realloc 会覆盖旧内容吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3850749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:36:08  来源:igfitidea点击:

Does realloc overwrite old contents?

crealloc

提问by fuddin

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.

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

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

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

回答by pmg

Don't worry about the old contents.

不要担心旧内容。

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

正确的使用方法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 */

回答by ChrisW

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.

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

回答by jhabbott

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.

您应该像覆盖旧指针一样进行编程,是的。旧内存不再分配,因此可以由程序的另一部分(例如系统线程)重新分配,并在调用 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.

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

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

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

  • 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.

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

回答by R.. GitHub STOP HELPING ICE

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 reallocreturned the same pointer you passed to it, any use of the old pointer is undefined behavior.

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

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).

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