c++ - Struct size stays the same even after adding a new member to it -
c++ - Struct size stays the same even after adding a new member to it -
when execute
cout << sizeof(string); i got 8 answer.
now having structure
typedef struct { int a; string str; } mytype; and executing
cout << sizeof(mytype); i got 16 answer.
now made alter in structure
typedef struct { int a, b; string str; } mytype; and executing
cout << sizeof(mytype); i got 16 answer!!!. how? happening?
perhaps padding happening. e.g. sizeof(int) can 4 bytes , compiler can add together 4 bytes after a sake of info alignment. layout this:
typedef struct { int a; // 4 bytes // 4 bytes padding string str; // 8 bytes } mytype; typedef struct { int a; // 4 bytes int b; // 4 bytes string str; // 8 bytes } mytype; c++ string struct
Comments
Post a Comment