在C ++中使用malloc? [英] Using malloc in C++?

查看:441
本文介绍了在C ++中使用malloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Bjarne Stroustrup中读过,使用malloc和free应该在C ++中避免使用
,因为它们处理的是未初始化的内存,而一个

应该使用new和delete。


但为什么会出现问题呢?我不明白为什么使用malloc代替新的

不能给出相同的结果。

I have read in Bjarne Stroustrup that using malloc and free should be
avoided in C++ because they deal with uninitialized memory and one
should instead use new and delete.

But why is that a problem? I cannot see why using malloc instead of new
does not give the same result.

推荐答案

" desktop" < ff*@sss.com写信息

news:f4 ********** @ news.net.uni-c.dk ...
"desktop" <ff*@sss.comwrote in message
news:f4**********@news.net.uni-c.dk...

>我在Bjarne Stroustrup中读过,在C ++中应该避免使用malloc和free,因为它们处理未初始化的内存而应该使用new和delete 。


但为什么会出现问题呢?我不明白为什么使用malloc代替新的

不会给出相同的结果。
>I have read in Bjarne Stroustrup that using malloc and free should be
avoided in C++ because they deal with uninitialized memory and one should
instead use new and delete.

But why is that a problem? I cannot see why using malloc instead of new
does not give the same result.



(未经测试的代码)


class Foo

{

int MyInt;

std :: string MyString;

};


int main()

{

Foo * Bar = new Foo;

Foo * Screwed = malloc(sizeof(Foo));

}


New将调用std :: string的构造函数。 malloc不会。

(Untested code)

class Foo
{
int MyInt;
std::string MyString;
};

int main()
{
Foo* Bar = new Foo;
Foo* Screwed = malloc( sizeof( Foo ) );
}

New will call the constructor for the std::string. malloc won''t.




desktop写道:

desktop wrote:

我有在Bjarne Stroustrup中读到使用malloc和free应该在C ++中避免使用
,因为它们处理的是未初始化的内存,而一个

应该使用new和delete。


但为什么会出现问题呢?
I have read in Bjarne Stroustrup that using malloc and free should be
avoided in C++ because they deal with uninitialized memory and one
should instead use new and delete.

But why is that a problem?



当你处理C ++中的对象分配问题时,

new不仅会分配内存而且还会带来构造函数。

The problem comes when you deal with object allocation, in C++, where
new not only allocates memory but also takes the constructor in hand.


>我看不出为什么使用malloc代替新的

不能给出相同的结果。
>I cannot see why using malloc instead of new
does not give the same result.



为了更好地理解,请通过以下链接:
http://wiki.answers.com/Q/What_is_th...er_than_syntax

问候,

ar


有些事情已知且事情未知,介于两者之间。

For better understanding go through this link :
http://wiki.answers.com/Q/What_is_th...er_than_syntax
Regards,
ar

"There are things known and things unknown, in between are The Doors."


desktop写道:
desktop wrote:

我在Bjarne Stroustrup中读过,使用malloc和free应该在C ++中避免使用
,因为它们处理的是未初始化的内存和一个/>
应改为使用new和delete。


但为什么会出现问题呢?我不明白为什么使用malloc代替新的

不会给出相同的结果。
I have read in Bjarne Stroustrup that using malloc and free should be
avoided in C++ because they deal with uninitialized memory and one
should instead use new and delete.

But why is that a problem? I cannot see why using malloc instead of new
does not give the same result.



对于一桶字节或C样式结构,使用malloc / free或

new / delete几乎没有区别。对于带有构造函数的对象

和/或析构函数,你必须使用new / delete,所以你也可以使用

对所有的分配都一样。


-

Ian Collins。

For a bucket of bytes or a C style struct, using malloc/free or
new/delete makes little or no difference. For objects with constructors
and/or destructors you have to use new/delete, so you may as well use
the same for all allocations.

--
Ian Collins.


这篇关于在C ++中使用malloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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