Skip to content

Commit

Permalink
Merge pull request #362 from zhaoxi-scut/matd_memory_fix
Browse files Browse the repository at this point in the history
Modify the memory allocation method of `matd_t` to comply with ISO standards
  • Loading branch information
christian-rauch authored Nov 26, 2024
2 parents 4dfd338 + 0106e3e commit 724a7d8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ endif()

if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Werror)
# add_compile_options(-Wpedantic)
add_compile_options(-Wpedantic)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
add_compile_options(
-Wno-gnu-zero-variadic-macro-arguments
-Wno-strict-prototypes
-Wno-static-in-inline
)
endif()
add_compile_options(-Wno-shift-negative-value)
endif()

Expand Down
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 724a7d8

Please sign in to comment.