Why should I avoid using malloc in c++?

Possible Duplicates: What is the difference between new/delete and malloc/free? In what cases do I use malloc vs new? Why should I avoid using malloc in c++? Because malloc does not call the constructor of newly allocated objects. Consider: class Foo { public: Foo() { /* some non-trivial construction process */ } void Bar() { /* does something on Foo's instance variables */ } }

为什么我应该避免在c ++中使用malloc?

可能重复: new / delete和malloc / free有什么区别? 我在哪些情况下使用malloc vs new? 为什么我应该避免在c ++中使用malloc ? 因为malloc不调用新分配的对象的构造函数。 考虑: class Foo { public: Foo() { /* some non-trivial construction process */ } void Bar() { /* does something on Foo's instance variables */ } }; // Creates an array big enough to hold 42 Foo instances, then calls

Differences between `malloc` and `new`

Possible Duplicate: What is the difference between new/delete and malloc/free? Could someone please revise/edit the below - the differences between malloc and new - and see if everything is correct, or am I missing something or got something wrong? Thank you, Both malloc and new are used for Dynamic Memory Allocation. malloc is a C function, whereas new is a C++ operator. malloc requir

`malloc`和`new`之间的区别

可能重复: new / delete和malloc / free有什么区别? 有人可以修改/编辑下面的内容 - malloc和new之间的区别 - 看看是否一切正确,或者我错过了什么或者出了什么问题? 谢谢, malloc和new都用于动态内存分配。 malloc是一个C函数,而new是一个C ++操作符。 malloc在动态分配内存时需要特殊的类型转换,而new不需要任何类型转换。 每当我们使用new来分配内存时,它也会调用任何需要的构造函数,而malloc不会那样

Confusing with delete and free function in C++

Possible Duplicate: What is the difference between new/delete and malloc/free? class Foo { public: Foo() { x = new int; } ~Foo() { delete x; } private: int *x; }; Foo *p = new Foo[10]; free ( p ); I am getting confuse with the above code. Is there any problem about it? When you call free() for an object allocated with new , its destructor is not calle

用C ++中的删除和自由功能混淆

可能重复: new / delete和malloc / free有什么区别? class Foo { public: Foo() { x = new int; } ~Foo() { delete x; } private: int *x; }; Foo *p = new Foo[10]; free ( p ); 我对上面的代码感到困惑。 这有什么问题吗? 当你为一个分配了new的对象调用free() ,它的析构函数没有被调用,所以在这个例子中你会得到内存泄漏。 另外,在这个例子中,你必须使用delete[]因为

When to use Malloc instead of New

Duplicate of: In what cases do I use malloc vs new? Just re-reading this question: What is the difference between "new" and "malloc" and "calloc" in C++? I checked the answers but nobody answered the question: When would I use malloc instead of new? There are a couple of reasons (I can think of two). Let the best float to the top. A couple that spring t

何时使用Malloc而不是New

重复:我在哪些情况下使用malloc vs new? 只要重新阅读这个问题: C ++中的“new”和“malloc”和“calloc”有什么区别? 我检查了答案,但没有人回答这个问题: 我什么时候可以使用malloc而不是新的? 有几个原因(我能想到两个)。 让最好的浮动到顶部。 想起一对夫妇: 当你需要在C ++和C之间移植代码时 当您在可能从C调用的库中分配内存时,C代码必须释放分配。 从我发布在该主题上的new/malloc的Stroustrup

C++: is malloc equivalent to new?

Possible Duplicate: What is the difference between new/delete and malloc/free? Hi guys i am a noob in c++, want to know whether memblock = (char *)malloc( currentByteLength); is equivalent to memblock = new char[currentByteLength] in c++. memblock = (char *)malloc( currentByteLength); memblock = new char[currentByteLength]; No difference now. But if you replace char with int , then

C ++:是malloc等同于新的?

可能重复: new / delete和malloc / free有什么区别? 嗨,大家好,我是c ++的noob,想知道是否 memblock = (char *)malloc( currentByteLength); 相当于 memblock = new char[currentByteLength] 在c ++中。 memblock = (char *)malloc( currentByteLength); memblock = new char[currentByteLength]; 现在没有区别。 但是如果你用int替换char ,那么是的,会有区别,因为在这种情况下, malloc会分配currentByte

What does "Memory allocated at compile time" really mean?

