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
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 |
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 |
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 |
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 |
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 |
How a macro differs from a template?
advantages and disadvantages of using Borland C++ / version 5.
Give 2 examples of a code optimization?
Is it legal in c++ to overload operator++ so that it decrements a value in your class?
What does it mean to declare a member function as virtual?
What are the advantages of using typedef in a program?
write a c++ program that gives output 4 3 4 2 3 4 1 2 3 4 using looping statement
Do inline functions improve performance?
What is pointer in c++ with example?
Why c++ is called oop?
Why Pointers are not used in C++?
CAN U SAY WHICH PROGRAMING LANGUAGE IS USED BY DOCTORS....?