Deciphering Object Sizes of some classes - Why don't they appear the same as the calculated ones? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Deciphering Object Sizes of some classes - Why don't they appear the same as the calculated ones?

I must first show you some sizeof results in CppDroid: sizeof(int) -> 4 sizeof(double) -> 8 sizeof(char) -> 1 Now I tried deciphering some class object sizes using sizeof. I read somewhere that classes have the size of the members added together. So In my class: class Memory { char a; }m; cout<<sizeof(m); //Shows 1. But, class Memory2 { char a; int b; }m; cout<<sizeof(m); //Shows 8? And In derived classes, I get: class I:public Memory2 { char aaa;} i; cout<<sizeof(i); //Shows 16? Why ?

12th Jul 2017, 3:56 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
4 Answers
13th Jul 2017, 5:29 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 7
Is it due to use of a different compiler or is the compiler playing safe by reserving some extra memory for the class objects?
12th Jul 2017, 3:59 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 7
I would appreciate it more if I recieved an answer for my question as well...😐😶
13th Jul 2017, 5:04 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
@Dplusplus I was aware of only step one, and hadn't read this before. Thank You very much!
16th Jul 2017, 3:08 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar