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

xutengl
Posts: 2
Joined: Thu Jun 01, 2023 11:44 pm

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

Postby xutengl » Fri Jun 02, 2023 12:03 am

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

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Fri Jun 02, 2023 8:58 am

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)

MicroController
Posts: 1136
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

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

Postby MicroController » Fri Jun 02, 2023 11:43 am

"(which allocate RAM/the array either from BSS or from the heap...)" :)

Who is online

Users browsing this forum: HighVoltage and 126 guests