diff --git a/sPuReMD/src/lin_alg.c b/sPuReMD/src/lin_alg.c
index 69b69b6fe90f2702833df578f137b8d35214bf1b..c40baca37319a6ff6809b7eacf658f3c568db20c 100644
--- a/sPuReMD/src/lin_alg.c
+++ b/sPuReMD/src/lin_alg.c
@@ -113,13 +113,15 @@ static int compare_matrix_entry(const void *v1, const void *v2)
 void Sort_Matrix_Rows( sparse_matrix * const A )
 {
     unsigned int i, j, si, ei;
+    size_t temp_size;
     sparse_matrix_entry *temp;
 
 #if defined(_OPENMP)
 //    #pragma omp parallel default(none) private(i, j, si, ei, temp) shared(stderr)
 #endif
     {
-        temp = smalloc( sizeof(sparse_matrix_entry) * (A->n + 1), __FILE__, __LINE__ );
+        temp = NULL;
+        temp_size = 0;
 
         /* sort each row of A using column indices */
 #if defined(_OPENMP)
@@ -130,6 +132,16 @@ void Sort_Matrix_Rows( sparse_matrix * const A )
             si = A->start[i];
             ei = A->start[i + 1];
 
+            if ( temp_size < ei - si )
+            {
+                if ( temp != NULL )
+                {
+                    sfree( temp, __FILE__, __LINE__ );
+                }
+                temp = smalloc( sizeof(sparse_matrix_entry) * (ei - si), __FILE__, __LINE__ );
+                temp_size = ei - si;
+            }
+
             for ( j = 0; j < (ei - si); ++j )
             {
                 temp[j].j = A->j[si + j];