删除析构函数中的指针时出现分段错误 [英] segmentation fault while deleting a pointer in a destructor

查看:83
本文介绍了删除析构函数中的指针时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个类,我在

构造函数中为双数组分配空间。我在其中一个方法中使用了两次double数组,然后在类的析构函数中删除了那个数组。


现在,删除操作是给我一个分段错误。如果我移动

使用

指针所在方法中的指针的分配和删除,它可以正常工作......但是然后另一个指针

在析取指针被删除时会在析构函数中出现分段错误。总而言之,在那一个

方法中使用了一堆指针。这些内存在构造函数中被分配,并且在析构函数中被删除了
(它不是虚拟类)。


我试图找到哪里是问题但到目前为止还没有成功。

在继续之前,我有一个具体的问题。


我是否正确理解指针在类的构造函数中使用new进行分配,然后在类成员中使用该指针

以及后续删除类析构函数中的指针是

应该好吗?


谢谢。

Hello,

I have class in which I am allocating space for a double array in the
constructor. I use the double array twice in one of the methods and then
delete that array in the class''s destructor.

Now, that delete operation is giving me a segmentation fault. If I move
the allocation and deletion of the pointer within the method where the
pointer is being is used, it works okay ... but then another pointer
gives a segmentation fault in the destructor when that pointer is being
deleted. In all, there a bunch of pointers being used in that one
method. Memory for these is being allocated in the constructor and being
deleted in the destructor (it is not a virtual class).

I am trying to find where is the problem but haven''t successful so far.
Before I continue, I have a specific question.

Am I correct in understanding that the pointer allocation using new in a
constructor of a class, then the usage of that pointer in a class member
and subsequent deletion of the pointer in the class destructor is
supposed to go okay?

Thanks.

推荐答案

8月3日,8日:15:00,HS < hs.sa ... @ gmail.comwrote:
On Aug 3, 8:15 pm, "H.S." <hs.sa...@gmail.comwrote:

现在,删除操作给我一个分段错误。
Now, that delete operation is giving me a segmentation fault.



您最好的选择是在尽可能小的

计划中重现问题并向我们展示。没有看到你在做什么,我们只能猜测
。这是我的:


你还没有定义复制构造函数和/或赋值

运算符,或者没有错误地定义它们;所以对象被删除

两次。

Your best bet is to reproduce the problem in the smallest possible
program and to show us. Without seeing what you''re doing, we can only
guess. Here is mine:

You haven''t defined the copy constructor and/or the assignment
operator, or defined them incorrectly; so the object gets deleted
twice.


我是否正确理解指针分配在
$ b中使用new $ b类的构造函数,然后在类成员中使用该指针

以及后续删除类析构函数中的指针

应该没问题?
Am I correct in understanding that the pointer allocation using new in a
constructor of a class, then the usage of that pointer in a class member
and subsequent deletion of the pointer in the class destructor is
supposed to go okay?



是的。


Ali

Yes.

Ali


ac ****** @ gmail.com 写道:

On 8月3日晚上8:15,HS < hs.sa ... @ gmail.comwrote:
On Aug 3, 8:15 pm, "H.S." <hs.sa...@gmail.comwrote:

>现在,该删除操作给我一个分段错误。
>Now, that delete operation is giving me a segmentation fault.



您最好的选择是在尽可能小的

计划中重现问题并向我们展示。没有看到你在做什么,我们只能


Your best bet is to reproduce the problem in the smallest possible
program and to show us. Without seeing what you''re doing, we can only



我明白这一点。但是同一个程序在其他时候有效。所以我是

不确定我是否能够重现问题并想到

首先验证我的理解。

I understand that. But the same program works at other times. So I was
not sure if I would be able to reproduce the problem and thought of
verifying my understanding first.


猜。这是我的:


你还没有定义复制构造函数和/或赋值

运算符,或者没有错误地定义它们;所以对象被删除

两次。
guess. Here is mine:

You haven''t defined the copy constructor and/or the assignment
operator, or defined them incorrectly; so the object gets deleted
twice.



有问题的变量都是双数组而不是任何自定义的

对象。例如,ctor具有:

m_dpz = new double [m_n];


m_dpz用作变量来保存成员函数中的向量。


dtor有:

delete [] m_dpz;


同样还有其他变量(类似的类型和长度)。

The variable in question are all double arrays and not any custom
objects. For example, the ctor has:
m_dpz = new double [m_n];

m_dpz gets used as a variable to hold a vector in a member function.

The dtor has:
delete [] m_dpz;

Similarly there are other variables (similar type and length).


>
>

>我是否正确理解在类的构造函数中使用new的指针分配,然后在类成员中使用该指针
以及后续删除类析构函数中的指针
应该可以吗?
>Am I correct in understanding that the pointer allocation using new in a
constructor of a class, then the usage of that pointer in a class member
and subsequent deletion of the pointer in the class destructor is
supposed to go okay?



是的。


Yes.



hmm ..这证实了我所知道的。因此,一个玩具示例不会显示任何问题......我想。有了这个,我可以

继续寻找其他问题。

谢谢。

hmm .. that confirms what I knew. Consequently, a toy example is not
going to show any problems ... I think. With that out of the way, I can
proceed to look for other problems.
Thanks.


8月3日晚上9点10分,HS < hs.sa ... @ gmail.comwrote:
On Aug 3, 9:10 pm, "H.S." <hs.sa...@gmail.comwrote:

acehr ... @ gmail.com写道:
acehr...@gmail.com wrote:


有问题的变量都是双数组而不是任何自定义的

对象。例如,ctor具有:

m_dpz = new double [m_n];


m_dpz用作变量来保存成员函数中的向量。
The variable in question are all double arrays and not any custom
objects. For example, the ctor has:
m_dpz = new double [m_n];

m_dpz gets used as a variable to hold a vector in a member function.



为什么不使用std :: vector?

Why not use std::vector?


dtor有:

delete [] m_dpz;
The dtor has:
delete [] m_dpz;


a玩具示例不会显示任何问题...
a toy example is not going to show any problems ...



玩具示例将根据定义显示问题。我说要在最小的可能程序中重现问题。最小的

可能的程序是你的玩具示例,根据定义它显示你的

问题。一旦你开始添加它来重现它,你将会找到罪魁祸首。


我写了这个程序导致了分段错误。在我的系统上:


class C

{

double * m_dpz;

$ b $公开:


C()



m_dpz(新双[1])

{

m_dpz [-2] = 42;

}


~C()

{

delete [] m_dpz;

}

};


int main()

{

C c0;

}


阿里

The toy example will show the problem, by definition. I said "to
reproduce the problem in the smallest possible program". That smallest
possible program is your toy example and by definition it shows your
problem. Once you start adding pieces to it to reproduce, you will
find the culprit.

I wrote this program that causes a "Segmentation fault" on my system:

class C
{
double * m_dpz;

public:

C()
:
m_dpz(new double[1])
{
m_dpz[-2] = 42;
}

~C()
{
delete[] m_dpz;
}
};

int main()
{
C c0;
}

Ali


这篇关于删除析构函数中的指针时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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