我看到在c++中有多种方法来分配和释放数据,我明白,当你调用malloc时你应该叫自由,当你使用新的操作符应与删除和对它是错误的把两个调用free()(例如,创建新的操作符),但是我不清楚我应该使用malloc / free,当我在现实世界中应该使用新的/删除程序。

如果你是c++专家,请告诉我你在这方面遵循的任何经验法则或惯例。


当前回答

只有当对象的生命周期与创建它的作用域不同时才需要动态分配(这也适用于使作用域变小或变大),并且您有特定的原因按值存储它不起作用。

例如:

 std::vector<int> *createVector(); // Bad
 std::vector<int> createVector();  // Good

 auto v = new std::vector<int>(); // Bad
 auto result = calculate(/*optional output = */ v);
 auto v = std::vector<int>(); // Good
 auto result = calculate(/*optional output = */ &v);

从c++ 11开始,我们有std::unique_ptr来处理已分配的内存,它包含已分配内存的所有权。Std::shared_ptr是为必须共享所有权而创建的。(你需要的比你在一个好的程序中期望的要少)

创建一个实例变得非常简单:

auto instance = std::make_unique<Class>(/*args*/); // C++14
auto instance = std::unique_ptr<Class>(new Class(/*args*/)); // C++11
auto instance = std::make_unique<Class[]>(42); // C++14
auto instance = std::unique_ptr<Class[]>(new Class[](42)); // C++11

c++ 17还增加了std::optional,这可以防止你需要内存分配

auto optInstance = std::optional<Class>{};
if (condition)
    optInstance = Class{};

一旦'instance'超出作用域,内存就会被清理。转让所有权也很容易:

 auto vector = std::vector<std::unique_ptr<Interface>>{};
 auto instance = std::make_unique<Class>();
 vector.push_back(std::move(instance)); // std::move -> transfer (most of the time)

那你什么时候还需要新的?从c++ 11开始几乎没有。大多数情况下你使用std::make_unique,直到你遇到一个通过原始指针转移所有权的API。

 auto instance = std::make_unique<Class>();
 legacyFunction(instance.release()); // Ownership being transferred

 auto instance = std::unique_ptr<Class>{legacyFunction()}; // Ownership being captured in unique_ptr

在c++ 98/03中,您必须进行手动内存管理。如果您是这种情况,请尝试升级到标准的最新版本。如果你被卡住了:

 auto instance = new Class(); // Allocate memory
 delete instance;             // Deallocate
 auto instances = new Class[42](); // Allocate memory
 delete[] instances;               // Deallocate

确保正确跟踪所有权,以免出现内存泄漏!Move语义也不能工作。

那么,在c++中什么时候需要malloc呢?唯一有效的原因是分配内存并在以后通过放置new初始化它。

 auto instanceBlob = std::malloc(sizeof(Class)); // Allocate memory
 auto instance = new(instanceBlob)Class{}; // Initialize via constructor
 instance.~Class(); // Destroy via destructor
 std::free(instanceBlob); // Deallocate the memory

尽管如此,上面的操作是有效的,这也可以通过new操作符来完成。vector就是一个很好的例子。

最后,我们仍然有一个房间里的大象:C。如果你必须使用一个在c++代码中分配内存并在C代码中释放内存的C库(或者相反),你就被迫使用malloc/free。

如果你是在这种情况下,忘记虚函数、成员函数、类……只有包含pod的结构才被允许。

规则的一些例外情况:

您正在编写一个具有高级数据结构的标准库,其中malloc是合适的 你必须分配大量的内存(在10GB文件的内存副本中?) 您拥有阻止您使用某些构造的工具 您需要存储不完整的类型

其他回答

我以前玩过很少的计算机图形C/ c++应用程序。 过了这么久,有些东西消失了,我很想念它们。

关键是,malloc和new,或free和delete,可以同时工作, 特别是对于某些基本类型,这是最常见的。

