Segmentation error

I have a problem of this C++ program when trying to execute
it says "Segmentation Error"
and did not give which line I need to fix, so... generally, I am doomed, and I need help...

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 
/*******************LINEAR CHAIN**********************
|    this program is developed to calculate the eigenvalues and 	|
| eigenvectors of a linear chain of n-masses and (n+1)springs		|
| where n is even. Using Jacobian's method for sym. matrix		|
|		The chain is limited up to diatomic chain	|
******************************************************/


#include <iostream>
using namespace std;
#include <math.h>
//stdlib for memory allocation/ dynamic memory
#include <stdlib.h>

void matrix_alloc( double **array, int nrows, int ncolumns);
void matrix_init( double **array, int n);
void matrix_init_identity( double **array, int n);
void update( int k, double t, double& y, double e[], bool changed[], int& state);
/*void update(index k, parameter t, parameter y, eigenvector e[], changed[], state*/
void rotate(int k, int l, int i, int j, double **matrix, double c, double s);
int maxind (int k, double **array, int n);

int main(){
	int n=10;			// n need to be even to create continous boundary
	double **E, **S;
	//dynamic memory allocation to E[][] and S[][]
	matrix_alloc(E, n, n);
	matrix_alloc(S, n, n);
	//varible declaration
	double e[n];
	int i, k, l, m, state;
	double s, c, t, p, y;
	int ind[n];
	bool changed[n];
	//manually set the spring constant nad mass
	double spring[2] = {10, 10};
	double mass [2] = {1, 1};

	//Assign components to matrix S[][] only works for even nxn matrix
	for ( i = 0; i < (n-2); i = i+2){ //set for upper, lower diagonal and diagonal
		S[i][i+1] = -spring[1]/mass[0];
		S[i+1][i] = -spring[1]/mass[1];
		S[i][i] = (spring[0] + spring[1]) / mass[0];
	};
	for ( i = 1; i < (n-3); i = i+2){
		S[i][i+1] = -spring[0]/mass[1];
		S[i+1][i] = -spring[0] / mass[0];
		S[i][i] = (spring[0] + spring[1]) / mass[1];
	};
	S[n-1][n-1] = (spring[0] + spring[1]) / mass[1];
	S[0][n-1] = -spring[0] / mass[0];
	S[n-1][0] = -spring[0] / mass[1];
	
	//init E[][] and arrays ind[], e[], changed[]
	matrix_init_identity( E, n);
	state = n;
	for (k=0; k<n; k++){
		ind[k] = maxind( k, S, n);
		e[k] = S[k][k];
		changed[k] = true;
	};
	//rotation around biggest index - pivot p(k,l)- by Givens' matrix
	while ( state != 0) {
		m = 0;
		//find pivot p( k, l)
		for ( k=1; k < (n-1); k++){
			if ( fabs( S[k][ind[k]]) > fabs(S[m][ind[m]]))
				m = k;
		};
		//prepare Givens' matrix's components
		k = m;
		l = ind[m];
		p = S[k][l];
		y = (e[l] - e[k])/2;		t = fabs(y) + sqrt( p*p + y*y );
		s = sqrt( p*p + t*t);		
		c = t/s;
		s = p/s;
		t = p*p/t;
		if (y<0){
			s = -s;
			t = -t;
		};
		S[k][l] = 0;
		update( k, -t, y, e, changed, state);
		update( l, t, y, e, changed, state);
		//rotate rows and columns k, l
		for ( i = 0; i < k; i++) rotate(i, k, i, l, S, c, s);
		for ( i = k + 1; i < l; i++) rotate( k, i, i, l, S, c, s);
		for ( i = l + 1; i < n; i++) rotate( k, i, l, i, S, c, s);
		//rotate eigenvectors
		for ( i = 0; i < n; i++) rotate ( k, i, l, i, E, c, s);
		// update rows k, l by changing ind[k], ind[l]
		ind[k] = maxind( k, S, n);
		ind[l] = maxind(l, S, n);
	};
};

