Question & Answer: Given this struct on a 32-bit architecture, what is sizeof(struct thestruct)?…..

Given this struct on a 32-bit architecture, what is sizeof(struct thestruct)?

struct thestruct {
int A;
int B:1;
char* C;
double d;
};

————————————————-

What is the size of each element in (struct thestruct)?

struct thestruct {
int A;
int B:1;
char* C;
double d;
};
——————————————-

What ordering of items in (struct thestruct) would result in the smallest struct size?

struct thestruct {
int A;
int B:1;
char* C;
double d;
};
—————————————————-

What would be the sizeof(struct thestruct) if you optimized it for smallest size?

struct thestruct {
int A;
int B:1;
char* C;
double d;
};

Expert Answer

 

[1] Total Size = 24

[2]
Size = size of A + size of B+ size of C + size of D
= 4 + 4 +8 +8
= 24

[3] The order of fields in a struct does matter – the compiler is not allowed to reorder fields, so the size of the struct may change as the result of adding some padding.In this case, however, you are defining a so-called flexible member, an array the size of which you can change.
So no Arrya difine in Struct defination.

Same Size = 24

Still stressed from student homework?
Get quality assistance from academic writers!