Utilizing dynamic memory with calloc() and malloc()

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

cross mob
Anonymous
Not applicable

Hi Everyone,

I am using a cyble-224116-01 BLE module along with PSOC Creator 4.2. I include the stdlib.h library and my code compiles correctly, however, a call to calloc() or malloc() always returns null. I was wondering if I had to do anything special for it to work properly? Or is calloc() and malloc() not operable for my device?

Regards, Uziel.

0 Likes
1 Solution
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

Have you changed the heap size?

Go to Design Wide Resources > Systems Tab > Heap Size

Set it to 0x800 bytes for example, but you can allocate more based on your requirements.

After that let me know if the allocation works or not.

Regards,

Dheeraj

View solution in original post

0 Likes
4 Replies
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

Have you changed the heap size?

Go to Design Wide Resources > Systems Tab > Heap Size

Set it to 0x800 bytes for example, but you can allocate more based on your requirements.

After that let me know if the allocation works or not.

Regards,

Dheeraj

0 Likes
Anonymous
Not applicable

Thank you for your reply! I did try that and it was working returning me an address, I tried it again today, and get null returned once again. What can be the problem?

Regards,

Uziel.

0 Likes

I am using malloc() frequently and I never had any issues with it.  As Dheera stated it is essential to set the heap size to the required amount (plus some extra for malloc's housekeeping). Only when there is no contiguous block of memory with the requires size is found NULL will be returned. When there are still any doubts, please post your complete project or a shortened version that shows the error so that we all can have a look at all of your settings. To do so, use

Creator->File->Create Workspace Bundle (minimal)

and attach the resulting file.

Bob

0 Likes

Are you saying that the memory allocations are working sometimes and other times you receive a NULL? If this is the case, then it be specific to your project in which something is utilizing the heap and hence the allocation is failing.

Regards,

Dheeraj

0 Likes