I'm reading about struct with flexible array members and I wrote a little program to test my understanding:
struct HelloWorld {
int a;
std::string b;
};
std::string arr[5] = { "abcdetttttttttttttttttttttttttt",
"abcdefgh", "abcrrrr", "abcdtttttttt",
"abcdezzzzzzzz" };
HelloWorld * h = new HelloWorld[5];
for (int i = 0; i < 5; i++) {
std::cout << (h+i) << std::endl;
h[i].a = 5;
h[i].b = arr[i];
}
for (int i = 0; i < 5; i++) {
std::cout << (h + i) << std::endl;
}
First of all, I read that the flexible array member, in this case the string b, is not counted toward the size of the struct when using sizeof. However, when I calculate sizeof HelloWorld it returns 48 bytes. Shouldn't it be just 4 bytes? Secondly, When I dynamically allocate an array of 5 HelloWorld, each object's adress is separated by 48 bytes. Even when I fill the member b with different-sized string, these addresses dont change, so I'm guessing the struct is storing the strings somewhere else and not in the allocated space by "new". Where are these strings being stored?