Log In
1 vote

Suppose you are compiling on a machine with $1$-byte chars, $2$-byte shorts, $4$-byte ints, and $8$-byte doubles, and with alignment rules that require the address of every primitive data element to be an integer multiple of the element’s size. Suppose further that the compiler is not permitted to reorder fields; padding is used to ensure alignment. How much space will be consumed by the following array?

struct {
short s;
char c;
short t;
char d;
double r;
int i;
} A[10]; /*10 element array of structs */
  1. $150$ bytes
  2. $320$ bytes
  3. $240$ bytes
  4. $200$ bytes
in Programming and DS
recategorized by

1 Answer

0 votes

“The address of every primitive data element to be an integer multiple of the element’s size”, doesn’t make any sense as we can use any integer such as 1 as  a  multiple so we can store  it usual way.

So the total bytes needed for an element = 2+1+2+1+8+4 = 18 bytes


So  for array of 10 elements it should be 10* 18 = 180 bytes  (don’t match with any option)

Same question (but here the alignment rule is,

The address is an even multiple of the element's size


And according to this the answer is 240 bytes option  (c)

Related questions

1 vote
1 answer
Consider the following pseudo-code fragment, where $a$ and $b$ are integer variables that have been initialized: /* Pre-conditions : $(a > 1 \wedge a < b)$ */ /* Assume that overflow never occurs */ int $x=0$; int $p=1$; while $(p<b) \{$ $p=p*a$; $x=x+1$; $\}$ ... $\lfloor \: \: \rfloor$ means floor */ $\lfloor \log_a^b \rfloor$ /* $\lceil \: \: \rceil$ means ceil */
asked Nov 20, 2020 in Programming and DS jothee 171 views
1 vote
3 answers
What is the output of the following program: (Assume that the appropriate preprocessor directives are included and there is not syntax error) main( ) { char S[]="ABCDEFGH"; printf("%C", *(&S[3])); printf("%s", S+4); printf("%u", S); /*Base address of S is 1000 */ } $\text{ABCDEFGH}1000$ $\text{CDEFGH}1000$ $\text{DDEFGHH}1000$ $\text{DEFGH}1000$
asked Jul 30, 2016 in Programming jothee 1.3k views
1 vote
3 answers
The number of positive integers not exceeding $100$ that are either odd or the square of an integer is _______ $63$ $59$ $55$ $50$
asked Nov 20, 2020 in Set Theory & Algebra jothee 228 views
2 votes
2 answers
How many ways are there to pack six copies of the same book into four identical boxes, where a box can contain as many as six books? $4$ $6$ $7$ $9$
asked Nov 20, 2020 in Combinatory jothee 137 views