In programming languages like C and C++, people often refer to static and dynamic memory allocation. I understand the concept but the phrase "All memory was allocated (reserved) during compile time" always confuses me. Compilation, as I understand it, converts high level C/C++ code to machine language and outputs an executable file. How is memory "allocated" in a compiled

“在编译时分配的内存”真的意味着什么?

在像C和C ++这样的编程语言中,人们通常会引用静态和动态内存分配。 我理解这个概念,但是“在编译期间分配(保留)所有内存”这句话总是令我困惑。 据我了解,编译将高级C / C ++代码转换为机器语言并输出可执行文件。 在编译的文件中如何“分配”内存? 是不是所有的虚拟内存管理内存总是分配在内存中? 按定义,内存分配是不是运行时概念? 如果我在C / C ++代码中创建一个1KB静态分配的变量,是否会增加相同数量的可执

Difference between 'new operator' and 'operator new'?

“新操作员”和“操作员新”有什么区别? I usually try to phrase things differently to differentiate between the two a bit better, but it's a good question in any case. Operator new is a function that allocates raw memory -- at least conceptually, it's not much different from malloc() . Though it's fairly unusual unless you're writing something like your own container, you can call

'新运营商'和'运营商新'之间的区别?

“新操作员”和“操作员新”有什么区别? 我通常试图用不同的方式来区分这两者,但在任何情况下都是一个很好的问题。 operator new是一个分配原始内存的函数 - 至少在概念上,它与malloc()没有多大区别。 虽然这很不寻常,除非你写的东西像你自己的容器,你可以直接调用operator new,如: char *x = static_cast<char *>(operator new(100)); 也可以在全局或特定的类中重载运算符new。 IIRC,签名是: void *operator

Why does the use of 'new' cause memory leaks?

I learned C# first, and now I'm starting with C++. As I understand, operator new in C++ is not similar to the one in C#. Can you explain the reason of the memory leak in this sample code? class A { ... }; struct B { ... }; A *object1 = new A(); B object2 = *(new B()); What is happening When you write T t; you're creating an object of type T with automatic storage duration. It wi

为什么使用'new'导致内存泄漏?

我首先学习了C#,现在我开始使用C ++。 据我所知,C ++中的new操作符与C#中的操作符不同。 你能解释这个示例代码中内存泄漏的原因吗? class A { ... }; struct B { ... }; A *object1 = new A(); B object2 = *(new B()); 发生什么事 当你写T t; 你正在创建一个T类型的对象,并自动存储持续时间。 当它超出范围时它会自动清理。 当你写new T()你正在创建一个具有动态存储持续时间的T型对象。 它不会自动清理。

Malloc vs New for Primitives

I understand the benefits of using new against malloc in C++. But for specific cases such as primitive data types (non array) - int , float etc., is it faster to use malloc than new ? Although, it is always advisable to use new even for primitives, if we are allocating an array so that we can use delete[] . But for non-array allocation, I think there wouldn't be any constructor call for

马洛克vs新的原始人

我明白在C ++中使用new对malloc的好处。 但对于诸如基本数据类型(非数组)的特定情况( int , float等),使用malloc比使用new更快吗? 尽管如此,即使对基元使用new也是可取的,如果我们正在分配一个数组,以便我们可以使用delete[] 。 但对于非数组分配,我认为不会有任何构造函数调用int ? 因为, new运算符分配内存,检查它是否被分配,然后调用构造函数。 但是对于原语非数组堆分配,使用malloc比new更好吗?

How to initialise memory with new operator in C++?

I'm just beginning to get into C++ and I want to pick up some good habits. If I have just allocated an array of type int with the new operator, how can I initialise them all to 0 without looping through them all myself? Should I just use memset ? Is there a “C++” way to do it? It's a surprisingly little-known feature of C++ (as evidenced by the fact that no-one has given this as an a

如何用C ++中的新运算符初始化内存?

我刚刚开始进入C ++,我想要选择一些良好的习惯。 如果我刚刚为new运算符分配了一个int类型的数组,我怎样才能将它们全部初始化为0而无需自己循环遍历它们? 我应该只使用memset吗? 有没有“C ++”的方式来做到这一点? 这是C ++的一个令人惊讶的鲜为人知的特性(就像没有人给出这个答案一样),但它实际上有特殊的语法来默认初始化一个数组(在技术上,它被称为“value-初始化“): new int[10](); 请注意,您必须使用空括