什么是malloc优于c ++中的新功能。 [英] whats the advantageof malloc over new in c++.

查看:92
本文介绍了什么是malloc优于c ++中的新功能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是malloc的美丽在新之上为什么它有助于

programmer.for自己的内存区域。??

whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??

推荐答案

8月19日,09:52 ,Muzammil< muzammilPeer ... @ gmail.comwrote:
On 19 Aug, 09:52, Muzammil <muzammilPeer...@gmail.comwrote:

什么是malloc的美丽在新之上为什么它有助于

programmer.for *它自己的内存区域。
whats the beauty of "malloc" over "new" why its helpful for
programmer.for *its own memory area.??



malloc()并没有在C ++程序中得到很多使用。

malloc()只是分配内存。 new分配内存

并构造对象(通过调用构造函数)。

Placement new在提供的内存上运行构造函数。


malloc()主要用于历史兼容性

with C. new可以在内部使用malloc()(它没有必要)。


我可以看到,malloc()的主要用途是将内存传递给C函数并将其释放到C函数中。

- -

Nick Keighley

malloc() doesn''t get used in C++ programs very much.
malloc() just allocates memory. new allocates memory
and constructs the object (by calling the constructor).
Placement new runs the constructor on the memory provided.

malloc() is mainly provided for historical compatibility
with C. new may use malloc() internally (it doesn''t have to).

the main use for malloc(), I can see, is if memory is to
be passed to a C function and to be freed by the C function.
--
Nick Keighley


8月19日下午5:57,Nick Keighley< nick_keighley_nos ... @ hotmail.com>

写道:
On Aug 19, 5:57 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:

8月19日,09:52,Muzammil< muzammilPeer ... @ gmail.comwrote:
On 19 Aug, 09:52, Muzammil <muzammilPeer...@gmail.comwrote:

什么是malloc的美丽在新之上为什么它有助于

programmer.for自己的内存区域。
whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??



malloc()并没有在C ++程序中得到太多使用。

malloc()只是分配内存。 new分配内存

并构造对象(通过调用构造函数)。

Placement new在提供的内存上运行构造函数。


malloc()主要用于历史兼容性

with C. new可以在内部使用malloc()(它没有必要)。


我可以看到,malloc()的主要用途是将内存传递给C函数并由C函数释放。


malloc() doesn''t get used in C++ programs very much.
malloc() just allocates memory. new allocates memory
and constructs the object (by calling the constructor).
Placement new runs the constructor on the memory provided.

malloc() is mainly provided for historical compatibility
with C. new may use malloc() internally (it doesn''t have to).

the main use for malloc(), I can see, is if memory is to
be passed to a C function and to be freed by the C function.



+ malloc提供了_may_能够使用

realloc()增长的内存,而无需复制内容


+ new可以被覆盖来做各种各样的事情 - 这通常是好事 - 但是malloc()可能偶尔会有用作为一种方式来获取$>
绕过这个(虽然你通常不能确定正常

malloc函数会收到你的电话 - 有很多种方式

安装替代方案)


+你可以免费使用malloced内存,而不需要使用名为

的析构函数(通常不是一个好主意,但可能允许在某些
用法)


不过,简短的故事是不要使用malloc / realloc并且免费,除非你找到一个理由让到。


Tony

+ malloc provides memory that _may_ be able to be grown using
realloc() without having to copy the content

+ new can be overridden to do all manner of things - which is normally
a good thing - but malloc() might occasionally be useful as a way to
bypass this (though you can''t generally be sure that the "normal"
malloc function will receive your call - there are lots of ways of
installing alternatives)

+ you can free malloced memory without having the destructor called
(not normally a good idea, but may allow optimisations in certain
usages)

Still, the short story is don''t use malloc/realloc and free unless you
find a reason to have to.

Tony


2008-08-19 11:57,到*********** @ yahoo.co.uk 写道:
On 2008-08-19 11:57, to***********@yahoo.co.uk wrote:

8月19日下午5:57,Nick Keighley< nick_keighley_nos ... @ hotmail.com>

写道:
On Aug 19, 5:57 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:

> 8月19日,09:52,Muzammil< muzammilPeer ... @ gmail.comwrote:
>On 19 Aug, 09:52, Muzammil <muzammilPeer...@gmail.comwrote:

什么是美丽的malloc在新之上为什么它有助于

programmer.for自己的内存区域。
whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??


malloc()并没有在C ++程序中得到太多使用。
malloc()只是分配内存。 new分配内存并构造对象(通过调用构造函数)。
Placement new在提供的内存上运行构造函数。

malloc()主要用于历史兼容性<使用C. new可以在内部使用malloc()(它没有必要)。

malloc()的主要用途,我可以看到,如果内存是
被传递给C函数并被C函数释放。


malloc() doesn''t get used in C++ programs very much.
malloc() just allocates memory. new allocates memory
and constructs the object (by calling the constructor).
Placement new runs the constructor on the memory provided.

malloc() is mainly provided for historical compatibility
with C. new may use malloc() internally (it doesn''t have to).

the main use for malloc(), I can see, is if memory is to
be passed to a C function and to be freed by the C function.



+ malloc提供了_may_能够使用

realloc()增长的内存,而无需复制内容


+ malloc provides memory that _may_ be able to be grown using
realloc() without having to copy the content



前提是不必构造内存的内容。


-

Erik Wikstr? ?m

Provided that the content of that memory does not have to be constructed.

--
Erik Wikstr??m


这篇关于什么是malloc优于c ++中的新功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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