//***************Function*******************
void matrix_alloc (double **array, int nrows, int ncolumns){
	if (array != 0){
		cout << "error allocating memory"<<endl;
		return;
	};
	array = (double**)malloc(nrows * sizeof(int *));
	if(array == NULL){
		cout << "error allocating memory"<<endl;
		return;
		};
	for(int i = 0; i < nrows; i++){
		array[i] = (double*)malloc(ncolumns * sizeof(int));
		if ( array[i] == NULL){
			cout << "error allocating memory"<<endl;
			return;
		};
	};
};
void matrix_init( double **array, int n){
	for (int i=0; i < n; i++){
		for (int j=0; j < n; j++) {
			array[i][j] = 0;
		};
	     };
};
void matrix_init_identity (double **array, int n){
	for (int i=0; i < n; i++){
		for (int j=0; j < n; j++) {
			if (i == j){
				array[i][j] = 1;
			}else{
				array[i][j] = 0;
			};
		};
	     };
};
void update( int k, double t, double& y, double e[], bool changed[], int& state){
	y = e[k];
	e[k] = y + t;
	if (changed[k] && (y = e[k])){
		changed[k] = false;
		state = state - 1;
	}else{
		if ((!changed[k]) && (y != e[k])){
			changed[k] = true;
			state = state + 1;
		};
	};
};
void rotate(int k, int l, int i, int j, double **matrix, double c, double s){
	double a, b;
	a = c*matrix[k][l] - s*matrix[i][j];
	b = s*matrix[k][l] + c*matrix[i][j];
	matrix[k][l] = a;
	matrix[i][j] = b;
};
int maxind (int k, double **array, int n){ 
	/*find maximum component, off diagonal in (row k) of matrix (array) size (n)*/
	int m = k+1;
	for (int i = k+2; i < n; i++){
		if ( fabs(array[k][i]) > fabs( array[k][m]))
			m = i;
	};
};
Your matrix_alloc function does not allocate memory for the pointer passed to it. Instead, it just allocates memory to a local buffer, leaving whatever you called it with unchanged.

Basically, it does nothing but leak memory

1
2
3
4
double **E, **S;

matrix_alloc(E, n, n);  // this doesn't change E
matrix_alloc(S, n, n);  // this doesn't change S 


If you want matrix_alloc to change E,S ... you'll need to pass them by pointer (which means your function would need to take a double***, or have them returned by the function (so the function returns double** instead of void.

The latter is simpler:

