Fix too short allocations and missing initialisation when allocation/copying vectors...
authorChristoph Mallon <christoph.mallon@gmx.de>
Fri, 3 Oct 2008 19:15:34 +0000 (19:15 +0000)
committerChristoph Mallon <christoph.mallon@gmx.de>
Fri, 3 Oct 2008 19:15:34 +0000 (19:15 +0000)
[r22447]

matrix.c

index f6b5356..3f19844 100644 (file)
--- a/matrix.c
+++ b/matrix.c
@@ -1,44 +1,32 @@
-#include "assert.h"
+#include <assert.h>
+#include <string.h>
 
 #include "pbqp_t.h"
 #include "matrix.h"
 
 pbqp_matrix *pbqp_matrix_alloc(pbqp *pbqp, unsigned rows, unsigned cols)
 {
-       pbqp_matrix *mat;
-       unsigned index;
-
        assert(cols> 0);
        assert(rows> 0);
 
        unsigned length = rows * cols;
 
-       mat = obstack_alloc(&pbqp->obstack, sizeof(*mat) + sizeof(num) * (length - 1));
+       pbqp_matrix *mat = obstack_alloc(&pbqp->obstack, sizeof(*mat) + sizeof(*mat->entries) * length);
        assert(mat);
 
        mat->cols = cols;
        mat->rows = rows;
-       for (index = 0; index < length; ++index) {
-               mat->entries[index] = 0;
-       }
+       memset(mat->entries, 0, sizeof(*mat->entries) * length);
 
        return mat;
 }
 
 pbqp_matrix *pbqp_matrix_copy(pbqp *pbqp, pbqp_matrix *m)
 {
-       int i;
-       int len;
-       pbqp_matrix *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy));
-
+       unsigned     len  = m->rows * m->cols;
+       pbqp_matrix *copy = obstack_copy(&pbqp->obstack, m, sizeof(*copy) + sizeof(*copy->entries) * len);
        assert(copy);
 
-       len = m->rows * m->cols;
-
-       for (i = 0; i < len; ++i) {
-               copy->entries[i] = m->entries[i];
-       }
-
        return copy;
 }