c c++-析构函数中使用delete报错

问题描述

析构函数中使用delete报错

把析构函数里的delete去掉就不会报错了;但是搞不懂:我的构造函数里不是用了new吗,为什么会出现这种情况?
报错信息是:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),网上查了一下都说是重发调用delete的结果,但是我好像没有那么做。

//Matrix.h
class Matrix
{
public:
Matrix();
~Matrix();
Matrix(int,double*); //矩阵行列数、数组指针
Matrix operator+(const Matrix&);//矩阵相加
friend ostream& operator<<(ostream&,const Matrix&);//
private:
int n; //矩阵的行列数
double* pt;
};

//Matrix.cpp
Matrix::Matrix():pt(0)
{
n = 0;
}
Matrix::Matrix(int r,double* p): n(r)
{

pt = new double[r*r];
for(int i=0; i<r*r;i++)

pt[i] = p[i];

}
Matrix::~Matrix()
{
delete []pt;
pt = 0;
}
//矩阵相加函数实现
Matrix Matrix::operator+(const Matrix& m)
{
for(int i=0;i<n*n;i++)

pt[i] = pt[i] + m.pt[i];

return *this;
}
//矩阵输出函数的实现
ostream& operator<<(ostream& output,const Matrix& m)
{

for(int i=0;i<m.n*m.n;i++)

{

if((i+1)%m.n == 0)

output<<m.pt[i]<<endl;

else

output<<m.pt[i]<<" ";

}

return output;

}

然后就是main函数中
double *p1,*p2,x,y;
int n;

cout<<"请输两个矩阵的行列数(n*n):"<<endl<<"n=";
cin>>n;

cout<<"请输入第一个矩阵m1的元素:"<<endl;

p1=(double *)malloc(sizeof(double)*n*n);
for(int i=0;i<n*n;i++)
{
    cin>>x;
    p1[i]=x;
}

cout<<"请输入第二个矩阵m2的元素:"<<endl;
p2=(double*)malloc(n*n*sizeof(double));
for(int j=0;j<n*n;j++)
{
    cin>>y;
    p2[j]=y;
}   

Matrix m1(n,p1),m2(n,p2),M;
M=m1+m2;
cout<<"m1+m2:"<<endl;
cout<<M<<endl;
free(p1);
free(p2);
return 0;

解决方案

delete []pt;换成delete pt;试试。
因为pt不是一个数组、

解决方案二:

缺少拷贝构造函数,对象引用时,拷贝构造出新的对象,而pt指向的申请空间都是同一片,最终你释放了两次,那么第二次必然就野了

时间: 2024-08-30 00:14:52

c c++-析构函数中使用delete报错的相关文章

sqlite-安卓中使用SQlite报错

问题描述 安卓中使用SQlite报错 创建一个数据库,根据返回值设置textView值.然后报错:"SYNTAX ERROR(code1) " 代码: package com.example.dbex; import java.sql.SQLOutput; import java.sql.SQLPermission; import android.content.ContentValues; import android.content.Context; import android.

arcgis-ae+c#中返回距离函数报错

问题描述 ae+c#中返回距离函数报错 IFeatureLayer aFL = axMapControl1.Map.get_Layer(0) as IFeatureLayer; IFeatureLayer bFL = axMapControl1.Map.get_Layer(1) as IFeatureLayer; IFeatureClass aFClass = aFL.FeatureClass; IFeatureCursor aFC = aFClass.Search(null, true); I

c语言-把数组中的元素 循环的作为函数中的参数 报错!

问题描述 把数组中的元素 循环的作为函数中的参数 报错! 把数组中的元素 循环的作为函数中的参数 怎么会报错 麻烦前辈帮忙看看 #include<stdio.h>int ShowNumber();int main (){ int number[10]; int i ; //循环变量 //随便为number数组赋值 for(i=0;i<10;i++)// i=0 1 2 3 4 5 6 7 8 9 { number[i]=i; } for(i=0;i<10;i++) { ShowNu

android中sqlite一直报错

问题描述 android中sqlite一直报错 在应用中用到sqlite,创建了一些表,代码如下: db.execSQL("CREATE TABLE IF NOT EXISTS " + GAMES_HISTORY_TABLE+ " (" + GAME_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + RIGHT_DRILLS + "INTEGER NOT NULL, " + NUM_

android中应用编译报错

问题描述 android中应用编译报错 一个带下拉列表的应用,在编译的时候报错: The constructor ArrayAdapter(LayoutNext, int, String[]) is undefined 不知道应该怎么解决? 我的实现代码: public class LayoutNext extends Fragment implements OnClickListener,OnItemSelectedListener{ TimePicker timepicker; privat

post请求-Post请求方式在地址栏中敲回车报错

问题描述 Post请求方式在地址栏中敲回车报错 Spring mvc中表单以post方式提交后将数据带到新的页面,但是在浏览器地址栏中敲回车的时候报错,地址栏回车属于get方式,请问这种情况如何避 解决方案 不指定请求访问方式不就可以了么 解决方案二: post 方式,不是get,给你个post工具 谷歌浏览器一样post测试 postman 解决方案三: post方式只能设计一个表单,一个提交按钮,才可以.浏览器输入地址,是get 解决方案四: 不知道题主的困惑在哪个地方,是想要调试post的

myeclipse中启动tomcat报错

问题描述 myeclipse中启动tomcat报错 在myeclipse中启动tomcat时报错,不知道怎么解决,请网友指点:报错信息如下Deployment is out of date due to changes in the underlying project contents.You'll need to 'Redeploy' the project to update the deployed archive; 警告提示是:构建路径指定执行环境 JavaSE-1.6.工作空间中没有

select-存储过程中SELECT赋值报错什么原因?

问题描述 存储过程中SELECT赋值报错什么原因? CREATE OR REPLACE PROCEDURE PROC_DICTABLE_TBYSFL(p_ANetUser VARCHAR)ISvc_DICTABLEID varchar2(36);vc_DICTABLECOLID varchar2(36);vc_DICCOL1 VARCHAR2(20);vc_DICCOL2 VARCHAR2(20);vc_DICCOL3 VARCHAR2(20);vc_DICCOL4 VARCHAR2(20);v

ndk ndroid tudio jni-AndroidStudio中开发ndk报错,求助!!!

问题描述 AndroidStudio中开发ndk报错,求助!!! D:workspacesAndroidProjectsGISpeechappsrcmainjniHTKLibexcpt.h:20:21: fatal error: crtdefs.h: No such file or directory #include <crtdefs.h> as 的项目中使用ndk 开发,其中有些 c 文件中引用了一些 系统平台下的 h 文件,在 vs2010 中可以调通,但是加入到 jni 中就报错,请问