(a) Give the minimum size of the following C++ structure, assuming that char and
ID: 653569 • Letter: #
Question
(a) Give the minimum size of the following C++ structure, assuming that char and bool values occupy one byte, short values occupy two bytes, int and float values occupy four bytes, double and long values occupy eight bytes, and pointers occupy four bytes.
struct s {
float *a;
char b[2];
union {
double *c;
int d;
short e;
};
bool f;
long *g[4];
};
(b) Assuming that the structure has its minimum size, what is the offset of each member from the beginning of the structure? (The offset of a will be 0, for example.) Include the offsets of c, d, and e in your answer.
Explanation / Answer
a)
float *a --> 4 Bytes
char b[2] --> 2*2 = 4 Bytes
double *c; --> 4 Bytes
int d; --> 4 Bytes
short e; --> 2 Bytes
bool f; --> 1 Bytes
long *g[4]; --> 4*4 = 16 Bytes
Total --> 35 Bytes
b) Offsets for each variable
float *a --> 0
char b[2] --> 4
double *c; --> 8
int d; --> 12
short e; --> 16
bool f; --> 18
long *g[4]; --> 19
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.