Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "linalg.h"
- typedef struct matrix
- {
- size_t mrows;
- size_t ncols;
- double *arr;
- } matrix;
- void MatDestruct(Mat mat)
- {
- if(mat==NULL) // Matrix doesn't have memory allocated to it.
- {
- fprintf(stderr,"Error: mat points to NULL in MatDestruct.\n");
- exit(EXIT_FAILURE);
- }
- double compare=(mat->mrows)*(mat->ncols);
- if(compare>ULONG_MAX) // Matrix dimensions are too large.
- {
- fprintf(stderr,"Error: input dimensions out of range in MatDestruct.\n");
- exit(EXIT_FAILURE);
- }
- if(mat->arr!=NULL) // Safeguard against using this function on already initialized matrices.
- {
- for(size_t i=0;i<(mat->mrows)*(mat->ncols);i++) // Zeroes the data before freeing the memory.
- {
- mat->arr[i]=0;
- }
- free(mat->arr);
- }
- mat->mrows=0;
- mat->ncols=0;
- mat->arr=NULL;
- free(mat);
- mat=NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement