glibc 检测到 *** ./burcu.exe: free(): 无效的下一个大小 (快速): 0x00000000
*** glibc detected *** ./burcu.exe: free(): invalid next size (fast): 0x000000001dad6310 ***
当我删除 2d 指针数组时,我的程序中有一个问题。我确实搜索了这个问题,但我在代码中看不到任何错误。我不知道这段代码有什么问题:
矩阵.h:
#ifndef _MATRIX_
#define _MATRIX_
#include<iostream>
#include<string>
using namespace std;
template <class Type>
class Matrix {
int line, column;
Type** arr;
public:
Matrix/*<Type> */(int, int);
void print() const;
~Matrix/*<Type>*/();
Type getElement (int, int) const;
bool contains (int) const;
};
template <class Type>
Matrix<Type> ::Matrix (int line_in, int column_in)
{ line=line_in;
column= column_in;
arr = new Type*[column];
for( int i = 0 ; i < column ; i++ )
arr[i] = new Type[line];
for( int i=0; i<line; i++)
for( int j=0; j<column; j++)
{arr[i][j]=(Type) (rand()%101)/10;
/*cout<< arr[i][j]<<endl;*/}
}
template <class Type>
Matrix<Type>::~Matrix ()
{
for(int i=0;i<column;++i)
delete [] arr[i];
delete [] arr;
}
template <class Type>
Type Matrix<Type>::getElement( int index_1, int index_2) const
{ string error= "Index out of bounds";
if (index_1>line||index_2>column) throw error;
else
return arr[index_1-1][index_2-1];
}
/* template <class Type>
void Matrix <Type>::print() const
{ for( int i=0; i<line; i++)
{for( int j=0; j<column; j++)
{cout<< arr[i][j] << " ";}
cout<<"n";}
}
*/
/* template <class Type>
bool Matrix <Type>::contains( int number) const
{ for( int i=0; i<line; i++)
{ for( int j=0; j<column; j++)
{ if( arr[i][j]== number)
return true;}
}
return false;
}*/
#endif
主.cpp:
#include <iostream>
#include <ctime>
#include <string>
#include<cstdlib>
#include<conio.h>
#include"matrix.h"
using namespace std;
int main()
{
srand(time(NULL));
Matrix<int> m1(3,5); // creating some objects
Matrix<int> m2(3,5); // matrices’ elements are assigned randomly from 0 to 10
Matrix<double> m3(5,5);
Matrix<double> m4(5,6);
/* try{
cout << m1.getElement(3,5) << endl; // trying to get the element at(3,6)
}
catch(const string & err_msg)
{
cout << err_msg << endl;
}*/
//cout << "Printing m4" << endl;
//m4.print(); // printing m4
/*try{
Matrix<double> m6 = m4 / m3; // trying to divide two matrices
}
catch(const string & err_msg)
{
cout << err_msg << endl;
} */
// cout << "Printing m1" << endl;
// m1.print();
/* if(m1.contains(4)) // checking if the matrix has an element with value 4
cout << "Matrix contains the element" << endl;
else
cout << "Matrix does not contain the element" << endl;*/
/* Matrix<int> m5 = m2;
cout << "Printing m5" << endl;
m5.print();
try{
--m1; // decrement m1's matrix elements by 1
m5 = m1 + m2; // sum m1 and m2 object's matrices and assign result to m5
++m3; // increment m1's matrix elements by 1
m2 = m5 / m1;
if(m5 == m1) // comparing two objects
cout << "Objects are equal" << endl;
else
cout << "Objects are not equal" << endl;
}
catch(const string & err_msg)
{
cout << err_msg << endl;
}
cout << "Printing m5" << endl;
m5.print();
}
*/
getch();
return 0;
}
Linux 上的错误消息:
*** glibc detected *** ./burcu.exe: free(): invalid next size (fast): 0x000000001dad6310 ***
======= Backtrace: =========
/lib64/libc.so.6[0x38e3e70d7f]
/lib64/libc.so.6(cfree+0x4b)[0x38e3e711db]
./burcu.exe[0x400c45]
./burcu.exe(__gxx_personality_v0+0x1f0)[0x400908]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x38e3e1d994]
./burcu.exe(__gxx_personality_v0+0x61)[0x400779]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:16 28050037 /users/lnxsrv1/ee/akgunhas/burcu3/burcu.exe
00601000-00602000 rw-p 00001000 00:16 28050037 /users/lnxsrv1 /ee/akgunhas/burcu3/burcu.exe
1dad6000-1daf7000 rw-p 1dad6000 00:00 0
38e3a00000-38e3a1c000 r-xp 00000000 fd:00 1593483 /lib64/ld-2.5.so
38e3c1c000-38e3c1d000 r--p 0001c000 fd:00 1593483 /lib64/ld-2.5.so
38e3c1d000-38e3c1e000 rw-p 0001d000 fd:00 1593483 /lib64/ld-2.5.so
38e3e00000-38e3f4d000 r-xp 00000000 fd:00 32363 /lib64/libc-2.5.so
38e3f4d000-38e414d000 ---p 0014d000 fd:00 32363 /lib64/libc-2.5.so
38e414d000-38e4151000 r--p 0014d000 fd:00 32363 /lib64/libc-2.5.so
38e4151000-38e4152000 rw-p 00151000 fd:00 32363 /lib64/libc-2.5.so
38e4152000-38e4157000 rw-p 38e4152000 00:00 0
38e4200000-38e4282000 r-xp 00000000 fd:00 291216 /lib64/libm-2.5.so
38e4282000-38e4481000 ---p 00082000 fd:00 291216 /lib64/libm-2.5.so
38e4481000-38e4482000 r--p 00081000 fd:00 291216 /lib64/libm-2.5.so
38e4482000-38e4483000 rw-p 00082000 fd:00 291216 /lib64/libm-2.5.so
38e7a00000-38e7a0d000 r-xp 00000000 fd:00 291213 /lib64/libgcc_s-4.1.2-20080825.so.1
38e7a0d000-38e7c0d000 ---p 0000d000 fd:00 291213 /lib64/libgcc_s- 4.1.2-20080825.so.1
38e7c0d000-38e7c0e000 rw-p 0000d000 fd:00 291213 /lib64/libgcc_s- 4.1.2-20080825.so.1
38eae00000-38eaee6000 r-xp 00000000 fd:00 785480 /usr/lib64/libstdc++.so.6.0.8
38eaee6000-38eb0e5000 ---p 000e6000 fd:00 785480 /usr/lib64/libstdc++.so.6.0.8
38eb0e5000-38eb0eb000 r--p 000e5000 fd:00 785480 /usr/lib64/libstdc++.so.6.0.8
38eb0eb000-38eb0ee000 rw-p 000eb000 fd:00 785480 /usr/lib64 /libstdc++.so.6.0.8
38eb0ee000-38eb100000 rw-p 38eb0ee000 00:00 0
2abb21ac1000-2abb21ac3000 rw-p 2abb21ac1000 00:00 0
2abb21ad9000-2abb21adb000 rw-p 2abb21ad9000 00:00 0
7fff72e31000-7fff72e46000 rw-p 7ffffffe9000 00:00 0 [stack]
7fff72f24000-7fff72f27000 r-xp 7fff72f24000 00:00 0 [vdso]
ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vsyscall]
Aborted
在VS 2010中:
HEAP[burcuhw3.exe]: Heap block at 001738B0 modified at 00173904 past requested size of 4c
Windows has triggered a breakpoint in burcuhw3.exe.
This may be due to a corruption of the heap, which indicates a bug in burcuhw3.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while burcuhw3.exe has focus.
The output window may have more diagnostic information.
The program '[1320] burcuhw3.exe: Native' has exited with code 0 (0x0).
似乎内部数据在构造函数中作为arr[column][line]
分配,然后用作arr[line][column]
。那将是一个问题!
相关文章:
- 重新定位图像时如何前进到下一个内存块
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- CIN 仅在输入非数字值时跳过下一个 CIN
- 查找最小的下一个更大的元素
- 转到基于范围的 for 循环中的下一个迭代器
- 如何检查流中文件的下一个单词是否为 alpha?
- 如何打印下一个字母直到 Z 并继续到 A?
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- 使用一个内存集数组和单个堆栈在 O(n) 中查找数组的下一个更大元素
- 有没有一种标准方法可以在C++中获取第 n 个"下一个"浮点值
- 下一个云桌面客户端构建过程
- C++自定义流操纵器,用于更改流上的下一个字符串
- 链表指针赋值为什么我们不能直接将尾巴分配给 temp 而不是尾巴>尾巴下一个
- 下一个排列定义
- 查找下一个具有真值C++的数组索引
- 如何比较文件中包含的下一个字符
- 检查IP是否在其他IP网络范围内,并查找下一个可用IP
- 当我使用c++生成器时,如何在输入完成后自动跳转到下一个"TEdit"
- glibc 检测到 *** ./burcu.exe: free(): 无效的下一个大小 (快速): 0x00000000