# Forum: µC & Digital Electronics Double pointer "freeze" on uC

 Author: Josh (Guest) Posted on: 2011-02-15 16:45

Rate this post
 0 ▲ useful ▼ not useful
Hello folks!

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
calculation. Therefor iam working with double pointers, structs and 2
dimensional arrays.

######################## Code snippet ####################

#################################### matrix struct (incl col,row)

typedef struct {
int  row;
int  col;

typedef struct {
/*
* only the starting address of the following will be
* returned to the C programmer, like malloc() concept
*/
double  *matrix;
}  MATBODY;

typedef  double **MATRIX;

############################################# 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 ));
}

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.

Does anybody know where the problem is?
Did anyone make similar experiences??

I hope someone can help me!

thank you!

 Author: THaala (Guest) Posted on: 2011-02-15 23:43

Rate this post
 0 ▲ useful ▼ not useful
hi,

because i didn't understand your code depply - i have tested your code
inside devstudio 2010. Except you forgot to post the function mat_fill()
i cannot see any problem. However - do you have had a look to the
assembler output ?

regards ,
T

 Author: Josh (Guest) Posted on: 2011-02-16 13:37

Rate this post
 0 ▲ useful ▼ not useful
Hi T!

The code is working now - it was a platform-own function which
initializes the memory management routines and which was missing.
Afterwards i could use malloc etc. and write values to the matrices!

Despite of that, thanks for your work!

• $formula (LaTeX syntax)$