简体   繁体   English

为数组重新分配内存c ++

[英]reallocating memory for an array c++

I was looking on C++ reference and found the dynamic memory management and was looking at the function that reallocate memory and I was wondering if this function could be used to make an array bigger I think that is what they mean but I really don't understand this line 我在查看C ++参考并找到了动态内存管理,并在查看重新分配内存的函数,我想知道该函数是否可以用于使数组更大,我想那是他们的意思,但我真的不明白这条线

more_numbers = (int*) realloc (numbers, count * sizeof(int));

this appears in the example on cplusplus reference so here is my question in a nutshell 这出现在cplusplus参考上的示例中,因此这是我的问题

can realloc() be used to make an array bigger? 可以使用realloc()来使数组变大吗? thanks :) 谢谢 :)

Only if the memory for the array was allocated via malloc() , calloc() , or realloc() earlier. 仅当数组的内存是通过malloc()calloc()realloc()分配的。 Otherwise you're asking for trouble. 否则,您会遇到麻烦。

"The function may move the memory block to a new location, in which case the new location is returned". “该功能可以将存储块移动到新位置,在这种情况下,将返回新位置”。 Even though it preserves the data in your block, any pointers to the objects inside this block may become invalid. 即使将数据保留在块中,指向该块内部对象的任何指针也可能变为无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM