Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing double pointer causes segmentation fault
    primarykey
    data
    text
    <p>I am programming Jacobis method for eigenvalue problems in QM and I have just started c++, I want to use double pointers to construct matrices but the physics problem involved requires a lot of code.</p> <p>I don't want to clutter my <code>main()</code> with unreadible lines (others will have to read this code..) and so wanted to divide the problem into subfunctions. I made a function that takes a double pointer and returns a matrix, but why can't I access it outside the function? My code <strong>segfaults</strong> (marked below) when I try to. How do I construct a matrix outside main() while still being able to access it in <code>main()</code>?</p> <p></p> <pre><code>enter code her enter code here int i, j, k; //== BEGIN MAIN ==// int main () { //Constants and variables double **A; double epsilon = pow((double)10, double(-8)); //The convergence limit for jacobis method int N = 10; //Dimension of matrix char test[] = "test"; cout &lt;&lt;"The inner matrix function:"&lt;&lt;endl; makematrix(N, A); cout&lt;&lt;endl&lt;&lt;"The outer matrix function:"&lt;&lt;endl; //This part segfaults for(i=0; i&lt;N; i++) { cout&lt;&lt;endl; for(j=0; j&lt;N; j++) { cout&lt;&lt;A[i][j]&lt;&lt;" "; } } return 0; } //== END MAIN ==// //==Begin function definitions==// void makematrix(int N, double **A) { //Function for initializing our tridiagonal matrices for jacobis method A = new double*[N]; for(i=0; i&lt;N; i++) { A[i] = new double[N]; } for(i=0; i&lt;N; i++) { for(j=0; j&lt;N; j++) { A[i][j] = 0; } } //Prints the matrix declared here for(i=0; i&lt;N; i++) { cout&lt;&lt;endl; for(j=0; j&lt;N; j++) { cout&lt;&lt;A[i][j]&lt;&lt;" "; } } cout &lt;&lt;endl; return; } </code></pre>
    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