confusing output

#include<iostream.h>

class A{
public:
char a;
};
class B:public A{
public:
int b;
};

void main(){

B ob;

cout<<sizeof ob<<endl;
}

this program shows the output 8 bytes that is confusing actually is should show 5 bytes because char a size is 1 byte and int b size is 4 byte.....please help me to understand this contradiction ....
This should answer your question : http://en.wikipedia.org/wiki/Data_structure_alignment#Typical_alignment_of_C_structs_on_x86

Edit : C structs and C++ classes works similarly in this case.
Last edited on
Topic archived. No new replies allowed.