cwa*_*ole 16 c++ pointers memory-management reference
我一直在尽力学习C++,但我之前的培训在一个主要问题上不足:内存管理.我的主要语言都有自动垃圾收集,所以跟踪一切从来没有真正必要.我已经尝试过在线阅读C++中的内存管理,但我对此感到怀疑,因为我遗漏了一些东西.
所以,这是一个多部分的问题:
delete
在循环重新迭代之前调用任何新指针.它是否正确?你需要用引用做些什么吗?malloc
free
calloc
realloc
***********************更新*******************
这是为了解释在评论一中对lmgtfy的引用(由Ewan提供).如果您开始阅读那里可用的信息,则对初学者没用.我认为这是一个伟大的理论,但它对这个问题既不相关也不有用.
小智 24
你真的,真的需要阅读一本好书 - 如果没有一本书,坦率地学习C++是不可能的.我推荐使用C++的两个创始人Koenig&Moo的Accelerated C++.
Mar*_*ork 15
_
int* data1 = new int(5);
delete data1;
int* data2 = new int[5];
delete [] data2;
Run Code Online (Sandbox Code Playgroud)
从析构函数中抛出异常
动态分配对象数组
在构造函数中创建模式名称,在析构函数中删除(C++)
// Every new is matched by a delete.
for(int loop = 0;loop < 10;++loop)
{
data = new int(5);
}
delete data;
// The problem is that every 'use of' new is not matched by a delete.
// Here we allocate 10 integers but only release the last one.
Run Code Online (Sandbox Code Playgroud)
class MyArray
{
// Use RAII to manage the dynamic array in an exception safe manor.
public:
MyArray(int size)
:data( new int[size])
{}
~MyArray()
{
delete [] data;
}
// PROBLEM:
// Ignored the rule of 4.
// The compiler will generate a copy constructor and assignment operator.
// These default compiler generated methods just copy the pointer. This will
// lead to double deletes on the memory.
private:
int* data;
};
Run Code Online (Sandbox Code Playgroud)
// Understand what the properties of the smart pointers are:
//
std::vector<std::auto_ptr<int> > data;
// Will not work. You can't put auto_ptr into a standard container.
// This is because it uses move semantics not copy semantics.
Run Code Online (Sandbox Code Playgroud)
// Gurantee that exceptions don't screw up your object:
//
class MyArray
{
// ... As Above: Plus
void resize(int newSize)
{
delete [] data;
data = new int[newSize];
// What happens if this new throws (because there is not enough memory)?
// You have deleted the old data so the old data so it points at invalid memory.
// The exception will leave the object in a completely invalid state
}
Run Code Online (Sandbox Code Playgroud)
RC.*_*RC. 14
从最简单的意义上说,您需要了解的内存管理是您需要删除在堆上分配的内存.所以当创建一个像MyClass *myClass = new MyClass(x);
你需要在你的代码中有一些位置的对象时 ,可以用相应的东西释放/删除它delete
.这在实践中看起来很容易,但如果没有适当的设计和辅助对象(如共享指针)的使用,这可能会很快变得混乱,尤其是在维护代码和添加功能时.例如,这是一个典型的内存泄漏:
try
{
MyClass *myClass = new MyClass(x);
// Do some stuff can throw an exception
delete myClass;
}
catch(...)
{
// Memory leak on exceptions. Delete is never called
}
Run Code Online (Sandbox Code Playgroud)
或者另一个大内存管理问题是调用错误的删除类型:
int* set = new int[100];
delete set; // Incorrect - Undefined behavior
// delete [] set; is the proper way to delete an array of pointers
Run Code Online (Sandbox Code Playgroud)
帮助自己的常用方法是使用RAII习语.(资源分配是初始化)
以下是使用std库以防止内存泄漏的示例:
try
{
auto_ptr<MyClass> myClass(new MyClass(x));
// Now the heap allocated memory associated with myClass
// will automatically be destroyed when it goes out of scope,
// but you can use it much like a regular pointer
myClass->memberFunction();
}
catch (...)
{
}
Run Code Online (Sandbox Code Playgroud)
更多信息auto_ptr
可以在这里找到.如果你可以使用C++ 11,那么它 shared_ptr
是一个强烈推荐的选择,通常比auto_ptr更受欢迎.
关于内存管理我需要了解的最低限度是什么?(或者,我在哪里找到它)?
对于每个新的,必须有删除
我在哪里可以获得中级和高级知识/教程/等(一旦我完成了>基础知识)?
阅读有效的C++,更有效的C++和有效的STL.然后google(std::) auto_ptr,(boost::) scoped_ptr和(boost::) shared_ptr
更具体地说:指针和引用之间的性能差异是什么?
由于引用是指针值的副本,我不知道我的头脑,我没有预见到任何大的性能问题.
我听说在循环中,你需要确保在循环重新迭代之前在任何新指针上调用delete.它是否正确?
是.
你需要用引用做些什么吗?
没有.
什么是内存泄漏的经典例子?
int * foo() {
...
return new int(...);
}
int main() {
int i = *foo();
...
//the new int() from foo leaks
}
Run Code Online (Sandbox Code Playgroud)
我需要了解以下内容(我是否真的需要使用它们 - 如果是这样,在哪里?):
首先,你永远不应该delete
使用malloc
ed指针,也不要free
创建指针new
.通常,这些函数不应出现在c ++代码中.但是,如果你发现自己在c-land ...
malloc:类似于new(在堆上分配内存)
free:类似于delete(堆上的可用内存)
calloc:类似于new + memset(在堆上分配内存,将其设置为零)
realloc:尝试调整块的大小内存,或创建一个新的内存块,并复制旧数据,free
旧指针.没有真正的c ++等价物.
googleing可以找到一些整洁的记忆内容(这是拼写的吗?) placement new
归档时间: |
|
查看次数: |
1513 次 |
最近记录: |