<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[SOLVED: C++ Delete huge double arrays]]></title><description><![CDATA[<p>Hi at all here!</p>
<p>I'm new to C++ and as I have coded a lot in Java now I got problems with the memory and although I have been browsing through several pages now, I still didn't find out how to solve my problem the best.</p>
<p>What I'm doing is programming a little maltab clone for my numerical analysis course.<br />
Thus I have to be able to operate on giant matrices.<br />
But somehow I am not able to free memory to the system and my Destructor calls generate memory trouble.</p>
<p>This is my matrix class:</p>
<pre><code class="language-cpp">#ifndef INC_MATRIX_H
#define INC_MATRIX_H

#include &lt;iostream&gt;
class Matrix 
{

public:
		int row, col;
		double **array;

		Matrix(int r, int c);
		~Matrix();

		void operator=(Matrix B);
		Matrix transpose();
};
#endif
</code></pre>
<p>The implementation is as follows:</p>
<pre><code class="language-cpp">#include &quot;str.h&quot;
using namespace std;

Matrix::Matrix(int r, int c) {
		this-&gt;row = r; this-&gt;col = c;			
		array = new double*[r];
		for(int i = 0; i &lt; r; i++) 
			array[i] = new double[c];
	}
Matrix::~Matrix() {
		this-&gt;row = 0;
		this-&gt;col = 0;
		//delete this-&gt;array;// =new double*[0];; // This does not work somehow!
	}
void Matrix::operator=(Matrix B) {
		// includes some warnings concerning dimensions
		for(int i = 0; i &lt; this-&gt;row; i++) 	
			for(int j = 0; j &lt; this-&gt;col; j++) 	
				this-&gt;array[i][j] = B.array[i][j];
	return;
	}
Matrix Matrix::transpose() {
		Matrix C = *(new Matrix(this-&gt;col,this-&gt;row));
		for(int i = 0; i &lt; this-&gt;row; i++) 	
			for(int j = 0; j &lt; this-&gt;col; j++) 	
			 	C.array[j][i] = this-&gt;array[i][j];	
		return C;
	}
</code></pre>
<p>You can test it using this little programm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

#include &quot;str.h&quot;

using namespace std;

int main() {
int n = 20;
Matrix B(n,n);
for(int i = 0; i &lt; n; i++) B.array[i][i] = i;
	while(1) {
		cout &lt;&lt; B.array[0][0];
		B=B.transpose();
	}
}
</code></pre>
<p>You will see, that the memory usage grows and grows, although I thought it is just one matrix.<br />
Have you got any ideas or links how I can optimise my code?</p>
<p>Kind Regards.<br />
Roland</p>
<p>PS: May someone move this post to the c++ section, please. I posted it wrong. Thank you!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/257157/solved-c-delete-huge-double-arrays</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 08:08:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/257157.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 23 Dec 2009 21:32:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Sun, 27 Dec 2009 09:02:44 GMT]]></title><description><![CDATA[<p>Hi at all here!</p>
<p>I'm new to C++ and as I have coded a lot in Java now I got problems with the memory and although I have been browsing through several pages now, I still didn't find out how to solve my problem the best.</p>
<p>What I'm doing is programming a little maltab clone for my numerical analysis course.<br />
Thus I have to be able to operate on giant matrices.<br />
But somehow I am not able to free memory to the system and my Destructor calls generate memory trouble.</p>
<p>This is my matrix class:</p>
<pre><code class="language-cpp">#ifndef INC_MATRIX_H
#define INC_MATRIX_H

#include &lt;iostream&gt;
class Matrix 
{

public:
		int row, col;
		double **array;

		Matrix(int r, int c);
		~Matrix();

		void operator=(Matrix B);
		Matrix transpose();
};
#endif
</code></pre>
<p>The implementation is as follows:</p>
<pre><code class="language-cpp">#include &quot;str.h&quot;
using namespace std;

Matrix::Matrix(int r, int c) {
		this-&gt;row = r; this-&gt;col = c;			
		array = new double*[r];
		for(int i = 0; i &lt; r; i++) 
			array[i] = new double[c];
	}
Matrix::~Matrix() {
		this-&gt;row = 0;
		this-&gt;col = 0;
		//delete this-&gt;array;// =new double*[0];; // This does not work somehow!
	}
void Matrix::operator=(Matrix B) {
		// includes some warnings concerning dimensions
		for(int i = 0; i &lt; this-&gt;row; i++) 	
			for(int j = 0; j &lt; this-&gt;col; j++) 	
				this-&gt;array[i][j] = B.array[i][j];
	return;
	}
Matrix Matrix::transpose() {
		Matrix C = *(new Matrix(this-&gt;col,this-&gt;row));
		for(int i = 0; i &lt; this-&gt;row; i++) 	
			for(int j = 0; j &lt; this-&gt;col; j++) 	
			 	C.array[j][i] = this-&gt;array[i][j];	
		return C;
	}
</code></pre>
<p>You can test it using this little programm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

#include &quot;str.h&quot;

using namespace std;

int main() {
int n = 20;
Matrix B(n,n);
for(int i = 0; i &lt; n; i++) B.array[i][i] = i;
	while(1) {
		cout &lt;&lt; B.array[0][0];
		B=B.transpose();
	}
}
</code></pre>
<p>You will see, that the memory usage grows and grows, although I thought it is just one matrix.<br />
Have you got any ideas or links how I can optimise my code?</p>
<p>Kind Regards.<br />
Roland</p>
<p>PS: May someone move this post to the c++ section, please. I posted it wrong. Thank you!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827036</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827036</guid><dc:creator><![CDATA[fromjavatocpp]]></dc:creator><pubDate>Sun, 27 Dec 2009 09:02:44 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Wed, 23 Dec 2009 22:25:18 GMT]]></title><description><![CDATA[<p>You have to free the memory the same way you allocated it, i.e. one call to delete[] for every call to new[].<br />
So your destructor should look like this:</p>
<pre><code class="language-cpp">Matrix::~Matrix()
{
  for (int i = 0; i &lt; row; i++)delete[] array[i];
  delete[] array;
  row = 0;
  col = 0;
}
</code></pre>
<p>this-&gt; inside member functions is optional, by the way (unless you have a function parameter with the same name).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827051</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827051</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Wed, 23 Dec 2009 22:25:18 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Thu, 24 Dec 2009 11:51:19 GMT]]></title><description><![CDATA[<p>Ah! Ok. This works fine in my test case, but now I tested this with matrix matrix multiplication and now I get an error which displays me a &quot;memory map&quot;.</p>
<p>If you would add the following to the<br />
Header file:</p>
<pre><code class="language-cpp">Matrix operator*(Matrix B);
</code></pre>
<p>str.cpp file:</p>
<pre><code class="language-cpp">Matrix Matrix::operator*(Matrix B) 
	{
		if( this-&gt;col != B.row) { 
			cout &lt;&lt; &quot;mult: Error, Matrix dimensions do not match: &quot;;
			cout &lt;&lt; &quot;Op1 is (&quot; &lt;&lt; this-&gt;row &lt;&lt;&quot;, &quot;&lt;&lt; this-&gt;col &lt;&lt; &quot;), &quot; &lt;&lt; &quot;Op2 is (&quot; &lt;&lt; B.row &lt;&lt; &quot;, &quot; &lt;&lt; B.col &lt;&lt; &quot;)&quot; &lt;&lt; endl; 
		 return *(new Matrix(0,0)); 
		}
		// initialize the new matrix
		Matrix C = *(new Matrix(this-&gt;row, B.col));
		// multiplicate
		for(int i= 0; i &lt; this-&gt;row; i++) 	
			for(int j = 0; j &lt; B.col; j ++) 	
				for(int k = 0; k &lt; this-&gt;col; k ++) 
					C.array[i][j] += this-&gt;array[i][k] * B.array[k][j]; 
		return C;
	}
</code></pre>
<p>testcase:</p>
<pre><code class="language-cpp">B=B*B;
</code></pre>
<p>I've checked, that it is not the problem that the matrix is multiplied with itself or within the loop..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827086</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827086</guid><dc:creator><![CDATA[fromjavatocpp]]></dc:creator><pubDate>Thu, 24 Dec 2009 11:51:19 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Wed, 23 Dec 2009 23:58:37 GMT]]></title><description><![CDATA[<p>Did you take into account that unlike in Java, &quot;Matrix B&quot; is <em>not</em> a reference? When passing objects this way to a function, a copy of the object is created, which requires a copy-constructor to be defined. If you want to pass the matrix by reference, this is the way to go:</p>
<pre><code class="language-cpp">Matrix operator*(const Matrix&amp; B);
</code></pre>
<p>In the implementation of operator* you should not create matrixes with new, as this will result in a memory leak. Instead, create them this way:</p>
<pre><code class="language-cpp">return Matrix(0,0);
//and:
Matrix C(this-&gt;row, B.col);
</code></pre>
<p>When you return the matrixes, it's possible that they need to be copied, which again requires a valid copy constructor.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827096</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827096</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Wed, 23 Dec 2009 23:58:37 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Thu, 24 Dec 2009 00:51:57 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile-var-mode-is-viewprofile-and-u-is-1819.html" rel="nofollow">rüdiger</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-8.html" rel="nofollow">Rund um die Programmierung</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-15.html" rel="nofollow">C++</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39405.html" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827111</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827111</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Thu, 24 Dec 2009 00:51:57 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Thu, 24 Dec 2009 11:53:23 GMT]]></title><description><![CDATA[<p>Yeah, I almost understand what's happening. Your advice works fine with operator=!!</p>
<p>But I've still've got a memory leak when it comes to multiplication.</p>
<p>May you explain to me, how operator= and the copy constructor work together?</p>
<p>My Copy-Constructor is now</p>
<pre><code class="language-cpp">Matrix::Matrix(const Matrix&amp; B) 
		: row(B.row), col(B.col), array(new double*) 
	{
		for(int i = 0; i &lt; B.row; i++) {
			array[i] = new double[B.col];    // #include &lt;algorithm&gt; for std::copy
			for(int j = 0; j &lt; B.col; j++)
				array[i][j] = B.array[i][j];
		}
	}
</code></pre>
<p>and I changed multiplication like this:</p>
<pre><code>Matrix Matrix::operator*(const Matrix&amp; B) 
	{
		if( this-&gt;col != B.row) { 
			cout &lt;&lt; &quot;mult: Error, Matrix dimensions do not match: &quot;;
			cout &lt;&lt; &quot;Op1 is (&quot; &lt;&lt; this-&gt;row &lt;&lt;&quot;, &quot;&lt;&lt; this-&gt;col &lt;&lt; &quot;), &quot; &lt;&lt; &quot;Op2 is (&quot; &lt;&lt; B.row &lt;&lt; &quot;, &quot; &lt;&lt; B.col &lt;&lt; &quot;)&quot; &lt;&lt; endl; 
		 return Matrix(0,0); 
		}
		// initialize the new matrix
		Matrix C(this-&gt;row, B.col);
		// multiplicate
		for(int i= 0; i &lt; this-&gt;row; i++) 	
			for(int j = 0; j &lt; B.col; j ++) 	
				for(int k = 0; k &lt; this-&gt;col; k ++) 
					C.array[i][j] += this-&gt;array[i][k] * B.array[k][j]; 
		return C;
	}
</code></pre>
<p>If I test it with</p>
<pre><code>while(1) C*B
</code></pre>
<p>I still am leaking memory. If you are wondering, why I do not have a Destructor:<br />
I get the memory map error again!</p>
<p>Btw: Do you know why I don't get these fancy code boxes?</p>
<p>Regards,<br />
Roland :xmas1:</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827218</guid><dc:creator><![CDATA[fromjavatocpp]]></dc:creator><pubDate>Thu, 24 Dec 2009 11:53:23 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Thu, 24 Dec 2009 11:25:33 GMT]]></title><description><![CDATA[<p>fromjavatocpp schrieb:</p>
<blockquote>
<p>I still am leaking memory. If you are wondering, why I do not have a Destructor:<br />
I get the memory map error again!</p>
</blockquote>
<p>If you haven't got a destructor which deletes the allocated memory, that's simple. Every multiplication creates a new matrix, therefore calls the constructor. In the constructor, you allocate memory by <code>new</code> that is never freed.</p>
<p>fromjavatocpp schrieb:</p>
<blockquote>
<p>May you explain to me, how operator= and the copy constructor work together?</p>
</blockquote>
<p>Have you ever heard of Copy&amp;Swap? That's an idiom to implement the assignment operator safely (regarding exceptions), plus to avoid code duplication.</p>
<p>First, provide a <code>swap()</code> method that exchanges the contents of two matrices:</p>
<pre><code class="language-cpp">void Matrix::swap(Matrix&amp; other)
{
    std::swap(col, other.col);
    std::swap(col, other.col);
    std::swap(array, other.array);
}
</code></pre>
<p>Then, you can implement the assignment operator using your copy constructor and <code>swap()</code> :</p>
<pre><code class="language-cpp">Matrix&amp; Matrix::operator= (const Matrix&amp; origin)
{
    Matrix temp(origin); // create a temporary copy of origin
    swap(temp); // exchange contents of *this and temp, so *this equals origin
    return *this;
} // note: temp gets out of scope here, so the old content is destroyed.
</code></pre>
<p>You can find more informations about Copy&amp;Swap in the english wikibooks.</p>
<p>fromjavatocpp schrieb:</p>
<blockquote>
<p>Btw: Do you know why I don't get these fancy code boxes?</p>
</blockquote>
<p>Did you accidentally check the box &quot;BBCode in diesem Beitrag deaktivieren&quot; (deactivate BBCode)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827227</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827227</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 24 Dec 2009 11:25:33 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Thu, 24 Dec 2009 13:29:41 GMT]]></title><description><![CDATA[<p>Hi!</p>
<p>Thank you for your advice with the copy and swap. Seems to make sense.</p>
<p>But still the problem is there: Maybe someone can try to get the code to run? (This would be nice!)</p>
<p>When I try to install a Destructor like you can see below, I get a Error:</p>
<blockquote>
<p>*** glibc detected *** ./a.out: corrupted double-linked list: 0x09d833a0 ***</p>
</blockquote>
<p>I have done the swap thing, too, and my =operator is like it should be and my copy constructor works fine now.</p>
<p>But still, what is wrong with my destructor?</p>
<p>My testprog now is as simple as this:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;
class Matrix {
public:
int row, col;
double **array;
 Matrix(int r, int c) {
	this-&gt;row = r; this-&gt;col = c;			
	array = new double*[r];
	for(int i = 0; i &lt; r; i++) 
		array[i] = new double[c];
}
 Matrix(const Matrix&amp; B) 
	: row(B.row), col(B.col), array(new double*) {
	for(int i = 0; i &lt; B.row; i++) {
	   array[i] = new double[B.col];    
	     for(int j = 0; j &lt; B.col; j++)
		array[i][j] = B.array[i][j];
        }
// the whole copy thing here does not work properly, because at the end it would be the same refernce and the problem still exists
}
~Matrix() {
	for (int i = 0; i &lt; row; i++) delete[] array[i];
	delete[] array;
	row = 0;
	col = 0; 
	}
void swap(Matrix&amp; B) 	{
         std::swap(row, B.row);
         std::swap(col, B.col);
         std::swap(array, B.array);
}
Matrix&amp; operator=(const Matrix&amp; B) {
    Matrix temp(B); // create a temporary copy of origin
    swap(temp); // exchange contents of *this and temp, so *this equals origin
    return *this;
}
}; // class

int main() {
   int n = 10;
   Matrix C(n,n);
   for(int i = 0; i &lt; n; i++)
	C.array[i][i] = i;	
   Matrix A = Matrix(C);
   cout &lt;&lt; C.array[1][1] &lt;&lt; &quot; &quot; &lt;&lt; A.array[1][1] &lt;&lt; endl;
   C.array[1][1] = 100;
   cout &lt;&lt; C.array[1][1] &lt;&lt; &quot; &quot; &lt;&lt; A.array[1][1] &lt;&lt; endl;
}
</code></pre>
<p>I just noticed that I get no problem when I do not call the Copy Constructor</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827300</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827300</guid><dc:creator><![CDATA[fromjavatocpp]]></dc:creator><pubDate>Thu, 24 Dec 2009 13:29:41 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Thu, 24 Dec 2009 15:19:47 GMT]]></title><description><![CDATA[<p>There is something strange in your code, in the constructor your wrote:</p>
<pre><code class="language-cpp">array = new double*[r];
</code></pre>
<p>However in the copyconstructor, you wrote:</p>
<pre><code class="language-cpp">array(new double*)
</code></pre>
<p>You have never specified how many rows should be created... maybe that could produce an error.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1827366</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1827366</guid><dc:creator><![CDATA[uhsuhz]]></dc:creator><pubDate>Thu, 24 Dec 2009 15:19:47 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Sun, 27 Dec 2009 09:01:34 GMT]]></title><description><![CDATA[<p>On again.. Thank you all!<br />
I think I have learned a lot about C++ from you.</p>
<p>Have a nice Sunday!</p>
<p>Best wishes,<br />
Roland</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1828307</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1828307</guid><dc:creator><![CDATA[fromjavatocpp]]></dc:creator><pubDate>Sun, 27 Dec 2009 09:01:34 GMT</pubDate></item><item><title><![CDATA[Reply to SOLVED: C++ Delete huge double arrays on Sun, 27 Dec 2009 11:54:26 GMT]]></title><description><![CDATA[<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1828358</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1828358</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 27 Dec 2009 11:54:26 GMT</pubDate></item></channel></rss>