realloc函数,适用于使用new而不是realloc分配的内存

时间:2020-12-17 21:18:23

I'm aware that there is a realloc function that would allow me to resize the memory block (and it's paired with a free function). However, I'm trying to do the same to a c++ class with some member pointers allocated memory using new instead of realloc. Is there an equivalent keyword to realloc in c++ that would allow me to achieve the same goal when the memory is paired using new/delete rather than malloc or realloc and free?

我知道有一个realloc函数可以让我调整内存块的大小(并且它与一个*函数配对)。但是,我正在尝试对c ++类执行相同操作,其中一些成员指针使用new而不是realloc分配内存。在c ++中是否有一个等效的关键字重新分配,当内存使用new / delete而不是malloc或realloc和free进行配对时,我可以实现相同的目标?

Thanks in advance.

提前致谢。

5 个解决方案

#1


No, there isn't. And frankly, if you are using new or new[]. your C++ code is probably not well designed. Look at using std::vector instead of new[], and at using values instead of new.

不,没有。坦率地说,如果你使用新的或新的[]。您的C ++代码可能设计得不好。看看使用std :: vector而不是new [],并使用值而不是new。

#2


I am not aware of if there is one or not, but you could probably use a vector as you are just resizing an array of elements :)

我不知道是否有一个,但你可能使用一个向量,因为你只是调整元素数组:)

std::vector<char> iVector(1000); // 1000 element as initial size

iVector.resize(2500); // resize the vector to 2500 elements

// or just use it without worrying about memory management

#3


If I understand your problem, you have a class which allocate some variable-length memory. There is two possible cases:

如果我理解你的问题,你有一个分配一些可变长度内存的类。有两种可能的情况:

The memory contains C++ objects

You have no choice: The memory should be, directly or indirectly allocated with new because you need the constructors called.

你别无选择:内存应该直接或间接地分配new,因为你需要调用的构造函数。

If you want to have a realloc-like behaviour, then do not use new[]. Use instead a std::vector, which will handle all the allocation/reallocation/Free and construction/destruction correctly.

