whats the size of class EXP on 32 bit processor?
class EXP
{
char c1;
char c2;
int i1;
int i2;
char *ptr;
static int mem;
};

Answers were Sorted based on User's Feedback



whats the size of class EXP on 32 bit processor? class EXP { char c1; char c2; int ..

Answer / jaroosh

Both answers are wrong.
First of all, static - class data do NOT contribute to the
class'/objects overall size.
Second, its totally wrong to assume that c1 and c2 will be
given both padding of 3 bytes (so they end up taking space
of 4). Why ?
Because (though Im not sure about every compiler, but 99% of
them will do something like the following) it is simply a
waste of space.
Here are the sizes of member variables of EXP :
class EXP
{
char c1; //1 byte
char c2; //1 byte + 2 bytes of padding! = 3 bytes
int i1; //4 bytes
int i2; //4 bytes
char *ptr; //4 bytes (compiler specific)
static int mem; // 0 bytes
};
this is why on most compilers
sizeof(EXP) is 16.

Is This Answer Correct ?    7 Yes 0 No

whats the size of class EXP on 32 bit processor? class EXP { char c1; char c2; int ..

Answer / ricardo

The answer is 16 (on most compilers), but not for the
reasons stated above.

If the class contained only c1 and c2, the size would be
2. Since i1 is an integer, though, it needs to be aligned
on a 4-byte multiple. The pointer and the other integer
also uses up 4 bytes. So, the total size is 16.

If there were another character field "c3" adjacent to c2,
the size would still be 16 bytes.

Is This Answer Correct ?    4 Yes 0 No

whats the size of class EXP on 32 bit processor? class EXP { char c1; char c2; int ..

Answer / shrinidhi

20 bytes.
static is given memory in heap.
for 1st two data members c1 and c2 compiler will take 4
bytes cz of padding.

Is This Answer Correct ?    2 Yes 1 No

whats the size of class EXP on 32 bit processor? class EXP { char c1; char c2; int ..

Answer / sxx010100

20 bytes is right, but static is in data segment portion od
memory, not the heap.

Is This Answer Correct ?    3 Yes 2 No

whats the size of class EXP on 32 bit processor? class EXP { char c1; char c2; int ..

Answer / sandeep mannarakkal

Static is independent of object but associated with class, i.e size of the object is independent of the static.
so here answer is 16 byes.(with the assumption of structure padding is available.)

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

What is c++ used for in games?

0 Answers  


write a program that a 5 digit number and calculates 2 power that number and prints it.

2 Answers   Vimukti Technologies,


How do I tokenize a string in c++?

0 Answers  


What is a concrete class?

1 Answers  


What is problem with Runtime type identification?

2 Answers  






How do you initialize a string in c++?

0 Answers  


What is the Difference between "printf" and "sprintf"?

7 Answers   iSoft, PentaWare, TCS,


Is there something that we can do in C and not in C++?

14 Answers   Patni,


Explain the volatile and mutable keywords.

0 Answers  


Why c++ is so important?

0 Answers  


What is format for defining a structure?

0 Answers  


Differentiate between structure and class in c++.

0 Answers  


Categories