分配和取消分配内存 [英] allocate and deallocate memory

查看:78
本文介绍了分配和取消分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
如果我的疑问微不足道,我感到抱歉,但是我不知道该如何解决.
我有一个在构造函数中分配这样的2d数组的类:

hi everybody.
i''m sorry if my doubt is trivial, but i can''t figure out how to fix it.
i have a class that in constructor I allocate a 2d array like this:

int i;
this->matrix = new double*[5];
for(i = 0 ; i < 5; i++)
    this->matrix[i]=new double[5];



然后在析构函数中像这样删除2d数组.



and in the destructor i delete the 2d array like this.

int i;
for(i = 0; i < 5 ; i++)
{
    if(this->matrix[i] != NULL)
    {
	delete[] this->matrix[i];
	this->matrix[i] = NULL;
    }
}
if(this->matrix != NULL)
{
    delete []this->matrix;
    this->matrix = NULL;
}



有时我有堆损坏,我发现这在析构函数中发生.我不知道二维数组分配是错误的还是破坏是错误的.

PS:很可能在我的程序中分配了一个包含1000个元素的数组,并且程序运行正常,有时会出现内存不足错误?如果是这样,导致此错误的原因是什么?通过OS进行内存管理?

谁能帮我吗?
在此先感谢Filipe



some times i have a heap corruption and I discovery that this happens in the destructor. I don''t know if the 2d array allocation is wrong or is the destruction that is wrong.

PS: It''s possible that in my program I allocate an array with 1000 elements and the program works fine, and sometimes give me out of memory error? if so, what causes this error? the memory management by OS?

Can anyone help me?
thanks in advance, Filipe

推荐答案


请参阅下面的

初学者的内存管理 [ http://www.cplusplus.com/doc/tutorial/dynamic/ [ http://msdn.microsoft.com/en-us/library/kewsb8ba.aspx [ ^ ]
http://www.doc.ic.ac.uk/lab/cplus/c ++.rules/chap16.html [ ^ ]
Hi,
Refer below

Memory Management for Beginners[^]
http://www.cplusplus.com/doc/tutorial/dynamic/[^]
http://msdn.microsoft.com/en-us/library/kewsb8ba.aspx[^]
http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap16.html[^]


在您的析构函数中,
In your destructor, the
if(this->matrix != NULL)
{



应该在for循环之前和周围-否则,您将遍历外部数组的内容以删除其成员,然后在删除外部数组之前检查它是否存在.



should go before and around the for loop - otherwise you are iterating over the contents of the outer array deleting its members, and then checking if it exists before you delete the outer array.


1.您的课程有几个构造函数?您的帖子暗示只有一个.如果那是正确的,那么您的构造函数应该没问题;否则,请检查其他构造函数,以确保它们使用相同的代码(或更优的是,调用相同的函数进行分配)

2.您是否在类之外传递了指向矩阵或矩阵行的指针?如果是这样,请确保调用者不要尝试删除[]这些指针!

3.复制包含该矩阵的类类型的对象时会发生什么?如果您没有副本构造函数-并且根据1.您可能没有-则编译器会为您创建一个副本构造函数,并且该副本构造函数将仅复制矩阵的指针,而不创建矩阵的新副本.堆上应该有的结构!结果,当副本被销毁时,矩阵将被释放,但原始对象中的指针将不受影响,并且销毁后,您将再次尝试将其删除,这会导致运行时错误.

您应该提供自己的副本构造函数.它需要对数据进行深层复制,以使销毁对象不会影响原始对象的矩阵对象.

请注意,副本构造函数可能在您不知道的情况下隐式调用,例如. G.当将一个对象按值传递给函数或将一个作为返回值传递时.同样,如果将对象存储在任何STL容器中,它们可能会调用复制构造函数.

4.关于您的析构函数代码,请参阅解决方案3.

我的赌注是在3.;)
1. How many constructors does your class have? Your post implies there is only one. If that''s correct your constructor should be fine; otherwise check your other constructors to make sure they use the same code (or better, call the same function for the allocation)

2. Do you pass out a pointer to your matrix or matrix rows outside your class? If so, make sure that the callers don''t try and delete[] these pointers!

3. What happens when you copy an object of the class type that contains this matrix? If you don''t have a copy constructor - and according to 1. you probably don''t - then the compiler creates one for you, and that copy constructor will only copy the pointer of your matrix, not create a new copy of the structure on the heap as it should! As a result, when the copy is destructed, the matrix will be released, but the pointer in the original object will not be affected and upon destruction you will again try to delete it which causes a runtime error.

You should provide a copy constructor of your own instead. It needs to do a deep copy of your data so that destructing the object will not affect the matrix object of the original.

Note that a copy constructor may be invoked implicitely without you being aware of it, e. g. when passing an object by value to a function or passing one as a return value. Also, if you store your objects in any of the STL containers they may invoke the copy constructor.

4. See solution 3 regarding your destructor code.

My bets are on 3. ;)


这篇关于分配和取消分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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