编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
- rob2468
- 帖子: 185
- 注册时间: 2009-03-19 8:39
- 联系:
编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;
class Matrix
{
public:
Matrix(int Row=0,int Col=0)
:row(Row),col(Col)
{
x=new double[row*col];
for(int i=row*col-1;i>=0;i--)
x=0;
}
void Set()
{
for(int i=row*col-1;i>=0;i--)
x=rand()%10;
}
Matrix transposition(const Matrix &mat)
{
row=mat.col;
col=mat.row;
for(int m=0;m<=row-1;m++)
{
for(int n=0;n<=col-1;n++)
{
x[m*col+n]=mat.x[n*row+m];
}
}
return *this;
}
friend ostream &operator<<(ostream &out,const Matrix &mat)
{
out<<mat.row<<" "<<mat.col<<endl;
for(int m=0;m<=mat.row-1;m++)
{
out<<"["<<mat.x[m*mat.col];
for(int n=1;n<=mat.col-1;n++)
out<<" "<<mat.x[m*mat.col+n];
out<<"]"<<endl;
}
return out;
}
~Matrix()
{
if(x!=NULL) delete []x;
}
private:
int row,col;
double *x;
};
int main()
{
time_t t;
srand(time(&t));
Matrix mat(4,5);
mat.Set();
cout<<mat<<endl;
Matrix mat2;
mat2=mat2.transposition(mat);
cout<<mat2<<endl;
return 0;
}
下面是运行显示的结果
4 5
[0 2 3 8 9]
[5 7 0 8 2]
[3 4 5 1 3]
[2 6 3 5 1]
*** glibc detected *** ./a: free(): invalid next size (fast): 0x091350b0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e23604]
/lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb7e255b6]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb8006231]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xb800628d]
./a[0x8048d5c]
./a[0x80489e3]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7dca775]
./a[0x8048831]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 9158821 /home/rob2468/桌面/a
08049000-0804a000 r--p 00001000 08:01 9158821 /home/rob2468/桌面/a
0804a000-0804b000 rw-p 00002000 08:01 9158821 /home/rob2468/桌面/a
09135000-09156000 rw-p 09135000 00:00 0 [heap]
b7c00000-b7c21000 rw-p b7c00000 00:00 0
b7c21000-b7d00000 ---p b7c21000 00:00 0
b7db3000-b7db4000 rw-p b7db3000 00:00 0
b7db4000-b7f10000 r-xp 00000000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f10000-b7f11000 ---p 0015c000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f11000-b7f13000 r--p 0015c000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f13000-b7f14000 rw-p 0015e000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f14000-b7f17000 rw-p b7f14000 00:00 0
b7f17000-b7f24000 r-xp 00000000 08:01 368705 /lib/libgcc_s.so.1
b7f24000-b7f25000 r--p 0000c000 08:01 368705 /lib/libgcc_s.so.1
b7f25000-b7f26000 rw-p 0000d000 08:01 368705 /lib/libgcc_s.so.1
b7f26000-b7f27000 rw-p b7f26000 00:00 0
b7f27000-b7f4b000 r-xp 00000000 08:01 386276 /lib/tls/i686/cmov/libm-2.9.so
b7f4b000-b7f4c000 r--p 00023000 08:01 386276 /lib/tls/i686/cmov/libm-2.9.so
b7f4c000-b7f4d000 rw-p 00024000 08:01 386276 /lib/tls/i686/cmov/libm-2.9.so
b7f4d000-b8031000 r-xp 00000000 08:01 2312654 /usr/lib/libstdc++.so.6.0.10
b8031000-b8035000 r--p 000e3000 08:01 2312654 /usr/lib/libstdc++.so.6.0.10
b8035000-b8036000 rw-p 000e7000 08:01 2312654 /usr/lib/libstdc++.so.6.0.10
b8036000-b803c000 rw-p b8036000 00:00 0
b804a000-b804d000 rw-p b804a000 00:00 0
b804d000-b804e000 r-xp b804d000 00:00 0 [vdso]
b804e000-b806a000 r-xp 00000000 08:01 368663 /lib/ld-2.9.so
b806a000-b806b000 r--p 0001b000 08:01 368663 /lib/ld-2.9.so
b806b000-b806c000 rw-p 0001c000 08:01 368663 /lib/ld-2.9.so
bff56000-bff6b000 rw-p bffeb000 00:00 0 [stack]
忽略
这是怎么回事,怎么解决
#include<stdlib.h>
#include<ctime>
using namespace std;
class Matrix
{
public:
Matrix(int Row=0,int Col=0)
:row(Row),col(Col)
{
x=new double[row*col];
for(int i=row*col-1;i>=0;i--)
x=0;
}
void Set()
{
for(int i=row*col-1;i>=0;i--)
x=rand()%10;
}
Matrix transposition(const Matrix &mat)
{
row=mat.col;
col=mat.row;
for(int m=0;m<=row-1;m++)
{
for(int n=0;n<=col-1;n++)
{
x[m*col+n]=mat.x[n*row+m];
}
}
return *this;
}
friend ostream &operator<<(ostream &out,const Matrix &mat)
{
out<<mat.row<<" "<<mat.col<<endl;
for(int m=0;m<=mat.row-1;m++)
{
out<<"["<<mat.x[m*mat.col];
for(int n=1;n<=mat.col-1;n++)
out<<" "<<mat.x[m*mat.col+n];
out<<"]"<<endl;
}
return out;
}
~Matrix()
{
if(x!=NULL) delete []x;
}
private:
int row,col;
double *x;
};
int main()
{
time_t t;
srand(time(&t));
Matrix mat(4,5);
mat.Set();
cout<<mat<<endl;
Matrix mat2;
mat2=mat2.transposition(mat);
cout<<mat2<<endl;
return 0;
}
下面是运行显示的结果
4 5
[0 2 3 8 9]
[5 7 0 8 2]
[3 4 5 1 3]
[2 6 3 5 1]
*** glibc detected *** ./a: free(): invalid next size (fast): 0x091350b0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e23604]
/lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb7e255b6]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb8006231]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xb800628d]
./a[0x8048d5c]
./a[0x80489e3]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7dca775]
./a[0x8048831]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 9158821 /home/rob2468/桌面/a
08049000-0804a000 r--p 00001000 08:01 9158821 /home/rob2468/桌面/a
0804a000-0804b000 rw-p 00002000 08:01 9158821 /home/rob2468/桌面/a
09135000-09156000 rw-p 09135000 00:00 0 [heap]
b7c00000-b7c21000 rw-p b7c00000 00:00 0
b7c21000-b7d00000 ---p b7c21000 00:00 0
b7db3000-b7db4000 rw-p b7db3000 00:00 0
b7db4000-b7f10000 r-xp 00000000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f10000-b7f11000 ---p 0015c000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f11000-b7f13000 r--p 0015c000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f13000-b7f14000 rw-p 0015e000 08:01 386268 /lib/tls/i686/cmov/libc-2.9.so
b7f14000-b7f17000 rw-p b7f14000 00:00 0
b7f17000-b7f24000 r-xp 00000000 08:01 368705 /lib/libgcc_s.so.1
b7f24000-b7f25000 r--p 0000c000 08:01 368705 /lib/libgcc_s.so.1
b7f25000-b7f26000 rw-p 0000d000 08:01 368705 /lib/libgcc_s.so.1
b7f26000-b7f27000 rw-p b7f26000 00:00 0
b7f27000-b7f4b000 r-xp 00000000 08:01 386276 /lib/tls/i686/cmov/libm-2.9.so
b7f4b000-b7f4c000 r--p 00023000 08:01 386276 /lib/tls/i686/cmov/libm-2.9.so
b7f4c000-b7f4d000 rw-p 00024000 08:01 386276 /lib/tls/i686/cmov/libm-2.9.so
b7f4d000-b8031000 r-xp 00000000 08:01 2312654 /usr/lib/libstdc++.so.6.0.10
b8031000-b8035000 r--p 000e3000 08:01 2312654 /usr/lib/libstdc++.so.6.0.10
b8035000-b8036000 rw-p 000e7000 08:01 2312654 /usr/lib/libstdc++.so.6.0.10
b8036000-b803c000 rw-p b8036000 00:00 0
b804a000-b804d000 rw-p b804a000 00:00 0
b804d000-b804e000 r-xp b804d000 00:00 0 [vdso]
b804e000-b806a000 r-xp 00000000 08:01 368663 /lib/ld-2.9.so
b806a000-b806b000 r--p 0001b000 08:01 368663 /lib/ld-2.9.so
b806b000-b806c000 rw-p 0001c000 08:01 368663 /lib/ld-2.9.so
bff56000-bff6b000 rw-p bffeb000 00:00 0 [stack]
忽略
这是怎么回事,怎么解决
- rob2468
- 帖子: 185
- 注册时间: 2009-03-19 8:39
- 联系:
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
我把mat2=mat2.transposition(mat);改成了mat2.transposition(mat);
结果能显示出矩阵转置前后的样子
后面还是一大串输出内容
难道后面的内容只是g++编译器对程序的一些说明?
这些内容表达了什么意思啊
结果能显示出矩阵转置前后的样子
后面还是一大串输出内容
难道后面的内容只是g++编译器对程序的一些说明?
这些内容表达了什么意思啊
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
看 mat2 的行和列是什么
PS:错漏百出
PS:错漏百出
^_^ ~~~
要理解递归,首先要理解递归。
地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
要理解递归,首先要理解递归。
地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
- rob2468
- 帖子: 185
- 注册时间: 2009-03-19 8:39
- 联系:
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
构造mat2时,我没有对它初始话,所以调用构造函数后行列都是0BigSnake.NET 写了:看 mat2 的行和列是什么
PS:错漏百出
还有上面我说的把mat2=mat2.transposition(mat);语句改成mat2.transposition(mat);之后
那 错漏百出 在什么地方
- rob2468
- 帖子: 185
- 注册时间: 2009-03-19 8:39
- 联系:
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
矩阵转置,特别是非方阵,不借助另一片空间是实现不了的。在你的程序中存在下标越界导致出错。
应该是这个原因吧
应该是这个原因吧
- xiaocheng_zh
- 帖子: 46
- 注册时间: 2009-05-30 15:52
- 来自: DL LIAONING CHN
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
看到了吧,mat2没有分配到空间,你的Matrix(int Row=0,int Col=0),默认的是Row=0,Col=0。等于mat2就是没有分配到任何内存空间,这种情况下编译器在编译时是可以通过,但链接时会发生错误,视具体的内存状况而不同;正如楼上判断的那样是内存越界了。要想正确运行很简单,就是mat2初始化的时候加上(5,4),正确地给mat2分配出20个double元素的空间,编译连接运行都没问题了。此外,使用new分配的情况下,最好使用if(point==NULL){……}作出分配内存失败情况下的处理,还有try{}catch(){}异常处理来防止内存越界的发生。对于这样的测试程序来说,只是小小的问题,但对于大型的矩阵转换,会发生灾难性的后果int main()
{
time_t t;
srand(time(&t));
Matrix mat(4,5);
mat.Set();
cout<<mat<<endl;
Matrix mat2;//GOD!!!mat2 is [0,0]
mat2=mat2.transposition(mat);
cout<<mat2<<endl;
return 0;
}
上次由 xiaocheng_zh 在 2009-06-23 14:25,总共编辑 1 次。
- xiaocheng_zh
- 帖子: 46
- 注册时间: 2009-05-30 15:52
- 来自: DL LIAONING CHN
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
你说的可能是一些警告之类的,因为"mat2=mat2.transposition(mat);"调用了默认赋值构造函数,你没有显式定义Matrix operator=(const Matrix&)。看来,你把C++看的太简单了点儿。我把mat2=mat2.transposition(mat);改成了mat2.transposition(mat);
结果能显示出矩阵转置前后的样子
后面还是一大串输出内容
难道后面的内容只是g++编译器对程序的一些说明?
这些内容表达了什么意思啊
-
- 帖子: 4
- 注册时间: 2009-05-08 21:28
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
代码: 全选
#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;
class Matrix {
public:
Matrix(int Row = 0, int Col = 0):row(Row), col(Col)
{
x = new double[row * col];
for (int i = row * col - 1; i >= 0; i--)
x[i] = 0;
}
void Set()
{
for (int i = row * col - 1; i >= 0; i--)
x[i] = rand() % 10;
}
void transposition(const Matrix & mat)
{
row = mat.col;
col = mat.row;
for (int m = 0; m <= row - 1; m++)
{
for (int n = 0; n <= col - 1; n++)
{
x[m * col + n] = mat.x[n * row + m];
}
}
//return *this;
}
friend ostream & operator<<(ostream & out, const Matrix & mat)
{
out << mat.row << " " << mat.col << endl;
for (int m = 0; m <= mat.row - 1; m++)
{
out << "[" << mat.x[m * mat.col];
for (int n = 1; n <= mat.col - 1; n++)
out << " " << mat.x[m * mat.col + n];
out << "]" << endl;
}
return out;
}
~Matrix() {
if (x != NULL)
delete[]x;
}
private:
int row, col;
double *x;
};
int main()
{
time_t t;
srand(time(&t));
Matrix mat(5, 5);
mat.Set();
cout << mat << endl;
Matrix mat2(5, 5);
mat2.transposition(mat);
cout << mat2 << endl;
return 0;
}
- CGer
- 帖子: 210
- 注册时间: 2009-06-21 19:38
- 来自: CUMT
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
很强大啊,我的线性代数都还没学好啊!
代码: 全选
sudo apt-get install girlfriend
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
有一些软件包无法被安装。
下列的信息可能会对解决问题有所帮助:
下列的软件包有不能满足的依赖关系:
girlfriend: 依赖: house但是它将不会被安装
girlfriend: 依赖: car但是它将不会被安装
house,car: 依赖: money但是它将不会被安装
E: 无法安装的软件包
- rob2468
- 帖子: 185
- 注册时间: 2009-03-19 8:39
- 联系:
Re: 编写的下面一个C++程序,在windows下正常运行,ubuntu下编译正确,运行错误
这个程序我自己已经完全修改过了,现在已经能正确运行了
我自己的这个程序写的还是挺锉的,有点模糊
8楼的朋友对于transposition()函数理解的比较好,作为一个类的成员函数,我类的对象直接调用该函数就能改变该对象的数据成员,返回类型void就可以了
我自己的这个程序写的还是挺锉的,有点模糊
就是这部分语句就存在内存处理不当的问题,主函数中我通过构造函数构造了一个叫mat2的对象,x申请的对空间为0,而在这个函数体中,矩阵么行和列已经发生了变化,我却忽略了对x的内存处理Matrix transposition(const Matrix &mat)
{
row=mat.col;
col=mat.row;
for(int m=0;m<=row-1;m++)
{
for(int n=0;n<=col-1;n++)
{
x[m*col+n]=mat.x[n*row+m];
}
}
return *this;
}
8楼的朋友对于transposition()函数理解的比较好,作为一个类的成员函数,我类的对象直接调用该函数就能改变该对象的数据成员,返回类型void就可以了