2012-05-06 28 views
0

我在我的程序中有一个问题,当我删除2d指针数组。我没有搜索这个问题,但我看不到我的代码中有任何错误。我不知道什么是错,此代码:*** glibc detected *** ./burcu.exe:free():无效的下一个大小(快):0x000000001dad6310 ***

matrix.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 

main.cpp中:在Linux上

#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; 
    } 

错误消息:

*** 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). 
+0

如果您按照[这些说明](http://randomascii.wordpress.com/2011/10/15/try-analyze-for-free/)更新您的MSVC Express,使用'/ analyze'编译可以指向出一些超出界限的内存访问和其他皮棉风格的评论。不会让你不必理解你的代码,但它是一个很好的安全网。 – DCoder

+0

对不起,刚刚测试'/ analyze'我自己 - 它没有捕捉到这个特殊的问题。没关系。 – DCoder

回答

3

似乎内部数据在构造函数中分配为arr[column][line],然后用作arr[line][column]。这将是一个问题!

相关问题