Dynamic memory calculation issue

I run below code for Arduino Uno and it compiled successfully. But I am expecting an error since I have used uint32_t data type for the variables. It should exceed the dynamic memory, right? Because I have used 1000 uint32_t. But it just consumes 13 bytes. Why?

uint32_t x;
uint32_t data[1000];

void setup() {
}

void loop() {
data[x++]=x; 
}

Arduino console log Sketch uses 482 bytes (1%) of program storage space. Maximum is 32256 bytes. Global variables use 13 bytes (0%) of dynamic memory, leaving 2035 bytes for local variables. Maximum is 2048 bytes.

Welcome,

The array is not used anywhere, so it's deleted in the compiled program

Because you don't use the array, so the compiler optimised it away.

How is this a tutorial?

If the compiler not optimize remove it, it will cause the error you expect.

volatile uint32_t data[1000];
Sketch uses 514 bytes (1%) of program storage space. Maximum is 32256 bytes.
data section exceeds available space in board
Global variables use 4013 bytes (195%) of dynamic memory, leaving -1965 bytes for local variables. Maximum is 2048 bytes.
Not enough memory; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing your footprint.
Error compiling for board Arduino Uno.

Your post was MOVED to its current location as it is more suitable.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.