patterncMinor
Is this a valid usage of structure assignment in C?
Viewed 0 times
thisassignmentusagestructurevalid
Problem
Q: Please comment on the usage of structures and structure assignment operations in C
I am working on converting a MATLAB program to C using BLAS and LAPACK for linear algebra support. The MATLAB code uses cell arrays. I created a Matrix datatype and a Cell data-type.
A section of the header file/implementation:
```
#define ASSERT(c,m)
#define PREC double
#define ZEROS(r,c) (zeros(r,c))
#define ONES(r,c) (ones(r,c))
#define EYE(r,c) (eye(r,c))
#define ALLOCM(r,c) (alloc_matrix(r,c))
#define PRINTM(M) (print_matrix(M))
#define FREEM(M) (free_matrix(M))
#define ALLOCC(r,c) (alloc_cell(r,c))
#define GETMC(C,r,c) (get_matrix_from_cell(C,r,c))
#define SETMC(C,r,c,M) (set_matrix_in_cell(C,r,c,M))
#define FREEC(C) (free_cell(C))
/ Matrix /
typedef struct {
PREC * array;
int rows; // The number of rows in the matrix
int cols; // The number of columns in the matrix
}Matrix;
/ Cell of Matrices /
typedef struct {
Matrix * array; // Cell array of matrices stored in row major form
int rows; // Number of rows in Cell array
int cols; // Number of cols in Cell array
}Cell;
/ Matrix utility functions /
Matrix alloc_matrix(int rows, int cols);
Matrix zeros(int rows, int cols);
Matrix ones(int rows, int cols);
Matrix eye(int rows, int cols);
Matrix corrcov(Matrix matrix);
void print_matrix(Matrix matrix);
void free_matrix(Matrix matrix);
/ Cell array utility functions /
Cell alloc_cell(int rows, int cols);
INLINE Matrix get_matrix_from_cell(Cell cell, int row, int col);
INLINE void set_matrix_in_cell(Cell cell, int row, int col, Matrix matrix);
void free_cell(Cell cell);
// Implementation
Matrix
alloc_matrix(int rows, int cols){
Matrix matrix;
ASSERT(rows > 0 && cols > 0, FATAL_NEGATIVE_DIMENSIONS);
matrix.array = (PREC ) malloc(sizeof(PREC) rows * cols);
ASSERT(matrix.array != NULL, FATAL_NO_MEMORY);
matrix.rows = rows;
matrix.cols = cols;
return matrix;
}
Matrix
zeros(int rows, int cols){
int i;
int size;
M
I am working on converting a MATLAB program to C using BLAS and LAPACK for linear algebra support. The MATLAB code uses cell arrays. I created a Matrix datatype and a Cell data-type.
A section of the header file/implementation:
```
#define ASSERT(c,m)
#define PREC double
#define ZEROS(r,c) (zeros(r,c))
#define ONES(r,c) (ones(r,c))
#define EYE(r,c) (eye(r,c))
#define ALLOCM(r,c) (alloc_matrix(r,c))
#define PRINTM(M) (print_matrix(M))
#define FREEM(M) (free_matrix(M))
#define ALLOCC(r,c) (alloc_cell(r,c))
#define GETMC(C,r,c) (get_matrix_from_cell(C,r,c))
#define SETMC(C,r,c,M) (set_matrix_in_cell(C,r,c,M))
#define FREEC(C) (free_cell(C))
/ Matrix /
typedef struct {
PREC * array;
int rows; // The number of rows in the matrix
int cols; // The number of columns in the matrix
}Matrix;
/ Cell of Matrices /
typedef struct {
Matrix * array; // Cell array of matrices stored in row major form
int rows; // Number of rows in Cell array
int cols; // Number of cols in Cell array
}Cell;
/ Matrix utility functions /
Matrix alloc_matrix(int rows, int cols);
Matrix zeros(int rows, int cols);
Matrix ones(int rows, int cols);
Matrix eye(int rows, int cols);
Matrix corrcov(Matrix matrix);
void print_matrix(Matrix matrix);
void free_matrix(Matrix matrix);
/ Cell array utility functions /
Cell alloc_cell(int rows, int cols);
INLINE Matrix get_matrix_from_cell(Cell cell, int row, int col);
INLINE void set_matrix_in_cell(Cell cell, int row, int col, Matrix matrix);
void free_cell(Cell cell);
// Implementation
Matrix
alloc_matrix(int rows, int cols){
Matrix matrix;
ASSERT(rows > 0 && cols > 0, FATAL_NEGATIVE_DIMENSIONS);
matrix.array = (PREC ) malloc(sizeof(PREC) rows * cols);
ASSERT(matrix.array != NULL, FATAL_NO_MEMORY);
matrix.rows = rows;
matrix.cols = cols;
return matrix;
}
Matrix
zeros(int rows, int cols){
int i;
int size;
M
Solution
About your valgrind issue
I don't think the valgrind warning is about uninitialized fields in Matrix. Your
I have no valgrind warnings. Sharing a snippet of code wich really produces the valgrind warning would help.
Other remarks
I don't think the valgrind warning is about uninitialized fields in Matrix. Your
zeros functions does the initialization, right? I tested myself with this code:Matrix zeros(int rows, int cols) {
Matrix tmp;
int i;
tmp.rows = rows;
tmp.cols = cols;
tmp.array = malloc(sizeof(float) * rows * cols);
for(i = 0; i < rows * cols; i++)
tmp.array[i] = 0;
return tmp;
}I have no valgrind warnings. Sharing a snippet of code wich really produces the valgrind warning would help.
Other remarks
- If you want to free the memory automatically and have your allocated memory copied, C++ will help.
- I don't understand your point about dot syntax though. You're only adding one level of nesting, this does not mean you're going to use "*" or "->" any less.
matrix->array[i]is wrong since->and[]both dereference your pointer: this is not going to compile
- Why don't you use a PREC** pointer? It is probably easier to use and will avoid errors
- Are you trying to make your MATLAB code fast? Matrix operations are probably very fast using matlab. If there's another part of your code wich is slow, consider using MEX-files.
Code Snippets
Matrix zeros(int rows, int cols) {
Matrix tmp;
int i;
tmp.rows = rows;
tmp.cols = cols;
tmp.array = malloc(sizeof(float) * rows * cols);
for(i = 0; i < rows * cols; i++)
tmp.array[i] = 0;
return tmp;
}Context
StackExchange Code Review Q#9439, answer score: 2
Revisions (0)
No revisions yet.