[Solved] Why is sizeof array(type string) 24 bytes with a single space element?


Why is sizeof array(type string) 24 bytes with a single space element?

Because the size of a std::string object is 24 bytes (on your system).

My understanding is that a char is 1 byte

Correct.

a string is 2 bytes (an extra byte for the \0 character).

An array of 2 char objects is indeed 2 bytes.

However, I discovered that a single element of a string array is 24 bytes.

You seem to be conflating different meanings of the word “string”. While a string is indeed an array of characters, std::string is not just an array of characters. Rather, std::string is a class that represents a string.

More specifically, std::string is a class that manages a dynamically allocated array of characters. As such, the size of a string must be at least the size of a pointer that points to the allocated memory.

1

solved Why is sizeof array(type string) 24 bytes with a single space element?