Output panel shows same memory despite changing 'word' to 'byte'

Hi all,

I have been using Swordfish Basic for quite a few years now however I have been experimenting with Arduino for past couple of years. In swordfish basic when I change the declaration of a variable from a 'byte' to a 'word' or vice versa and then compile the code, the output panel will show that an extra byte of dynamic memory has been used or has been freed.

When using the Arduino IDE however, the amount of dynamic memory used does not change. Here is a simple blink sketch to use as an example:

byte thisIsMyLED = 13; // let's give the number 13 a nickname
word ledDelay = 255;  // let's give the led delay amount the nickname of 'ledDelay'

void setup() {
    pinMode(thisIsMyLED, OUTPUT);  // set thisIsMyLED to an output
}

void loop() {
  digitalWrite(thisIsMyLED, 0);         // turn LED off
  delay(ledDelay);  // delay for however many milliseconds that 'ledDelay' represents
  digitalWrite(thisIsMyLED, 1);         // turn LED on
  delay(ledDelay);  // delay for however many milliseconds that 'ledDelay' represents
}

When I compile the code above, the console output shows:

Sketch uses 960 bytes (2%) of program storage space. Maximum is 32256 bytes.

Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

If I leave ledDelay as a 'word' but change its value to 1000 and recompile I get the following:

Sketch uses 960 bytes (2%) of program storage space. Maximum is 32256 bytes.

Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

If I change ledDelay back to 255 and declare it as a 'byte' instead of a 'word' I get the following when I compile:

Sketch uses 960 bytes (2%) of program storage space. Maximum is 32256 bytes.

Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

In each of these three instances, the amount of dynamic memory used is exactly the same, can someone please let me know why this is?

thank you in advance.

Probably, the compiler sees that ledDelay fits in a byte, and is never modified. So you can declare it with a longer data type but it will optimize away the difference.

aarg:
Probably, the compiler sees that ledDelay fits in a byte, and is never modified. So you can declare it with a longer data type but it will optimize away the difference.

That is what I originally thought however when I change the value from 255 (which fits in a byte) to 1000 (which requires more than a byte) the dynamic memory used is still the same.

The compiler notices that it is a constant value and eliminates it.

If you force it to be a variable you get

byte thisIsMyLED = 13; // let's give the number 13 a nickname
byte ledDelay;  // let's give the led delay amount the nickname of 'ledDelay'

void setup() {
  pinMode(thisIsMyLED, OUTPUT);  // set thisIsMyLED to an output
  ledDelay = 255;
}

void loop() {
  digitalWrite(thisIsMyLED, 0);         // turn LED off
  delay(ledDelay);  // delay for however many milliseconds that 'ledDelay' represents
  digitalWrite(thisIsMyLED, 1);         // turn LED on
  delay(ledDelay);  // delay for however many milliseconds that 'ledDelay' represents
}
10 Bytes (0%) for uint8_t
11 Bytes (0%) for uint16_t
13 Bytes (0%) for uint32_t

Don't use "word". It's a Microsoft abomination.

Whandall:
The compiler notices that it is a constant value and eliminates it.

That makes perfect sense now!

I guess that leads me to a related question then - if the compiler automatically recognises a "variable" as a constant, then there seems to be no requirement for the 'const' qualifier.

Is that statement correct?

In my opinion it is not correct.

There is more about constness than RAM usage.

It implements a contract between the programmer and the program, "say what you mean, mean what you say". Then it's easier to detect whether there is a breach of intent (i.e. bug).

Thank you Whandall and aarg - you have been most helpful :slight_smile: