I have an application that runs on an ARM Cortex-M based MCU and is written in C and C++. I use gcc
and g++
to compile it and would like to completely disable any heap usage.
In the MCU startup file the heap size is already set to 0. In addition to that, I would also like to disallow any accidental heap use in the code.
In other words, I would like the linker (and/or the compiler) to give me an error when the malloc
, calloc
, free
functions or the new
, new[]
, delete
, delete[]
operators are used.
So far I've tried -nostdlib
which gives me issues like undefined reference to _start
. I also tried -nodefaultlibs
but that one still does not complain when I try to call malloc
. What is the right way to do this?
Notes:
- This app runs on “bare metal”, there is no operating system.
- I would also like to avoid any malloc usage in 3rd-party code (vendor-specific libraries, the standard library, printf etc.).
- I'm fully okay with not using the parts of the C / C++ standard libraries that would require dynamic memory allocations.
- I'd prefer a compile-time rather than a run-time solution.
printf()
will usemalloc()
/free()
internally. And with C++ involved, you probably can't use anything that relies on the C++ run-time library. Simply loading the C++ run-time library probably makes extensive use of the heap. I'd venture to say that this would have to be an application requirement from the very beginning of the design to be successful. – Andrew Henle#define malloc(dummy) NULL; _Static_assert(0, "Err: use of dynamic memory")
. Not pretty but fully portable. – Lundin