如果您希望具有类似realloc的行为,则不要使用new []。使用std :: vector,它将正确处理所有分配/重新分配/*和构造/破坏。

The memory is raw

You could either use new[] or malloc, because you are allocating PODs (an array of ints, or shorts, dumb structures, etc.).

您可以使用new []或malloc,因为您正在分配POD(一组int,或short,哑结构等)。

But again, new[] won't offer you a realloc-like behaviour... But if you start using malloc, then you must do housekeeping, i.e. be sure to call free at the right time, memorize the size of the array somewhere, and perhaps even have a size different from the array capacity (i.e. you allocate more, to avoid doing too much reallocs).

但同样,new []不会为你提供类似realloc的行为......但是如果你开始使用malloc,那么你必须做家务,即确保在正确的时间免费调用,记住某个地方的数组大小,甚至可能有一个不同于数组容量的大小(即你分配更多,以避免做太多的reallocs)。

And you know what? std::vector already do all this for you.

你知道吗? std :: vector已经为你做了这一切。

Conclusion

You are coding in C++, then the solution to your problem (i.e. allocating variable length memory inside a class), as already expressed by previous answers, is use std::vector.

您正在使用C ++进行编码,然后解决您的问题(即在类中分配可变长度内存),如前面的答案所表达的那样,是使用std :: vector。

#4


Take a look at how vector uses placement new to initialize, copy (move) and destruct objects in-place in memory that is in principle 'realloc'-ated. There is a fair amount of work in implementing something like vector, so I'd say simply use vector... It uses one contiguous memory block for all the objects, so you can use it with functions that expect arrays. And, it implicitly keeps track of the count of objects for you...

看一下vector如何使用placement new来初始化,复制(移动)和破坏内存中的对象,原则上是'realloc'-ated。在实现像vector这样的东西方面有相当多的工作,所以我只想使用vector ...它为所有对象使用一个连续的内存块,因此你可以将它用于期望数组的函数。并且,它隐含地跟踪对象的数量......

Of course, this convenience comes with a cost - it allocates memory on the heap behind the scenes. So if you absolutely know that you only need a reasonably sized array then an array on the stack can not be beaten for speed.

当然,这种便利带来了成本 - 它在幕后的堆上分配内存。因此,如果你完全知道你只需要一个合理大小的数组,那么堆栈上的数组就无法获得速度。

BR Per

#5


No. Just use malloc/realloc/free, what's problem?

不。只需使用malloc / realloc / free,有什么问题?

#1


No, there isn't. And frankly, if you are using new or new[]. your C++ code is probably not well designed. Look at using std::vector instead of new[], and at using values instead of new.

不,没有。坦率地说,如果你使用新的或新的[]。您的C ++代码可能设计得不好。看看使用std :: vector而不是new [],并使用值而不是new。

#2


I am not aware of if there is one or not, but you could probably use a vector as you are just resizing an array of elements :)

我不知道是否有一个,但你可能使用一个向量,因为你只是调整元素数组:)

std::vector<char> iVector(1000); // 1000 element as initial size

iVector.resize(2500); // resize the vector to 2500 elements

// or just use it without worrying about memory management

#3


If I understand your problem, you have a class which allocate some variable-length memory. There is two possible cases:

如果我理解你的问题,你有一个分配一些可变长度内存的类。有两种可能的情况:

The memory contains C++ objects

You have no choice: The memory should be, directly or indirectly allocated with new because you need the constructors called.

你别无选择:内存应该直接或间接地分配new,因为你需要调用的构造函数。

If you want to have a realloc-like behaviour, then do not use new[]. Use instead a std::vector, which will handle all the allocation/reallocation/Free and construction/destruction correctly.

如果您希望具有类似realloc的行为,则不要使用new []。使用std :: vector,它将正确处理所有分配/重新分配/*和构造/破坏。

The memory is raw

You could either use new[] or malloc, because you are allocating PODs (an array of ints, or shorts, dumb structures, etc.).

您可以使用new []或malloc,因为您正在分配POD(一组int,或short,哑结构等)。

But again, new[] won't offer you a realloc-like behaviour... But if you start using malloc, then you must do housekeeping, i.e. be sure to call free at the right time, memorize the size of the array somewhere, and perhaps even have a size different from the array capacity (i.e. you allocate more, to avoid doing too much reallocs).

但同样,new []不会为你提供类似realloc的行为......但是如果你开始使用malloc,那么你必须做家务,即确保在正确的时间免费调用,记住某个地方的数组大小,甚至可能有一个不同于数组容量的大小(即你分配更多,以避免做太多的reallocs)。

And you know what? std::vector already do all this for you.

你知道吗? std :: vector已经为你做了这一切。

Conclusion

You are coding in C++, then the solution to your problem (i.e. allocating variable length memory inside a class), as already expressed by previous answers, is use std::vector.

您正在使用C ++进行编码,然后解决您的问题(即在类中分配可变长度内存),如前面的答案所表达的那样,是使用std :: vector。

#4


Take a look at how vector uses placement new to initialize, copy (move) and destruct objects in-place in memory that is in principle 'realloc'-ated. There is a fair amount of work in implementing something like vector, so I'd say simply use vector... It uses one contiguous memory block for all the objects, so you can use it with functions that expect arrays. And, it implicitly keeps track of the count of objects for you...

看一下vector如何使用placement new来初始化,复制(移动)和破坏内存中的对象,原则上是'realloc'-ated。在实现像vector这样的东西方面有相当多的工作,所以我只想使用vector ...它为所有对象使用一个连续的内存块,因此你可以将它用于期望数组的函数。并且,它隐含地跟踪对象的数量......

Of course, this convenience comes with a cost - it allocates memory on the heap behind the scenes. So if you absolutely know that you only need a reasonably sized array then an array on the stack can not be beaten for speed.

当然,这种便利带来了成本 - 它在幕后的堆上分配内存。因此,如果你完全知道你只需要一个合理大小的数组,那么堆栈上的数组就无法获得速度。

BR Per

#5


No. Just use malloc/realloc/free, what's problem?

不。只需使用malloc / realloc / free,有什么问题?