Note that there are some explanatory texts on larger screens.

plurals
  1. POError while freeing memory
    primarykey
    data
    text
    <p>These are the files:</p> <pre><code>//============================================================================ // Name : linearMatrix.hpp // Author : Flores, Facundo Gabriel // Version : 0.1 // Description : This file contains the linearMatrix class. //=========================================================================== #ifndef LINEARMATRIX_HPP_ #define LINEARMATRIX_HPP_ class linearMatrix { public: //Constructor linearMatrix(const int Width, const int Heigth); //Destructor ~linearMatrix(); //We read a matrix file void Read_File_Matrix(const char *Path); //We write a matrix file void Write_File_Matrix(const char *Path); //Generate a diagonal dominant matrix void Generate_Diagonal_Dominant(void); //Generate a random matrix void Generate_Random_Matrix(void); //Copy a float *vector void Copy_Vector(const float *V, const int Width, const int Heigth); //Show a little vector void Show_Little_Matrix(void); private: int _Width, _Height; // Width and Height float* myVector; //Aux function for testing bool Test_Sizes(const int Width, const int Heigth); }; #endif /* LINEARMATRIX_HPP_ */ //============================================================================ // Name : linearMatrix.cpp // Author : Flores, Facundo Gabriel // Version : 0.1 // Description : This file contains the linearMatrix implementation class. // A linear matrix is a vector that represents a matrix. // We will read this kind of classes from files, because // they are generally large. But linearMatrix can represent // a 1D vector if Height is 1. //============================================================================ #include &lt;iostream&gt; using std::cout; using std::endl; using std::cerr; using std::bad_alloc; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;ctime&gt; #include &lt;new&gt; #include "linearMatrix.hpp" #define FALSE 0 #define TRUE 1 //Test if W and H are correct bool linearMatrix::Test_Sizes(const int W, const int H) { if(W &lt; 1 || W &gt; 32500) return FALSE; if(H &lt; 1 || H&gt; 32500) return FALSE; return TRUE; } // We set the Width and Height parameters linearMatrix::linearMatrix(const int Width, const int Height) { if(Test_Sizes(Width, Height)) { _Width = Width; _Height = Height; try{ myVector = new float[_Width * _Height]; } catch(bad_alloc &amp;ex) { cerr &lt;&lt; "Exception:" &lt;&lt; ex.what(); exit(1); } } else { cout&lt;&lt;"Bad sizes!"&lt;&lt;endl; exit(1); } } linearMatrix::~linearMatrix() { delete [] myVector; } //We set a random vector using its Width and Height void linearMatrix::Generate_Random_Matrix(void) { srand(time(NULL)); for(int i = 0; i &lt; _Width; i++) for(int j = 0; j &lt; _Height; j++) { // We are putting a number between 0 and 99 myVector[i * _Width + _Height] = rand() % 100; } } //We set a diagonal dominant matrix void linearMatrix::Generate_Diagonal_Dominant(void) { for(int i = 0; i &lt; _Width; i++) for(int j = 0; j &lt; _Height; j++) { if(i == j) //Diagonal item myVector[i * _Width + j] = 32500; else myVector[i * _Width + j] = 0.5; } } //We copy V into myVector void linearMatrix::Copy_Vector(const float *V, const int Width, const int Heigth) { if(Width != _Width || Heigth != _Height) { cout&lt;&lt;"Different sizes, we cannot copy vector V"&lt;&lt;endl; exit(1); } else { for(int i = 0; i &lt; _Width; i++) for(int j = 0; j &lt; _Height; j++) { myVector[i * _Width + j] = V[i * Width + j]; } } } //We show a little matrix. Assume H &lt; 11 and W &lt; 11 void linearMatrix::Show_Little_Matrix(void) { cout.precision(5); if(_Height &lt; 11 &amp;&amp; _Width &lt; 11) { for(int i = 0; i &lt; _Width; i++) { for(int j = 0; j &lt; _Height; j++) { cout&lt;&lt;myVector[i * _Width + j]&lt;&lt;" "; } cout &lt;&lt; endl; } } else { cout &lt;&lt; "I can show only little matrices in the console " &lt;&lt; endl; } } void linearMatrix::Write_File_Matrix(const char *Path) { FILE *pFile = fopen(Path, "wb"); fwrite(&amp;_Height, sizeof(int), 1, pFile); fwrite(&amp;_Width, sizeof(int), 1, pFile); fwrite(myVector, sizeof(float), _Height * _Width, pFile); fclose(pFile); } void linearMatrix::Read_File_Matrix(const char *Path) { FILE *pFile = fopen(Path, "rb"); if(pFile == NULL){ cout &lt;&lt; "Cannot read :" &lt;&lt; Path &lt;&lt; endl; exit(1); } fread(&amp;_Height, sizeof(int), 1, pFile); fread(&amp;_Width, sizeof(int), 1, pFile); try{ myVector = new float[_Width * _Height]; } catch(bad_alloc &amp;ex) { cout &lt;&lt; "Exception:" &lt;&lt; ex.what(); exit(1); } fread(myVector, sizeof(double), _Height * _Width, pFile); } </code></pre> <p>And I'm trying to use this class with the following <code>main.cpp</code>:</p> <pre><code>//============================================================================ // Name : main.cpp // Author : Flores, Facundo Gabriel // Version : 0.1 // Description : This file is a test. //=========================================================================== #include &lt;iostream&gt; #include "linearMatrix.hpp" int main(void) { linearMatrix Matrix2(3,1); Matrix2.Generate_Diagonal_Dominant(); Matrix2.Show_Little_Matrix(); return 0; } </code></pre> <p>So the error is:</p> <blockquote> <pre><code>*** glibc detected *** /.../linearMatrix/Debug/linearMatrix: free(): invalid next size (fast): 0x00000000007f1010 *** </code></pre> </blockquote> <p>I don't know how to use <code>valgrind</code> yet. But why is the error there? The program shows me the correct output, but when the destructor is "executed" the error appears while freeing <code>myVector</code>. How to solve it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload