Почему заполнение большого массива символов приводит к сбою? (ESP8266)

Вот код, о котором идет речь:

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() {
}

Сбой, который я получаю на своем NodeMCU ESP8266, сброс wdt. Обратите внимание, что это произошло на итерации 835. Это одна и та же итерация каждый раз.

...
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

Когда я уменьшаю длину массива до 4000, я получаю другой сбой, на этот раз это Обнаружено разрушение стека. Однако обратите внимание, что на этот раз все было успешно напечатано, и сбой происходит только после того, как все сделано.

3997
3998
Going to print string: 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

--------------- 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
...

Когда я уменьшаю длину массива еще больше, до 3000, ничего не происходит, и программа работает нормально, как и должна.

Что здесь происходит?

, 👍1


1 ответ


Лучший ответ:

4

Поскольку я не знаком с архитектурой ESP8266, я не могу дать окончательное объяснение проблемы. Однако довольно часто найти платформы, где стек не может вырасти за пределы фиксированного размера, что является путем меньше доступной памяти. Это происходит, например, на многопоточные среды, где каждый поток имеет свой собственный стек, выделяется в фиксированном размере. Старый трюк с размещением стека и куча на противоположных концах физической памяти не может работать на таком среды.

Для справки, статические переменные выделяются в данных программы раздел, а не куча, которая используется malloc и new.

,