PSoC5 malloc matrix

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

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=m[i-1]+ncol;

   

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

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

View solution in original post

0 Likes
6 Replies
Anonymous
Not applicable

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[4];
double m4x4_data[4][4];
int i;
for (i=0; i<4;i++) m4x4_ptr_array = &m4x4_data[-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!

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Anonymous
Not applicable

Hello Bob

   

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

0 Likes
Anonymous
Not applicable

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?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

0 Likes