Close

ESP32 Stack Size Caution... don't use 'large' local arrays

A project log for "Mercator Origins": Sat Nav & Telemetry for Divers

Want to map your dive? Want to navigate like a pro? Even if you are vision-impaired, this will empower you to navigate our underwater world.

mark-b-jonesMark B Jones 05/08/2023 at 13:493 Comments

I've just spent nearly two hours tracking down  a nasty crash caused by increasing a buffer declared within a function (ie on the stack) from 1024 bytes to 4096 bytes. Of course I did lots of code after doing this and before doing a compile so I had no clue that this was the root cause.

Lesson is... embedded programming is substantially different than Windows/Linux development so small changes, like in this instance only 3KB, can cause a problem. I have ended up using far more globals than I would normally in Windows/Linux dev. Also the Arduino IDE is designed for absolute beginners, which is great as it gives the lowest level of friction for progressing quickly with your embedded projects, however it does pretty much force you to use one code file (not even a .c file, it has to be suffixed .ino) and it is a bit strange in its behaviour when compiling. eg you don't need function prototypes forward declared of their first usage (where the function code is below in the text file than the call). This also results in rather misleading error messages when compiling too. However, I can't complain - it's been great to not have the bloat of visual studio with all the admin stuff that is associated with.

However, when I get back from my dive trip to Gozo it will be high time to bid adieu to the Arduino IDE and setup either PlatformIO or Visual Studio Code.

Here's some really detailed info on how to reduce memory usage, which also mentions minimise the use of the stack. It's for the Espressif dev environment but it's fair to assume the guidance crosses over to the Arduino IDE / build environment:

Espressif API Guide - Minimise RAM Usage

Discussions

Ken Yap wrote 05/08/2023 at 22:42 point

Back in the days of 16-bit Unix, one was aware of such limitations on local stack storage. Then came along 32-bit systems and virtual memory which were more forgiving of programmer spendthrift. Small systems are reawakening the need to be vigilant in coding.

  Are you sure? yes | no

Mark B Jones wrote 05/09/2023 at 07:41 point

Indeed, I agree Ken. Getting back into embedded coding (I did some at Uni and school, and then at Saturn Technologies) is reteaching me some of the disciplines that I should really apply to all development - whether embedded or not.

A side-project is going to be to have my Spectrum Next and Atari STe be coded/hardware'd up to be able to show my real-time stats and track from the internet. That would be so awesome for me - these are the two 8/16 bit computers that started and continued my interest in all things computing. And as a stretch to that side-project I would love to make a bit of hardware to plug into the Spectrum 48k to be able to watch the track too.

As all hackers (in the original sense of the word) know, it's because we can rather than we must. :-)

  Are you sure? yes | no

Ken Yap wrote 05/09/2023 at 10:30 point

Looking forward to seeing those side projects! 👍

  Are you sure? yes | no