Skip to content

Commit

Permalink
Modify the memory allocation method of matd_t
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxi-scut committed Nov 17, 2024
1 parent 4dfd338 commit 6d0f9e0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 6 additions & 3 deletions common/matd.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ matd_t *matd_create(int rows, int cols)
if (rows == 0 || cols == 0)
return matd_create_scalar(0);

matd_t *m = calloc(1, sizeof(matd_t) + (rows*cols*sizeof(double)));
matd_t *m = calloc(1, sizeof(matd_t));
m->nrows = rows;
m->ncols = cols;
m->data = calloc(rows * cols, sizeof(double));

return m;
}

matd_t *matd_create_scalar(TYPE v)
{
matd_t *m = calloc(1, sizeof(matd_t) + sizeof(double));
matd_t *m = calloc(1, sizeof(matd_t));
m->nrows = 0;
m->ncols = 0;
m->data = calloc(1, sizeof(double));
m->data[0] = v;

return m;
Expand Down Expand Up @@ -220,7 +222,8 @@ void matd_destroy(matd_t *m)
if (!m)
return;

assert(m != NULL);
assert(m->data != NULL);
free(m->data);
free(m);
}

Expand Down
3 changes: 1 addition & 2 deletions common/matd.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ extern "C" {
typedef struct
{
unsigned int nrows, ncols;
double data[];
// double *data;
double *data;
} matd_t;

#define MATD_ALLOC(name, nrows, ncols) double name ## _storage [nrows*ncols]; matd_t name = { .nrows = nrows, .ncols = ncols, .data = &name ## _storage };
Expand Down

0 comments on commit 6d0f9e0

Please sign in to comment.