Points to be considered while designing or coding for lesser footprint deliverables

StackOverflow https://stackoverflow.com/questions/2094895

  •  21-09-2019
  •  | 
  •  

Question

Please post the points one should keep in mind while designing or coding for lesser footprint deliverables for embedded systems.

I am not giving compiler or platform details, as I want generic information. But, any specific information on Linux based OS is also welcome.

Was it helpful?

Solution

Depends on how low you want to get. I'm currently coding for fiscal printers, and there's no OS, and the main rule is no dynamic memory allocation. The funny thing is that I still convinced the crew to code fully modern C++ ;).

Actually there are a few rules we decided upon:

  • no dynamic allocation
  • hence, no STL
  • no exception handling (obvious reasons)

OTHER TIPS

There isn't a general answer, only ones specific to language/platform ... but

Small memory footprint ...

  1. Don't use Java, C#/mono, PHP, Perl, Python or anything with garbage collection
  2. Get as close to the metal as feasible, Use C
  3. Do alot of profiling to see where memory is getting allocated, if you are using dynamic allocation
  4. Ensure you prevent heap-fragmentation by allocating sensible chunks and sizes of the heap
  5. Avoid recursive functions especially those that use malloc(). Better allocating a chunk and passing a pointer around.
  6. use free() ;)
  7. Ensure your types are no bigger than required
  8. Turn on compiler optimizations

There will be more.

for real low footprint consider doing Assembly directly.

We all know that Hello World in C or C++ is 20kb+(because of all the default libraries which get linked). In Assembly this overhead is gone. As pointed out in the comments one can reduce the standard libraries quite a bit. However, the fact remains that the code density you can get when coding assembly is much higher than a compiler will generate from a higher language. So for code where every byte matters, use assembly.

also when programming on devices with less capable processors, programming in assembly language might be your only way to do make the program fast enough for it to be realtime enough to (for instance) control machines

When faced with such constraints, it is advisable to pre-allocate memory in order to guarantee that the system will work under load. A design pattern such as "object pooling" can be used to share resources within the system.

The C language enables tight resource (i.e. memory & compute cycles) control. It should be strongly considered.

Avoid recursion as it is easy to abuse and can result in stack overflow conditions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top