6 Replies Latest reply on May 18, 2016 10:24 AM by JoMe_264151

# PSoC5 malloc matrix

Hello

I try to use matrices in PSoC5. Therefore I found good basic routines to work with matrices. But I get a allocation failure when I try to allocate a 4x4 double matrix. Do anyone know what I have to improve to that?

Here the code to allocate the matrix:

/*Allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]*/

double **matrix(long nrl, long nrh, long ncl, long nch)
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;

/* allocate pointers to rows */
m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
if (!m) pr_error("allocation failure 1 in LINALG_dmatrix()");
m += NR_END;
m -= nrl;

/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
if (!m[nrl]) pr_error("allocation failure 2 in LINALG_dmatrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;

for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;

/* return pointer to array of pointers to rows */
return m;
}

• ###### 1. Re: PSoC5 malloc matrix

I found a temporary solution to my problem, but I don't like it:

Instead of using the malloc-function I did it like this:

double **m4x4;
double * m4x4_ptr_array;
double m4x4_data;
int i;
for (i=0; i<4;i++) m4x4_ptr_array[i] = &m4x4_data[i][-1];
m4x4 = &m4x4_ptr_array[-1];

If anybody has an idea why the malloc function gives a allocation failure, I am still interested in it!

• ###### 2. Re: PSoC5 malloc matrix

why the malloc function gives a allocation failure  Did you put aside enough heap memory? in .cydwr view, system tab, set the heap to as much memory as you need + 0x0200 bytes.

Bob

• ###### 3. Re: PSoC5 malloc matrix

Hello Bob

Thank you for your answer! Heap Size? When I look in the .cydwr view, there is written: 0x80. What means this exactly?

• ###### 4. Re: PSoC5 malloc matrix

0x80 bytes for heap. A bit few. When you use sprintf increase that to 0x0200 (bytes)

Bob

• ###### 5. Re: PSoC5 malloc matrix

Ok, I found here something about the heap. So when I increase this storage, some library-functions like sprinf and malloc have more space. So do I have increase this value, until these failures are disappeared? Do you know any rule how to dimension this value?

• ###### 6. Re: PSoC5 malloc matrix

As I said: set the heap to as much memory as you need + 0x0200 bytes.

Bob