what is the size of this class
class size
{
public:
char data1;
double d;
int data2;
char data3;
double data4;
short data5;
};
please explain the padding for these double variables.
Answer Posted / siva
Hi guys why dont you test properly before posting answers???
the answer is 40. how it is padding is as shown bellow
char data1 1+7 bytes
double d 8 bytes
int data2 4 bytes
char data3 1+7 bytes
double data4 8 bytes
short data5 4 bytes
so 40 bits
padding intermes of char type only, not for int, double and
short.
also, padding depends one next member type.
also observe char member are not in sequence.if they are in
sequence all will use same size as first one.
example:
if above structure is as shown bellow
struct size{
char data1 //1+7 bytes
char data6 //uses above 8 bytes
char data7 //uses above 8 bytes
char data8 //uses above 8 bytes
double d //8 bytes
int data2 //4 bytes
char data3 //1+7 bytes
double data4 //8 bytes
short data5 //4 bytes
}
still its size is 40 byts.
here you guys can observe before char data8 double d is
declared thatsway char data8 is padding to 1+7 = 8 byts
if it is int d then char data8 is padding to 1+3 =4 byts
| Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
What do nonglobal variables default to a) auto b) register c) static
Explain virtual destructor?
Describe the advantage of an external iterator.
What does getch() do according to the ANSI C++ standard a) Reads in a character b) Checks the keyboard buffer c) Nothing in particular (Its not defined there)
What are the advantages of using pointers in a program?
What are the differences between the function prototype and the function defi-nition?
Can constructor be private in c++?
There are 100 students in a class. The management keep information in two tables. Those two tables are given like Roll no Name Age 001 ABC 15 002 XYZ 14 and Roll No Subject Marks 001 Math 75 001 Physics 55 002 Math 68 001 Hindi 69 They want the information like this Roll No Name Hindi Physics Math Total 001 ABC 69 55 75 199 002 XYZ 68 74 84 226 And Roll No Suject Highest 001 Math 98 007 Physics 84 021 Hindi 74 All 275 All information is kept in structure in main memory. You have to find last two tables.
What are multiple inheritances (virtual inheritance)? What are its advantages and disadvantages?
Out of fgets() and gets() which function is safe to use?
What is the difference between a definition and a declaration?
Explain what are accessor methods?
What is oops in c++?
What is virtual base class?
How would you use the functions randomize() and random()?