1
2
E = matrix_alloc(n,n);
S = matrix_alloc(n,n);
So, correct me if I am wrong, if I were to do the latter (returned by the function double**)
Is this the right way to do?
it says
error: a function-definition is not allowed here before ‘{’ token


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//***************Function*******************
double** matrix_alloc  ( int nrows, int ncolumns){  //error: a function-definition is not allowed here before ‘{’ token
	double **array;

	array = (double**)malloc(nrows * sizeof(int *));
	if(array == NULL){
		cout << "error allocating memory"<<endl;
		};
	for(int i = 0; i < nrows; i++){
		array[i] = (double*)malloc(ncolumns * sizeof(int));
		if ( array[i] == NULL){
			cout << "error allocating memory"<<endl;
		};
	};
	return array;
};




========================================================
reference

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 /*******************LINEAR CHAIN**********************
 |    this program is developed to calculate the eigenvalues and 		|
 | eigenvectors of a linear chain of n-masses and (n+1)springs		|
 | where n is even. Using Jacobian's method for sym. matrix			|
 |		The chain is limited up to diatomic chain									|
******************************************************/


#include <iostream>
using namespace std;
#include <math.h>
//stdlib for memory allocation/ dynamic memory
#include <stdlib.h>

double** matrix_alloc(int nrows, int ncolumns);
void matrix_init( double **array, int n);
void matrix_init_identity( double **array, int n);
void update( int k, double t, double& y, double e[], bool changed[], int& state);
/*void update(index k, parameter t, parameter y, eigenvector e[], changed[], state*/
void rotate(int k, int l, int i, int j, double **matrix, double c, double s);
int maxind (int k, double **array, int n);

int main(){
	int n=10;			// n need to be even to create continous boundary
	double **E, **S;
	//dynamic memory allocation to E[][] and S[][]
	E = matrix_alloc( n, n);
	S = matrix_alloc( n, n);
	//varible declaration
	double e[n];
	int i, k, l, m, state;
	double s, c, t, p, y;
	int ind[n];
	bool changed[n];
	//manually set the spring constant nad mass
	double spring[2] = {10, 10};
	double mass [2] = {1, 1};

	//Assign components to matrix S[][] only works for even nxn matrix
	for ( i = 0; i < (n-2); i = i+2){ //set for upper, lower diagonal and diagonal
		S[i][i+1] = -spring[1]/mass[0];
		S[i+1][i] = -spring[1]/mass[1];
		S[i][i] = (spring[0] + spring[1]) / mass[0];
	};
	for ( i = 1; i < (n-3); i = i+2){
		S[i][i+1] = -spring[0]/mass[1];
		S[i+1][i] = -spring[0] / mass[0];
		S[i][i] = (spring[0] + spring[1]) / mass[1];
	};
	S[n-1][n-1] = (spring[0] + spring[1]) / mass[1];

	
	//init E[][] and arrays ind[], e[], changed[]
	matrix_init_identity( E, n);
	state = n;
	for (k=0; k<n; k++){
		ind[k] = maxind( k, S, n);
		e[k] = S[k][k];
		changed[k] = true;
	};
	//rotation around biggest index - pivot p(k,l)- by Givens' matrix
	while ( state != 0) {
		m = 0;
		//find pivot p( k, l)
		for ( k=1; k < (n-1); k++){
			if ( fabs( S[k][ind[k]]) > fabs(S[m][ind[m]]))
				m = k;
		};
		//prepare Givens' matrix's components
		k = m;
		l = ind[m];
		p = S[k][l];
		y = (e[l] - e[k])/2;		t = fabs(y) + sqrt( p*p + y*y );
		s = sqrt( p*p + t*t);		
		c = t/s;
		s = p/s;
		t = p*p/t;
		if (y<0){
			s = -s;
			t = -t;
		};
		S[k][l] = 0;
		update( k, -t, y, e, changed, state);
		update( l, t, y, e, changed, state);
		//rotate rows and columns k, l
		for ( i = 0; i < k; i++) rotate(i, k, i, l, S, c, s);
		for ( i = k + 1; i < l; i++) rotate( k, i, i, l, S, c, s);
		for ( i = l + 1; i < n; i++) rotate( k, i, l, i, S, c, s);
		//rotate eigenvectors
		for ( i = 0; i < n; i++) rotate ( k, i, l, i, E, c, s);
		// update rows k, l by changing ind[k], ind[l]
		ind[k] = maxind( k, S, n);
		ind[l] = maxind(l, S, n);
	};
	//cout << data on screen
	for (i = 0; i < n; i++){
		cout << "Eigenvalues are" ;
		cout << e[i] <<endl;
		cout <<"Correspond eigenvectors are " <<endl;
		for (int j = 0; j < n; j++){
			cout<<"     "<< E[i][j];
		};
		cout << endl;		
};

//***************Function*******************
double** matrix_alloc ( int nrows, int ncolumns){
	double **array;

	array = (double**)malloc(nrows * sizeof(int *));
	if(array == NULL){
		cout << "error allocating memory"<<endl;
		};
	for(int i = 0; i < nrows; i++){
		array[i] = (double*)malloc(ncolumns * sizeof(int));
		if ( array[i] == NULL){
			cout << "error allocating memory"<<endl;
		};
	};
	return array;
};
void matrix_init( double **array, int n){
	for (int i=0; i < n; i++){
		for (int j=0; j < n; j++) {
			array[i][j] = 0;
		};
	     };
};
void matrix_init_identity (double **array, int n){
	for (int i=0; i < n; i++){
		for (int j=0; j < n; j++) {
			if (i == j){
				array[i][j] = 1;
			}else{
				array[i][j] = 0;
			};
		};
	     };
};
void update( int k, double t, double& y, double e[], bool changed[], int& state){
	y = e[k];
	e[k] = y + t;
	if (changed[k] && (y = e[k])){
		changed[k] = false;
		state = state - 1;
	}else{
		if ((!changed[k]) && (y != e[k])){
			changed[k] = true;
			state = state + 1;
		};
	};
};
void rotate(int k, int l, int i, int j, double **matrix, double c, double s){
	double a, b;
	a = c*matrix[k][l] - s*matrix[i][j];
	b = s*matrix[k][l] + c*matrix[i][j];
	matrix[k][l] = a;
	matrix[i][j] = b;
};
int maxind (int k, double **array, int n){ 
	/*find maximum component, off diagonal in (row k) of matrix (array) size (n)*/
	int m = k+1;
	for (int i = k+2; i < n; i++){
		if ( fabs(array[k][i]) > fabs( array[k][m]))
		{m = i;}
	};
};

Last edited on
Yes you're doing it correctly.

The problem is you are missing a } in main. Count your braces.
thank you Disch,
I am grateful for your help

regards,
N.Le
Topic archived. No new replies allowed.