重新分配C ++数组的内存. [英] Reallocating memory of a C++ array.

查看:275
本文介绍了重新分配C ++数组的内存.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在C ++中realloc?

Possible Duplicate:
How do you realloc in C++?

我知道,如果已经通过malloc()calloc()分配了内存,则可以使用realloc()重新分配(扩展)C ++数组.我的问题是,如何在C ++中扩展通过new运算符分配了内存的数组?

I know that C++ arrays can be reallocated (expanded) using realloc() if memory has been allocated via malloc() or calloc(). My question is, how can I expand an array in C++ whose memory has been allocated via the new operator?

推荐答案

您不能-这就是在C ++中使用std::vector<>的原因.

You can't - that's why in C++ you use std::vector<>.

如果要执行此操作,则必须分配一个新数组(通过new),然后在(例如std::copy)之间复制旧项,然后在以前的数组中delete[]进行复制.

If you wanted to do this, you'd have to allocate a new array (via new), then copy the old items across (std::copy for example), then delete[] the previous array.

只需使用std::vector-让它为您做所有的事情...

Just use std::vector - let it do all that stuff for you...

这篇关于重新分配C ++数组的内存.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