segmentation fault for a 2D vector

Hi everybody.
My programm compiles withou any problem. I call a function where I want to compute the kronecker product. Inside the function KroneckerProduct everything works, there is no segfault and also the output at the end of the subfunction KroneckerProduct from the entries of C_help is right.
My problem is that I can not work with the matrix kronResult without a segfault (or in other words, the segfault seems to appear in the parameter kronResult.


It would be great if someone can find the error in the above sourcecode:


1
2
3
4
5
6
7
int main
{
...
	vector < vector <double> > kronResult; 
	KroneckerProduct(kronResult, I, E_jj, nNodes);
...
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void KroneckerProduct (vector < vector <double> > &C_help, vector < vector <double> > A_help, vector < vector <double> > B_help, int n)
{

  int matrixSize = n*n;
  C_help.resize(matrixSize);
  for( int i = 0; i < matrixSize; i++ ) 
    C_help[i].resize(matrixSize);
  
  int index_row = 0;
  int index_col = 0;
  
 ... calculate the kronecker product...

  //OUTPUT

  cout << endl;
  cout << " C :" << endl;
  
  for (int i = 0; i < n*n; i++)
    {
      cout << endl;
      for (int j = 0; j < n*n; j++)
	cout << C_help[i][j] << " ";
    }
  
  cout << endl;



  
}



By the way, I (identity) and E_jj are also 2D vectors (n x n) that are also tested and have the right entries
Last edited on
What you showed here is not the reason for the crash. The cause for the segfault is not necessary where it appears.

void KroneckerProduct (vector < vector <double> > &C_help, vector < vector <double> > A_help, vector < vector <double> > B_help, int n)

should be

void KroneckerProduct (vector < vector <double> > &C_help, const vector < vector <double> > &A_help, const vector < vector <double> > &B_help, int n) // Note: const &

otherwise you always copy the content of A_help and B_help.


This
1
2
3
4
  int matrixSize = n*n;
  C_help.resize(matrixSize);
  for( int i = 0; i < matrixSize; i++ ) 
    C_help[i].resize(matrixSize);

is probably not what you want

1
2
3
  C_help.resize(n); // n columns
  for( int i = 0; i < n; i++ ) 
    C_help[i].resize(n); // n rows -> n*n matrix 
and
1
2
3
4
5
6
  for (int i = 0; i < n; i++)
    {
      cout << endl;
      for (int j = 0; j < n; j++)
	cout << C_help[i][j] << " ";
    }


but that's all not the reason why
Topic archived. No new replies allowed.