您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> C/C++ >> 求教,C++程序中一个段错误问题,实在找不到了

求教,C++程序中一个段错误问题,实在找不到了

来源:网络整理     时间:2016/9/2 4:10:00     关键词:

关于网友提出的“ 求教,C++程序中一个段错误问题,实在找不到了”问题疑问,本网通过在网上对“ 求教,C++程序中一个段错误问题,实在找不到了”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 求教,C++程序中一个段错误问题,实在找不到了
描述:

段错误c++测试

//头文件
#ifndef ARRAY_H_INCLUDED
#define ARRAY_H_INCLUDED
#include 
using namespace std;
class Array
{
    public:
    Array();
    Array( int,int );
    Array( const Array &arr );
    ~Array();
    void inputArray();
    Array operator+( const Array & arr );
    Array operator-( const Array & arr );
    Array operator*(  Array & arr );
    bool operator==(const Array & arr );
    bool operator!=(const Array & arr );
    friend ostream & operator<<( ostream &,const Array & arr );
    friend istream & operator>>( istream & in,Array & arr );
    private:
    int rank;//列
    int row;//行
    int **point;
};
#endif // ARRAY_H_INCLUDED
//.cpp 函数实现文件
#include 
#include "Array.h"
using namespace std;
Array::Array()
{
    rank = 1;
    row = 1;
    point = new int*[1];
    point[1] = new int[1];
}
Array::Array( int h,int l )
{
    if( h < 0 || l < 0)
    {
        row = 1;
        rank = 1;
    }
    else
    {
        row = h;
        rank = l;
    }
    point = new int*[row];//申请一个长度为row的指针数组
    for( int i=0;i<>
    {
        point[i] = new int[rank];//创建一个二维数组,row为行,rank为列
    }
    for( int j=0;j<>
    {
        for( int k=0;k<>
        {
            point[j][k] = 0;
        }
    }
}
Array::Array( const Array &arr )
{
    rank = arr.rank;
    row = arr.row;
    point = new int*[arr.row];
    for( int i=0;i<>
    {
        point[i] = new int[arr.rank];//创建一个二维数组,row为行,rank为列
    }
    for( int j=0;j<>
    {
        for( int k=0;k<>
        {
            point[j][k] = arr.point[j][k];
        }
    }
}
Array::~Array()
{
    for( int i=0;i<>
    {
        delete[] point[row];
    }
    delete point;
}
istream & operator>>( istream & in,Array & arr )//重载输入
{
    int temp;
    cout << "请按行输入矩阵: \n";
    for( int i=0;i<>
    {
        for( int j=0;j<>
        {
            in >> temp;
            arr.point[i][j]= temp;
        }
    }
    return in;
}
ostream & operator<<( ostream & out, const Array & arr )//重载输出
{
    int temp;
    int j = 0;
     for( int i=0;i<>
    {
        for( j=0;j<>
        {
            temp = arr.point[i][j];
            out << temp << " ";
        }
        out <<>
    }
    return out;
}
Array Array::operator+( const Array & arr )//重载加
{
    cout << "两矩阵相加:\n";
    Array sum( this->row,this->rank );
    for( int i=0;i<=this->row;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            sum.point[i][j] = this->point[i][j] + arr.point[i][j];
        }
    }
    
    return sum;
}
Array Array::operator-( const Array & arr )//重载减
{
    cout << "两矩阵相减:\n";
//        <<"结果矩阵信息: "<row<<" 行 "<rank<<" 列 \n";
    Array sum( this->row,this->rank );
    for( int i=0;i<=this->row;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            sum.point[i][j] = this->point[i][j] - arr.point[i][j];
        }
    }
    return sum;
}
Array Array::operator*(  Array & arr )//矩阵相乘
{
    cout << "两矩阵相乘:\n"
        <<"结果矩阵信息: "<row<<" 行 "<<>
    Array mult( this->row,this->rank );
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            for( int k=0;krank;k++ )
            {
                mult.point[i][j] += this->point[i][k] * arr.point[k][j];
            }
        }
    }
    return mult;
}
bool Array::operator==(const Array & arr )//判断矩阵相等
{
    if(( this->row != arr.row ) || ( this->rank != arr.rank ) )
    {
        return false;
    }
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            if( this->point[i][j] != arr.point[i][j] )
            {
                return false;
            }
        }
    }
    return true;
}
bool Array::operator!=(const Array & arr )
{
   if(( this->row != arr.row ) && ( this->rank != arr.rank ) )
    {
        return true;
    }
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            if( this->point[i][j] != arr.point[i][j] )
            {
                return true;
            }
        }
    }
    return false;
}
//main测试
#include 
#include "Array.h"
using namespace std;
int main()
{
    Array temp(2,2);
    cout <<"矩阵m1的行数2和列数2: \n";
    Array m(2,2);
    cin >> m;
    cout <<"矩阵m2的行数2和列数2: \n";
    Array m3( 2,2 );
    cin >> m3;
    temp=m+m3;
    cout << temp <<>
 //   cout << m-m3 <<>
    return 0;
}

这是一个实现矩阵加减乘,等与不等的程序
在测试的时候运行到了两个矩阵相加的时候崩溃,提示段错误,我找不到到底是哪个指针指错了。
各位大神帮忙看一下~~谢谢了

解决方案1:

引用 4 楼 llr19931113 的回复:
引用 2 楼 tofu_ 的回复:引用 楼主 llr19931113 的回复:C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273……

加入你用动态分配获取了一个长度为10个字节的内存,char *p = new char[10];那么只有下标0~9是合法的,也就是p[0], p[1]...p[9]这10个是合法的,p[10]超过了分配的那块内存的尾端,所以称之为越界。
在你的程序中,point = new int*[row],所以合法的下标范围是0~row-1,在for( int i=0;i<=this->row;i++ )运行到最后一次的时候,i等于row,使用p[row]的时候就是在访问未知的内存区间,这很显然是不对的,容易导致程序崩溃。 解决方案2:

我去,operator =()写失败了。果然拷贝代码是不行的。


Array& operator=(const Array& other)
    {
     assert(row == other.row && rank == other.rank && other.point != NULL);
     if (this != &other)
     {
for (int j = 0; j < row; j++)
{
for (int k = 0; k < rank; k++)
{
point[j][k] = other.point[j][k];
}
}
     }
     return *this;
    }
解决方案3:

引用 楼主 llr19931113 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929……


class Array
{
    public:
    Array();
    Array( int,int );
    Array( const Array &arr );
    ~Array();
    void inputArray();
    const Array operator+( const Array & arr );
    Array operator-( const Array & arr );
    Array operator*(  Array & arr );
    bool operator==(const Array & arr );
    bool operator!=(const Array & arr );
    Array& operator=(const Array& other)
    {
     if (this != &other)
     {
int** new_point = new int*[row]; //申请一个长度为row的指针数组
// 下标范围错误
//    for( int i=0;i<>
for (int i = 0; i < row; i++)
{
new_point[i] = new int[rank];     //创建一个二维数组,row为行,rank为列
}
for (int j = 0; j < row; j++)
{
for (int k = 0; k < rank; k++)
{
new_point[j][k] = other.point[j][k];
}
}
point = new_point;
     }
     return *this;
    }
    friend ostream & operator<<( ostream &,const Array & arr );
    friend istream & operator>>( istream & in,Array & arr );
    private:
    int rank;//列
    int row;//行
    int **point;
};
//.cpp 函数实现文件
Array::Array()
{
    rank = 1;
    row = 1;
    point = new int*[1];
    point[1] = new int[1];
}
Array::Array( int h,int l )
{
    if( h < 0 || l < 0)
    {
        row = 1;
        rank = 1;
    }
    else
    {
        row = h;
        rank = l;
    }
    point = new int*[row];//申请一个长度为row的指针数组
    // 下标范围错误
//    for( int i=0;i<>
    for( int i=0;i<>
    {
        point[i] = new int[rank];//创建一个二维数组,row为行,rank为列
    }
    for( int j=0;j<>
    {
        for( int k=0;k<>
        {
            point[j][k] = 0;
        }
    }
}
Array::Array( const Array &arr )
{
    rank = arr.rank;
    row = arr.row;
    point = new int*[arr.row];
    // 应该是行,你写成列了
    // for( int i=0;i<>
    for( int i=0;i<>
    {
        point[i] = new int[arr.rank];//创建一个二维数组,row为行,rank为列
    }
    for( int j=0;j<>
    {
        for( int k=0;k<>
        {
            point[j][k] = arr.point[j][k];
        }
    }
}
Array::~Array()
{
    for( int i=0;i<>
    {
     // 错了
//        delete[] point[row];
        delete[] point[i];
    }
    // 这个也是数组啊
//    delete point;
    delete[] point;
}
istream & operator>>( istream & in,Array & arr )//重载输入
{
    int temp;
    cout << "请按行输入矩阵: \n";
    for( int i=0;i<>
    {
        for( int j=0;j<>
        {
            in >> temp;
            arr.point[i][j]= temp;
        }
    }
    return in;
}
ostream & operator<<( ostream & out, const Array & arr )//重载输出
{
    int temp;
    int j = 0;
     for( int i=0;i<>
    {
        for( j=0;j<>
        {
            temp = arr.point[i][j];
            out << temp << " ";
        }
        out <<>
    }
    return out;
}
const Array Array::operator+( const Array & arr )//重载加
{
    cout << "两矩阵相加:\n";
    Array sum( this->row,this->rank );
    // 越界
//    for( int i=0;i<=this->row;i++ )
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            sum.point[i][j] = this->point[i][j] + arr.point[i][j];
        }
    }
    return sum;
}
Array Array::operator-( const Array & arr )//重载减
{
    cout << "两矩阵相减:\n";
//        <<"结果矩阵信息: "<row<<" 行 "<rank<<" 列 \n";
    Array sum( this->row,this->rank );
    // 越界
//    for( int i=0;i<=this->row;i++ )
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            sum.point[i][j] = this->point[i][j] - arr.point[i][j];
        }
    }
    return sum;
}
Array Array::operator*(  Array & arr )//矩阵相乘
{
    cout << "两矩阵相乘:\n"
        <<"结果矩阵信息: "<row<<" 行 "<<>
    Array mult( this->row,this->rank );
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            for( int k=0;krank;k++ )
            {
                mult.point[i][j] += this->point[i][k] * arr.point[k][j];
            }
        }
    }
    return mult;
}
bool Array::operator==(const Array & arr )//判断矩阵相等
{
    if(( this->row != arr.row ) || ( this->rank != arr.rank ) )
    {
        return false;
    }
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            if( this->point[i][j] != arr.point[i][j] )
            {
                return false;
            }
        }
    }
    return true;
}
bool Array::operator!=(const Array & arr )
{
   if(( this->row != arr.row ) && ( this->rank != arr.rank ) )
    {
        return true;
    }
    for( int i=0;irow;i++ )
    {
        for( int j=0;jrank;j++ )
        {
            if( this->point[i][j] != arr.point[i][j] )
            {
                return true;
            }
        }
    }
    return false;
}
//main测试
int main()
{
    Array temp(2,2);
    cout <<"矩阵m1的行数2和列数2: \n";
    Array m(2,2);
    cin >> m;
    cout <<"矩阵m2的行数2和列数2: \n";
    Array m3( 2,2 );
    cin >> m3;
    cout << m << endl;
    cout << m3 << endl;
    temp=m+m3;
    cout << temp <<>
    cout << m-m3 <<>
    return 0;
}
解决方案4:

需要提供 operator=(Array const&) 的重载,执行正确的赋值。另外


Array::Array( int h,int l )
{
...
    point = new int*[row];//申请一个长度为row的指针数组
    // for( int i=0;i<>
    for( int i=0;i<>
    // ------------^^^-------, I think you need row here.
    {
        point[i] = new int[rank];//创建一个二维数组,row为行,rank为列
    }

复制构造函数中存在同样的错误。
以上介绍了“ 求教,C++程序中一个段错误问题,实在找不到了”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3714790.html

相关图片

相关文章