Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the memory allocation method of matd_t to comply with ISO standards #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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