Skip to content

Commit 41909fd

Browse files
committed
Add missed memory deallocation to LAPACKE DGESV examples
1 parent 21299d2 commit 41909fd

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

LAPACKE/example/example_DGESV_colmajor.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ int main(int argc, char **argv) {
6666
A = (double *)malloc(n*n*sizeof(double)) ;
6767
if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
6868
b = (double *)malloc(n*nrhs*sizeof(double)) ;
69-
if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
69+
if (b==NULL){ printf("error of memory allocation\n"); free(A); exit(0); }
7070
ipiv = (lapack_int *)malloc(n*sizeof(lapack_int)) ;
71-
if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }
71+
if (ipiv==NULL){ printf("error of memory allocation\n"); free(A); free(b); exit(0); }
7272

7373
for( i = 0; i < n; i++ ) {
7474
for( j = 0; j < n; j++ ) A[i+j*lda] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
@@ -94,9 +94,17 @@ int main(int argc, char **argv) {
9494
printf( "The diagonal element of the triangular factor of A,\n" );
9595
printf( "U(%" LAPACK_IFMT ",%" LAPACK_IFMT ") is zero, so that A is singular;\n", info, info );
9696
printf( "the solution could not be computed.\n" );
97+
free(A);
98+
free(b);
99+
free(ipiv);
97100
exit( 1 );
98101
}
99-
if (info <0) exit( 1 );
102+
if (info <0) {
103+
free(A);
104+
free(b);
105+
free(ipiv);
106+
exit( 1 );
107+
}
100108
/* Print solution */
101109
print_matrix_colmajor( "Solution", n, nrhs, b, ldb );
102110
/* Print details of LU factorization */

LAPACKE/example/example_DGESV_colmajor_64.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ int main(int argc, char **argv) {
6666
A = (double *)malloc(n*n*sizeof(double)) ;
6767
if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
6868
b = (double *)malloc(n*nrhs*sizeof(double)) ;
69-
if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
69+
if (b==NULL){ printf("error of memory allocation\n"); free(A); exit(0); }
7070
ipiv = (int64_t *)malloc(n*sizeof(int64_t)) ;
71-
if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }
71+
if (ipiv==NULL){ printf("error of memory allocation\n"); free(A); free(b); exit(0); }
7272

7373
for( i = 0; i < n; i++ ) {
7474
for( j = 0; j < n; j++ ) A[i+j*lda] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
@@ -94,15 +94,26 @@ int main(int argc, char **argv) {
9494
printf( "The diagonal element of the triangular factor of A,\n" );
9595
printf( "U(%" LAPACK_IFMT ",%" LAPACK_IFMT ") is zero, so that A is singular;\n", info, info );
9696
printf( "the solution could not be computed.\n" );
97+
free(A);
98+
free(b);
99+
free(ipiv);
97100
exit( 1 );
98101
}
99-
if (info <0) exit( 1 );
102+
if (info <0) {
103+
free(A);
104+
free(b);
105+
free(ipiv);
106+
exit( 1 );
107+
}
100108
/* Print solution */
101109
print_matrix_colmajor_64( "Solution", n, nrhs, b, ldb );
102110
/* Print details of LU factorization */
103111
print_matrix_colmajor_64( "Details of LU factorization", n, n, A, lda );
104112
/* Print pivot indices */
105113
print_vector_64( "Pivot indices", n, ipiv );
114+
free(A);
115+
free(b);
116+
free(ipiv);
106117
exit( 0 );
107118
} /* End of LAPACKE_dgesv Example */
108119

LAPACKE/example/example_DGESV_rowmajor.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ int main(int argc, char **argv) {
6565
A = (double *)malloc(n*n*sizeof(double)) ;
6666
if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
6767
b = (double *)malloc(n*nrhs*sizeof(double)) ;
68-
if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
68+
if (b==NULL){ printf("error of memory allocation\n"); free(A); exit(0); }
6969
ipiv = (lapack_int *)malloc(n*sizeof(lapack_int)) ;
70-
if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }
70+
if (ipiv==NULL){ printf("error of memory allocation\n"); free(A); free(b); exit(0); }
7171

7272
for( i = 0; i < n; i++ ) {
7373
for( j = 0; j < n; j++ ) A[i*lda+j] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
@@ -91,9 +91,17 @@ int main(int argc, char **argv) {
9191
printf( "The diagonal element of the triangular factor of A,\n" );
9292
printf( "U(%" LAPACK_IFMT ",%" LAPACK_IFMT ") is zero, so that A is singular;\n", info, info );
9393
printf( "the solution could not be computed.\n" );
94+
free(A);
95+
free(b);
96+
free(ipiv);
9497
exit( 1 );
9598
}
96-
if (info <0) exit( 1 );
99+
if (info <0) {
100+
free(A);
101+
free(b);
102+
free(ipiv);
103+
exit( 1 );
104+
}
97105
/* Print solution */
98106
print_matrix_rowmajor( "Solution", n, nrhs, b, ldb );
99107
/* Print details of LU factorization */
@@ -105,7 +113,6 @@ int main(int argc, char **argv) {
105113
free(A);
106114
free(b);
107115
free(ipiv);
108-
109116
exit( 0 );
110117
} /* End of LAPACKE_dgesv Example */
111118

LAPACKE/example/example_DGESV_rowmajor_64.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ int main(int argc, char **argv) {
6565
A = (double *)malloc(n*n*sizeof(double)) ;
6666
if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
6767
b = (double *)malloc(n*nrhs*sizeof(double)) ;
68-
if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
68+
if (b==NULL){ printf("error of memory allocation\n"); free(A); exit(0); }
6969
ipiv = (int64_t *)malloc(n*sizeof(int64_t)) ;
70-
if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }
70+
if (ipiv==NULL){ printf("error of memory allocation\n"); free(A); free(b); exit(0); }
7171

7272
for( i = 0; i < n; i++ ) {
7373
for( j = 0; j < n; j++ ) A[i*lda+j] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
@@ -91,15 +91,26 @@ int main(int argc, char **argv) {
9191
printf( "The diagonal element of the triangular factor of A,\n" );
9292
printf( "U(%" LAPACK_IFMT ",%" LAPACK_IFMT ") is zero, so that A is singular;\n", info, info );
9393
printf( "the solution could not be computed.\n" );
94+
free(A);
95+
free(b);
96+
free(ipiv);
9497
exit( 1 );
9598
}
96-
if (info <0) exit( 1 );
99+
if (info <0) {
100+
free(A);
101+
free(b);
102+
free(ipiv);
103+
exit( 1 );
104+
}
97105
/* Print solution */
98106
print_matrix_rowmajor_64( "Solution", n, nrhs, b, ldb );
99107
/* Print details of LU factorization */
100108
print_matrix_rowmajor_64( "Details of LU factorization", n, n, A, lda );
101109
/* Print pivot indices */
102110
print_vector_64( "Pivot indices", n, ipiv );
111+
free(A);
112+
free(b);
113+
free(ipiv);
103114
exit( 0 );
104115
} /* End of LAPACKE_dgesv Example */
105116

0 commit comments

Comments
 (0)