2012-05-06 9 views
0

2d-pointer-arrayを削除するときに私のプログラムに問題があります。私はこの問題を検索しましたが、私のコードには何の不具合も見られません。私はこのコードが悪いのか分からない:*** glibcが検出されました*** ./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]として使用されます。それは問題になるでしょう!

関連する問題