c++ - Why structure size is incorrect -


this question has answer here:

i tried check how memory allocated structure objects, taking more space expected. using 64 bit windows os , microsoft visual studio 2010(i think 32 bit), can explain why printing 52 bytes ?

struct test {   int year;// should take 4 byte   string title;// how bytes take ? in case taking 31 bytes ?   double date;//should take 8 byte   int month;// should take 4 byte   } mine;  int main () {  cout << " size is: "<<sizeof(mine);//printing 52 ?  cout << " size is: "<<sizeof(struct test);//printing 52 ?   return 0; } 

note that

sizeof(struct) >= sizeof(its members) 

because each member might aligned lowest address after previous member satisfies:

mod(address/sizeof(member)) == 0 

for example, consider struct:

struct s {    char c;    int i[2];    double d; } 

the memory might that:

+-------------------------------------------------+ | c |  |  |  | i[0] | i[1] |  |  |  |  | .. v ..  | +-------------------------------------------------+      ^  ^  ^                ^  ^  ^  ^ 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -