Page 1 of 1

why can't I use an array of size larger than 3584 bytes in main() ?

Posted: Fri Jun 02, 2023 12:03 am
by xutengl
In the following code, I declared an array of size 3585 bytes, it compiles without error but during execution, it constantly reboots my esp32-Wrover-E chip. If I reduce the array size to 3584 bytes, then there is no such issue. Why?

Also, if I declare the array of size larger than 3585 bytes outside the main() or use malloc, there is no issue.

PS: I am using esp-idf 5.0.1 stable
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include <stdio.h>
  4.  
  5. void app_main(void)
  6. {
  7.     uint8_t int_array[3585] = {0};
  8.  
  9.     while (1)
  10.     {
  11.         int_array[0]++;
  12.         array[0]++;
  13.         printf("static array size: %d, count: %d\n", (unsigned int)sizeof(int_array), (unsigned int)int_array[0]);
  14.         vTaskDelay(pdMS_TO_TICKS(1000));
  15.     }
  16. }

Re: why can't I use an array of size larger than 3584 bytes in main() ?

Posted: Fri Jun 02, 2023 8:58 am
by ESP_Sprite
Your array is allocated on the stack, and the main task (like all tasks) only has a limited amount of stack available. Either use Menuconfig to increase that, or use any of the other two methods (which allocate stackRAM either from BSS or from the heap, not from stack) (thanks MicroController)

Re: why can't I use an array of size larger than 3584 bytes in main() ?

Posted: Fri Jun 02, 2023 11:43 am
by MicroController
"(which allocate RAM/the array either from BSS or from the heap...)" :)