sizeof()

I have found a possible problem with sizeof(). Am I missing something?
My code:

#define PGM_ARD_SERIAL_BAUD 57600 //set Arduino usb serial port speed

String BOX_msg_1 = "this";
String BOX_msg_2 = "where 2nd non_blank msg would be";
String BOX_msg_3 = "123456789X12345";

void setup() {
Serial.begin(PGM_ARD_SERIAL_BAUD); //set Arduino usb serial port speed

Serial.print(F("size of msg 1: "));
Serial.println(sizeof(BOX_msg_1));
Serial.print(F("size of msg 2: "));
Serial.println(sizeof(BOX_msg_2));
Serial.print(F("size of msg 3: "));
Serial.println(sizeof(BOX_msg_3));
}

void loop() {
// put your main code here, to run repeatedly:

}

//output from above:
size of msg 1: 6
size of msg 2: 6
size of msg 3: 6

The results were the same for daily 1.8.6 for Aug 8 2018, and for stable 1.8.5

What are you expecting the value to be?

You are using a C++ 'String' object. The String object contains (at a guess) a pointer to a block of bytes (2 bytes), and an offset to the start and end (2 bytes each). This makes it fast to work with substrings of a string.

When you use sizeof, you get the size of the object, not the length of the actual string.

Generally, we avoid using C++ String objects when working on the arduino. Behind the scenes, it uses dynamically-allocated memory to do its work, and that's limited and prone to fragmentation. Instead, we usually use C-strings, which are arrays of byte terminated with a zero '\0'. The C programming language uses these by default, and there are a number of standard library functions which are designed to accept these - see the sticky post

Try this for more info:

char BOX_msg_1[] = "test";
char BOX_msg_2[4] = "test"; // warning! This won't be properly terminated!
char *BOX_msg_3 = "test";

msg_1 is an array of char. The C compiler allocates 5 bytes for it - 't' 'e' 's' 't' '\0'.

msg_2 is an array of char. We explicitly say it's 4 bytes, so a \0 terminator isn't put on it. If you use the library string functions (eg: strlen(), Serial.println()), they won't do as they are supposed to.

msg_3 is a pointer to char - 2 bytes. The compiler creates a 5-byte array of char, populates it with the text and its terminator, and puts the memory address of that array into the variable.

old_dog:
//output from above:
size of msg 1: 6
size of msg 2: 6
size of msg 3: 6

Are you expecting the following outputs? If so, please consult this link.

GolamMostafa:
Are you expecting the following outputs? If so, please consult this link.

which can be misleading if you are actually willing to count the number of symbols of course...

String msg1 = "今日は";
String msg2 = "hé";

void setup() {
  Serial.begin(115200);

  Serial.print("Length of the message1 = ");
  Serial.println(msg1.length());

  Serial.print("Length of the message2 = ");
  Serial.println(msg2.length());
}

void loop() {}

will show in the console

[sub][color=purple]Length of the message1 = [color=red][b]9[/b][/color]
Length of the message2 = [color=red][b]3[/b][/color]
[/color][/sub]

whereas I only see 3 and 2 symbols in my strings...

:grin: :smiling_imp: