We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello!
Iam currently working on a project with the XC2785X-104F Controller, uVision 4 and Keil C166 Compiler. Within my code i have to work with matrices and several matrix calculations. Therefor iam working with double pointers, structs and 2 dimensional arrays.
######################### Code snippet ###################
#################################### matrix struct (incl col,row)
typedef struct { int row; int col; } MATHEAD;
typedef struct { MATHEAD head; double *matrix; } MATBODY;
typedef double **MATRIX;
#define Mathead(a) ((MATHEAD *)((MATHEAD *)(a) - 1)) #define MatRow(a) (Mathead(a)->row) #define MatCol(a) (Mathead(a)->col)
############################################# struct to work with
typedef struct { MATRIX A, B, C; double var;
}structure;
############################################ func to "create" matrices
MATRIX mat_creat( row, col, type ) int row, col, type; { MATRIX A;
if ((A =_mat_creat( row, col )) != NULL) { return (mat_fill(A, type)); } else return (NULL);
return (A); }
MATRIX _mat_creat( row, col ) int row, col; { MATBODY *mat; int i;
if ((mat = (MATBODY *)malloc( sizeof(MATHEAD) + sizeof(double *) * row)) == NULL) return NULL;//(mat_error( MAT_MALLOC ));
for (i=0; i<row; i++) { if ((*((double **)(&mat->matrix) + i) = (double *)malloc(sizeof(double) * col)) == NULL) return NULL;//(mat_error( MAT_MALLOC )); }
mat->head.row = row; mat->head.col = col;
return (&(mat->matrix)); //(& ) }
########################### main.c
structure test1;
// value for var test1.var = 1.234;
// send data via uart to terminal send_uart(test1.var); // works fine
// create a 2x1 matrix with unknown data test1.A = mat_creat(2,1,UNDEFINED);
// fill matrix with values test1.A[0][0] = 0x00; // <--------- causes uC-freeze at this point
// send data via uart to terminal send_uart(test1.A[0][0]); // is not delivered send_uart("TEST\n"); // is not delivered
########################## END Code snippet ##############################
If i comment that line out (// test1.A[0][0] = 0x00;) everything works fine. If not, there is no error nor a warning from the compiler. Just a freeze on the uC.
In my eyes the error lies in the usage of the double pointer, but i can't find the mistake. Also tried the same code in eclipse with gnu c compiler and everything worked fine...
Does anybody know where the problem is? Did anyone make similar experiences??
I hope someone can help me!
thank you!
Bye, Josh
He also wrote: "Next you're putting entirely too many casts into that code." and not "You have casts in that code."
No claim that zero casts are the only allowed count.
Someone see a criticism and get migthy upset, stamp the little feet in the floor and cries.
If you can not handle criticisms, stay away from organized jobs where regular code reviews are the rule. At my first such job I learned a lot and my code became far more 'natural' and thus more mainatainable.
Above (paraphrasing) there is a defensive statement "my code has been maintained by others" SO WHAT. I have maintained much code written by others the diffrence is not whether it was possible, but how much effort it took.
Erik