例如,一个char数组可以用malloc或new来分配。 一个主要的区别是,使用new,你可以实例化一个固定的数组大小。

char* pWord = new char[5]; // allocation of char array of fixed size 

在这种情况下,不能使用变量来表示数组的大小。 相反,malloc函数可以允许变量大小。

int size = 5; 
char* pWord = (char*)malloc(size); 

在这种情况下,可能需要一个转换强制转换操作符。 对于malloc返回的类型,它是指向void的指针,而不是char。 有时候编译器不知道如何转换这个类型。

分配内存块后,可以设置变量值。 对于一些较大的数组,memset函数确实会慢一些。 但是在赋值之前,所有的bit必须先设置为0。 因为数组的值可以有任意的内容。

假设,数组被分配给另一个较小的数组。 数组元素的一部分仍然可以有任意内容。 在这种情况下,建议调用memset函数。

memset((void*)pWord, 0, sizeof(pWord) / sizeof(char)); 

分配函数可用于所有C包。 这些是通用函数,必须适用于更多的C类型。 c++库是旧C库的扩展。 因此malloc函数返回一个泛型void*指针。 该结构没有定义new或delete操作符。 在这种情况下,可以使用malloc分配自定义变量。

new和delete关键字实际上是一些定义好的C操作符。 也许一个自定义联合或类可以定义这些操作符。 如果在类中没有定义new和delete,则它们可能不起作用。 但如果一个类是由另一个类派生的,这个类有这些操作符, new和delete关键字可以具有基本的类行为。

关于释放数组,free只能与malloc结合使用。 不能使用malloc分配变量,然后使用delete释放变量。

简单的删除操作符只引用数组的第一项。 因为pWord数组也可以写成:

pWord = &pWord[0]; // or *pWord = pWord[0]; 

当必须删除一个数组时,使用delete[]操作符:

delete[] pWord; 

类型转换并不坏,只是不适用于所有的变量类型。 转换类型转换也是一个必须定义的运算符函数。 如果没有为某个类型定义此操作符,则它可能不起作用。 但并不是所有的错误都是由这个转换强制转换操作符引起的。

另外,在使用自由调用时,必须使用到空指针的类型转换。 这是因为free函数的实参是一个空指针。

free((void*)pWord); 

可能会出现一些错误,因为数组的大小太小。 但这是另一个故事,并不是因为使用了演员阵容。

致以亲切的问候,艾德里安·布里纳斯

在c++中总是使用new。如果你需要一个无类型的内存块,你可以直接使用operator new:

void *p = operator new(size);
   ...
operator delete(p);

来自c++ FQA Lite:

[16.4] Why should I use new instead of trustworthy old malloc()? FAQ: new/delete call the constructor/destructor; new is type safe, malloc is not; new can be overridden by a class. FQA: The virtues of new mentioned by the FAQ are not virtues, because constructors, destructors, and operator overloading are garbage (see what happens when you have no garbage collection?), and the type safety issue is really tiny here (normally you have to cast the void* returned by malloc to the right pointer type to assign it to a typed pointer variable, which may be annoying, but far from "unsafe"). Oh, and using trustworthy old malloc makes it possible to use the equally trustworthy & old realloc. Too bad we don't have a shiny new operator renew or something. Still, new is not bad enough to justify a deviation from the common style used throughout a language, even when the language is C++. In particular, classes with non-trivial constructors will misbehave in fatal ways if you simply malloc the objects. So why not use new throughout the code? People rarely overload operator new, so it probably won't get in your way too much. And if they do overload new, you can always ask them to stop.

对不起,我就是忍不住。:)

只使用malloc和free来分配将由以c为中心的库和api管理的内存。对你控制的所有东西使用new和delete(以及[]变量)。

malloc()在C语言中用于动态分配内存 而在c++中,同样的工作由new()完成。 所以你不能混合两种语言的编码习惯。 如果你问calloc和malloc()之间的差异,那就好了