Pages: [1]   Go Down
Author Topic: Bool vs Boolean - again  (Read 1468 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

after googling for a while I found out that bool and boolean can be used interchangeably in the Arduino IDE. Is that correct?
I personally prefer bool, because it is standard C language style. What do you think?

And if it is correct, why do I get a 14 bytes larger program when compiling the following code:

http://pastebin.com/t7LxjMJ7

and declaring the variables at line 20 and 21 as bool instead of boolean?

(boolean: 8378 bytes, bool: 8392 bytes).

I know, it is not that much memory, but every little byte counts when you only have 32k of them...

Another strange thing: whether or not I declare the function at line 35 as bool or boolean I always get the same compiled size.
What is wrong with the compiler?

« Last Edit: December 27, 2012, 02:23:02 pm by fran83 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In Arduino.h, boolean is a typedef for uint8_t. I suspect that the bool type is optimized for holding true or false, while the boolean pseudo-type is not.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

'bool' is the same size as 'int' which is 2 bytes on most Arduino boards.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
because it is standard C language style.

I use unsigned char, which at comparison time, is really int.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In Arduino.h, boolean is a typedef for uint8_t. I suspect that the bool type is optimized for holding true or false, while the boolean pseudo-type is not.

Doesn't it have to be the other way round? Because in my sketch, boolean gets a smaller output than bool.

'bool' is the same size as 'int' which is 2 bytes on most Arduino boards.

But why doesn't changing the function return variable from boolean to bool the sketch compiled size then?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The size of the sketch isn't directly a result of the size of the variables. That is, increasing a variable you use from a byte to an int isn't going to increase the sketch size by a single byte. It will affect the SRAM, but the program memory is dependent on the AVR instruction set. So if changing an aspect of the code doesn't affect the sketch size, that means that either it didn't affect the assembled code, or the same overall size instructions are used.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12480
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
  Serial.println(sizeof(bool));
  Serial.println(sizeof(boolean));

returns
1
1

so size seems to be the same on first sight.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My mistake apparently it may be compiler, and, or architecture dependent.

My results match 1 byte on the current compiler as well.

Same under Windows (VS), MacOS (clang) and several version of Linux (gcc).

I wonder when that changed ...
« Last Edit: December 27, 2012, 06:11:15 pm by lloyddean » Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My mistake apparently it may be compiler, and, or architecture dependent.

My results match 1 byte on the current compiler as well.

Same under Windows (VS), MacOS (clang) and several version of Linux (gcc).

I wonder when that changed ...


I'm guessing bool is the "fastest" type, which is not necessarily the smallest type. On an 8 bit computer, 8 bits is the fastest. On bigger computers, the fastest size can be different.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

after googling for a while I found out that bool and boolean can be used interchangeably in the Arduino IDE. Is that correct?

Pretty much.

Quote
I personally prefer bool, because it is standard C language style. What do you think?

I prefer bool.

Quote
And if it is correct, why do I get a 14 bytes larger program when compiling the following code:

Code:
                Serial.println(heatingStatus);

heatingStatus is extended to 16 bits (one machine instruction) when heatingStatus is bool (different println is called).

Code:
                Serial.println(mixerStatus);

Ditto.  Repeat for the remaining prints.

Quote
Another strange thing: whether or not I declare the function at line 35 as bool or boolean I always get the same compiled size.
What is wrong with the compiler?

First, even though it does work, returning 1/0 is not correct.  A bool is either true or false.

You get the same size code because bool and boolean are both one byte and 1/0 is essentially interchangeable with true/false.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24424
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That is, increasing a variable you use from a byte to an int isn't going to increase the sketch size by a single byte.
Changing a "byte" (unsigned eight bit) to an "int" (signed sixteen bit) may make quite a large difference to the size of your sketch, but changing it for a more similar type, such as "unsigned int" may make no difference at all.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: