tonydg
April 29, 2023, 1:26am
1
I don't understand.
Some say that the bool or boolean size is one bit.
The Arduino reference says: Each bool variable occupies one byte of memory.
BUT
I ran a program to read the addresses of two booleans.
and from the addresses, I see that each one uses two bytes.
Can you help me understand which is correct?
Thank you
Antonio
kolaha
April 29, 2023, 2:04am
2
it is no place in flash smaller as 8bit, so yes, boolean take 1 full byte in arduino platform. of course you able to pack 8 your bool variable in 1 byte and spare 7 bytes of flash
tonydg
April 29, 2023, 3:04am
3
Thank you Kolaha.
I still don't understand why the address of one bool and another bull.
jumps by two numbers in the memory. So each one is occupying 2 bytes in the memory.
See next attachments
sketch_apr28a.ino (616 Bytes)
2 bytes in the memory
What Arduino board are you using?
What is the output, for your board, from this code?
bool someBool = false;
void setup()
{
Serial.begin(115200);
Serial.print("the size of a bool on this board is ");
Serial.print(sizeof(someBool));
Serial.println(" bytes");
}
void loop()
{
}
My Uno yields:
the size of a bool on this board is 1 bytes
Looks like you are printing the address of a pointer to a bool, not the address of the bool itself. On an UNO a pointer is 16 bits.
bool SmplBl_1 = true;
bool *pSmplBl_1 = &SmplBl_1;
bool SmplBl_2 = true;
bool *pSmplBl_2 = &SmplBl_2;
boolean Bln_1 = false;
boolean *pBln_1 = &Bln_1;
boolean Bln_2 = false;
boolean *pBln_2 = &Bln_2;
void setup() {
Serial.begin(115200);
}
void loop() {
int AddressOfpSmplBl_1 = & pSmplBl_1;
Serial.println(AddressOfpSmplBl_1);
int AddressOfpSmplBl_2 = & pSmplBl_2;
Serial.println(AddressOfpSmplBl_2);
int AddressOfpBln_1 = & pBln_1;
Serial.println(AddressOfpBln_1);
int AddressOfpBln_2 = &pBln_2;
Serial.println(AddressOfpBln_2);
while (!Serial.available()) {
}
Serial.read();
}
tonydg
April 29, 2023, 4:26am
6
Thank you, David;
You are right But if the next bool is 2 bytes apart. how can I use the adjacent byte?
Hello tonydg
Consider this declaration and initialization of an "array" of bits.
enum MyBits {Bit_0,Bit_1,Bit_2,Bit_3,Bit_4,Bit_5,Bit_6,Bit_7};
uint8_t myBool {0b00000000}; // start setup of bits set
void setup()
{
}
void loop()
{
}
Simply process these bits by using
Have a nice day and enjoy coding in C++.
It is not two bytes apart. You were printing the address of a pointer, which occupies two bytes, not the address of a bool, which occupies one. Try the following code, note that I put the bool variables in a struct, otherwise the compiler can scatter them throughout memory at its convenience.
struct {
bool SmplBl_1 = true;
bool SmplBl_2 = true;
boolean Bln_1 = false;
boolean Bln_2 = false;
} bools;
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println((unsigned int)&bools.SmplBl_1);
Serial.println((unsigned int)&bools.SmplBl_2);
Serial.println((unsigned int)&bools.Bln_1);
Serial.println((unsigned int)&bools.Bln_2);
while (!Serial.available()) {
}
Serial.read();
}
kolaha
April 29, 2023, 10:11am
9
in serial monitor on picture i see
int x = ....
print.sizeof(x)
INT is 2 bytes in adruino system. ESP - 4 bytes.
kolaha
April 29, 2023, 10:14am
10
david_2018:
boolean
boolean and bool is the same.
I am aware of that, I posted the OP's original code that was attached as an .ino file in post #3 .
system
Closed
October 26, 2023, 4:36pm
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.