Okay really confused about something here. Two questions.
First question is according to my compiler, string is always a size of 40 bytes. How is this possible when we can have more than 40 characters in a string and each character should be taking up 1 byte?
Second question: If I have a struct containing a string (40bytes) and an integer (4bytes), why is my resulting structure size 48 instead of 44? I can't figure out what is going on here.
Thanks if anyone knows/understands why i'm getting this behavior.
struct Employee //This struct is size 48 for some reason?
{
string name; //String takes up 40 bytes
int ID; //int takes up 4 bytes
};
struct Size8Struct //This struct is size 8 as expected
{
int ID;
int filler;
};
int main() {
cout << sizeof(Size8Struct) << endl; //returns 8 as expected
Employee Jim; Jim.ID=1; Jim.name="Jim";
cout << sizeof(Jim) << endl; //returns 48, why?
string test = "123456789012345678901234567890123456789012345678901234567890 test"; //How is it possible for this string to hold over 40 chars if it is only 40 bytes long?
cout << sizeof(test) << endl;
}