1

Here's the code in question:

void setup() {  
  Serial.begin(115200);
  Serial.println();

  char test[5000];
  int i;

  for (i = 0; i < sizeof(test)-1; i++) {
    test[i] = 'x';
    Serial.println(i);
  }

  Serial.println("Going to print string: ");
  test[i]='\0';
  Serial.println(test);
}

void loop() {
}

The crash I get on my NodeMCU ESP8266, wdt reset. Note that it happened on iteration 835. It's the same iteration every time.

...
834
835

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 3424, room 16 
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8 
tail 0
chksum 0x2b
csum 0x2b
v00041d50
~ld

When I decrease the length of the array to 4000, I get a different crash, this time it's Stack smashing detected. Note however, that this time everything was printed successfully and it only crashes after doing everything.

3997
3998
Going to print string: 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Stack smashing detected.

Exception (5):
epc1=0x40100d43 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: sys
sp: 3fffffa0 end: 3fffffb0 offset: 0000
3fffffa0:  3fffdad0 00000000 3ffee56c 40100d43  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3424, room 16 
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8 
tail 0
chksum 0x2b
csum 0x2b
v00041d50
~ld
...

When I decrease the length of the array further, to 3000, nothing happens and the program runs normally as it should.

What is going on here?

6
  • ahhh you're right, fixed, that's due to me renaming variables to make it cleaner for SO, it wasn't there when I was running it. The question still stands.
    – Daavee18
    May 14 at 17:53
  • Does the behavior change if you qualify the array as static? May 14 at 18:31
  • @EdgarBonet Yes, it actually fixes the problem! Thanks, could you explain or point me to some resource that would explain the reason for this difference? Shouldn't allocated memory be the same whether it's on the stack or on the heap?
    – Daavee18
    May 14 at 18:52
  • @Daavee18 - Likely your build has a memory layout with the (fixed-size) stack proceeded by something else critical. 4096 bytes is way larger than the stack size, so you end up with test pointing to some random memory that's bad to overwrite, but not critical at that point in time. But then test+835 ends up overwriting something required for printing, e.g. the first instruction of println or somesuch.
    – TLW
    May 15 at 1:42
  • If you're asking why the compiler doesn't warn you at compile time that you might overflow stack - there are compilers that will do that, but they tend to result in massive numbers of false positives from "potential" callstacks that can't actually occur in practice.
    – TLW
    May 15 at 1:43

1 Answer 1

4

As I am not familiar with the ESP8266 architecture, I cannot give a definitive explanation of the issue. It is, however, fairly common to find platforms where the stack cannot grow past a fixed size that is way smaller than the available memory. This happens, for example, on multi-threaded environments, where each thread has its own stack, allocated at a fixed size. The old trick of putting the stack and the heap at opposite ends of the physical memory cannot work on such environments.

For the record, static variables are allocated in the program's data section, not on the heap, which is used by malloc and new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.