How to store the returned pointer of malloc/calloc?

Technical questions regarding the XTC tools and programming with XMOS.
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

How to store the returned pointer of malloc/calloc?

Post by themech »

Hi,

I tried to get an answer for this question in the Q&A section but had no luck. Hopefully somebody has a solution here.
I am trying to allocate memory for a linked list I need. Therefore I use calloc which should give me a pointer of no type. This is why I typecast it to (int *):

Code: Select all

#include <stdlib.h>

int main(void){
    unsafe { 
        int * unsafe pdata = (int * unsafe)calloc(1, sizeof(int));
    }
    return 0; 
}

####
Debug error:
warning: C36258: pointer cast to type of larger alignment
Unfortunately it seems that an unsafe pointer has a different alignment. Has somebody an idea to implement malloc/calloc right, or how to allocate memory within xc?


themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

Any ideas or other solutions to implement a linked list?
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

That message is just a warning and, in this case, it is safe to ignore. The memory returned by calloc is 32bit aligned so the cast is safe. Is the warning the only issue you ran into?
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

Hi,

yes i think so I just wanted to avoid any nasty memory errors later on. Thanks for your reply!