Bloomberg LP Interview Question
Software Engineer / DevelopersIs the size of the class and object always same ?Why is it so ?
If the class has static members, then these members are not stored in the object. Inspite of that will the class and object have the same size ?
'Size of class' doesn't make sense because it's an abstract notion. If you want to calculate all the memory that relates to it you should also take the size of the code (hint: some member functions are generated by the compiler automatically... and then optimized). The total size of that memory is really implementation dependent (i.e. on the compiler).
Size of the object of that type (class A) depends on:
1. the platform (at least sizeof(void*))
2. the compiler (consider alignment settings). Though sizeof(A) is unlikely to be different from sizeof(void*).
For the sake of experiment, try sizeof(B) on your machine:
class B{
int i;
double d;
}
4 bytes for the pointer, 0 for static, static var is stored in static memory.
- NewStart April 21, 2010so 4 bytes total