Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy constructor fails and succeeds in identical circumstances
    primarykey
    data
    text
    <p>I am writing a matrix class for a math project, and I have a strange problem where the copy constructor I've written fails in one function but succeeds in another- and the circumstances are seemingly identical. I've run it through gdb and the copy constructor executes but somehow just doesn't assign the values after it finishes.</p> <p>Here is the output from gdb:</p> <pre><code>(gdb) break main.cpp:20 Breakpoint 1 at 0x8048913: file main.cpp, line 20. (gdb) run Starting program: /home/ian/Documents/math471/src/compress printing 1 1 1 1 1 2 3 4 1 3 5 7 1 4 7 10 printing 1 1 1 1 1 2 3 4 1 3 5 7 1 4 7 10 printing 1 1 1 1 1 2 3 4 1 3 5 7 1 4 7 10 printing 1 1 1 1 1 2 3 4 1 3 5 7 1 4 7 10 Breakpoint 1, main (argc=1, argv=0xbffff334) at main.cpp:20 20 tridiagonalize(A); (gdb) s tridiagonalize (A=...) at tridiagonalize.cpp:10 10 Matrix result(A); (gdb) print result $1 = {transposed = false, height = 3903476, width = 0, data = 0x0} (gdb) s Matrix::Matrix (this=0xbffff23c, A=...) at matrix.cpp:176 176 height = A.getHeight(); (gdb) print height $2 = 0 (gdb) n 177 width = A.getWidth(); (gdb) print height $3 = 4 (gdb) n 178 data = new double*[height]; (gdb) print width $4 = 4 (gdb) n 179 for(int i = 0; i &lt; height; i++){ (gdb) break matrix.cpp:185 Breakpoint 2 at 0x80492ba: file matrix.cpp, line 185. (gdb) c Continuing. Breakpoint 2, Matrix::Matrix (this=0xbffff23c, A=...) at matrix.cpp:185 185 transposed = false; (gdb) n 186 } (gdb) print transposed $5 = false (gdb) print this $6 = (Matrix * const) 0xbffff23c (gdb) print this-&gt;height $7 = 4 (gdb) n tridiagonalize (A=...) at tridiagonalize.cpp:11 11 int n = A.getWidth(); (gdb) print result $8 = {transposed = false, height = 3903476, width = 0, data = 0x0} (gdb) </code></pre> <p>As you can see, the values in result are the same before the constructor as after.</p> <pre><code>#include "matrix.h" #include "tridiagonalize.h" using namespace std; void function(Matrix &amp;A); int main(int argc, char** argv){ Matrix A(4, 4); for(int i = 0; i &lt; 16; i++){ A.set(i/4, i%4, (i%4)*(i/4)+1); } function(A); Matrix B = A; A.print(); B.print(); tridiagonalize(A); //A.print(); return 0; } void function(Matrix &amp;A){ Matrix result(A); A.print(); result.print(); } Matrix tridiagonalize(Matrix &amp;A){ Matrix result(A); int n = A.getWidth(); cout &lt;&lt; "width: " &lt;&lt; n &lt;&lt; endl; cout &lt;&lt; "width of result: " &lt;&lt; result.getWidth() &lt;&lt; endl; return result; } </code></pre> <p>EDIT: here is the matrix code:</p> <pre><code>void Matrix::set(int i, int j, double value){ if(transposed){ int tmp = i; i = j; j = tmp; } if(i &lt; 0 || j &lt; 0 || i &gt;= height || j &gt;= width){ cout &lt;&lt; "trying to set value outside of matrix" &lt;&lt; endl; return; } if(height &gt; 0 &amp;&amp; width &gt; 0){ data[i][j] = value; } return; } void Matrix::print() const{ cout &lt;&lt; "printing" &lt;&lt; endl; if(!transposed){ for(int i = 0; i &lt; height; i++){ for(int j = 0; j &lt; width; j++){ cout &lt;&lt; data[i][j] &lt;&lt; " "; } cout &lt;&lt; endl; } } else{ for(int i = 0; i &lt; width; i++){ for(int j = 0; j &lt; height; j++){ cout &lt;&lt; data[j][i] &lt;&lt; " "; } cout &lt;&lt; endl; } } return; } Matrix::~Matrix(){ //cout &lt;&lt; "deleting Matrix" &lt;&lt; endl; if(data != NULL){ for(int i = 0; i &lt; height; i++){ //cout &lt;&lt; data[i] &lt;&lt; endl; delete[] data[i]; } //cout &lt;&lt; data &lt;&lt; endl; delete[] data; } } double Matrix::get(int i, int j) const { if(transposed){ int tmp = i; i = j; j = tmp; } if(data != NULL &amp;&amp; i &gt;=0 &amp;&amp; j &gt;= 0 &amp;&amp; i &lt; height &amp;&amp; j &lt; width){ return data[i][j]; } else{ cout &lt;&lt; "error in get" &lt;&lt; endl; } return -1; } int Matrix::getHeight() const { if(transposed){ return width; } else{ return height; } } int Matrix::getWidth() const { if(transposed){ return height; } else{ return width; } } Matrix::Matrix(const Matrix&amp; A){ height = A.getHeight(); width = A.getWidth(); data = new double*[height]; for(int i = 0; i &lt; height; i++){ data[i] = new double[width]; for(int j = 0; j &lt; width; j++){ data[i][j] = A.get(i,j); } } transposed = false; } Matrix Matrix::operator= (Matrix &amp;B){ if(data != NULL){ for(int i = 0; i &lt; height; i++){ delete[] data[i]; } delete[] data; } data = NULL; Matrix result(B); return result; } </code></pre> <p>The tridiagonalize function and the 'function' function are set up the same, but function() works and tridiagonalize() doesn't. Any ideas why this is happening?</p> <p>EDIT: added the matrix code</p>
    singulars
    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.
    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