[math] Only reallocate on matrix_resize if needed

This commit is contained in:
Al
2015-12-07 01:20:16 -05:00
parent cfd0dc69f2
commit a066ee9aad

View File

@@ -23,10 +23,13 @@ matrix_t *matrix_new(size_t m, size_t n) {
bool matrix_resize(matrix_t *self, size_t m, size_t n) { bool matrix_resize(matrix_t *self, size_t m, size_t n) {
if (self == NULL) return false; if (self == NULL) return false;
// If the new size is smaller, don't reallocate, just ignore the extra values
if (m * n > (self->m * self->n)) {
matrix->values = realloc(sizeof(double) * m * n); matrix->values = realloc(sizeof(double) * m * n);
if (matrix->values == NULL) { if (matrix->values == NULL) {
return false; return false;
} }
}
self->m = m; self->m = m;
self->n = n; self->n = n;