什么是C ++中realloc的最佳替代方案 [英] What is the best alternative for realloc in C++

查看:136
本文介绍了什么是C ++中realloc的最佳替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以告诉我C ++中realloc的最佳选择是什么?

使用new运算符realloc分配内存是否安全? ?

解决方案

Kumar写道:


有人可以告诉我什么是在C ++中realloc的最佳替代方案?


您需要什么?

重新分配使用新运算符分配的内存是否安全?




不能。你只能在从malloc或

realloc获得的内存上使用realloc。此外,realloc无法正常使用非pod对象,因为

它并不关心构造函数和析构函数。

Kumar写道:


有人可以告诉我什么是C ++中realloc的最佳选择?


您需要什么?

重新分配使用新运算符分配的内存是否安全?




不能。你只能在从malloc或

realloc获得的内存上使用realloc。此外,realloc无法正常使用非pod对象,因为

它并不关心构造函数和析构函数。

2004年4月6日02:12:33 -0700 comp.lang.c ++, rs ****** @ acmet.com

(Kumar)写道,


有人可以告诉我什么是C ++中realloc的最佳选择?


std :: vector<> :: resize()

重新分配使用new运算符分配的内存是否安全?



不要使用''new''。


Hi,
Can somebody advise me on what is the best alternative for realloc in C++?
Is it safe to "realloc"ate memory allocated using new operator ?

解决方案

Kumar wrote:

Hi,
Can somebody advise me on what is the best alternative for realloc in
C++?
What would you need it for?
Is it safe to "realloc"ate memory allocated using new operator ?



No. You can only use realloc on memory that you got from malloc or
realloc. Also, realloc won''t work properly with non-pod objects, since
it doesn''t care about constructors and destructors.


Kumar wrote:

Hi,
Can somebody advise me on what is the best alternative for realloc in
C++?
What would you need it for?
Is it safe to "realloc"ate memory allocated using new operator ?



No. You can only use realloc on memory that you got from malloc or
realloc. Also, realloc won''t work properly with non-pod objects, since
it doesn''t care about constructors and destructors.


On 6 Apr 2004 02:12:33 -0700 in comp.lang.c++, rs******@acmet.com
(Kumar) wrote,

Hi,
Can somebody advise me on what is the best alternative for realloc in C++?
std::vector<>::resize()
Is it safe to "realloc"ate memory allocated using new operator ?



Don''t use ''new''.


这篇关于什么是C ++中realloc的最佳替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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