Skip to content

Commit

Permalink
Modify the memory allocation method of matd_t
Browse files Browse the repository at this point in the history
Add '-Wpedantic' flag to CMake
  • Loading branch information
zhaoxi-scut committed Nov 16, 2024
1 parent 4dfd338 commit a91a5a3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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)
add_compile_options(-Wno-shift-negative-value)
endif()

Expand Down
6 changes: 4 additions & 2 deletions common/matd.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ 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;
}
Expand Down Expand Up @@ -220,7 +221,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 a91a5a3

Please sign in to comment.