This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

When should I use nrf_calloc (malloc) instead static var?

When should I use nrf_calloc (malloc) instead of static declared variable on nrf52840 chip. I mean it has a huge ram memory for as much static declared variables as I would want ever. So my thinking about dynamic memory allocation:

So should I use this future when I work with external data sources (the size of those can be different time to time):

Blutooth, mqtt, wifi, ethernet and etc.

I mean of course it would be justified 10 years ago when MCUs had much less ram

If I understand correctly most profit we can get if we use calloc everywhere in my program. But in this case I'll get much more routines compare with simple global var declared + don't forgetting to free allocated memory, analyze how optimize the blocks use and etc

So the delema:

On one side all of my projects have several radio sources of data (although the max size of most of them are known)

On other side we use nrf52840 with 256kB of ram which very difficult to fill in completely on C. Also I read somewhere calloc, malloc are expensive functions for core itself (so maybe the power consumption is more). 

Conclusion:

Which is better solution in my case:

1 Use nrf_calloc everywhere in my code (where we have arrays)

2 Combine with static arrays. 

3 don't use at all nowadays.

Related