All Exams Test series for 1 year @ ₹349 only
Question

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*/

The correct answer is

240 bytes

Calculating Memory Consumption of a C Struct Array with Padding and Alignment

The question asks us to determine the total memory space consumed by an array of 10 structs, considering specific data type sizes and strict alignment rules that prevent field reordering but require padding.

Understanding the Struct Definition and Data Types

The given struct is defined as:

struct {short s;char c;short t;char d;double r;int i;} A[10];

We are given the following sizes and alignment requirements for the primitive data types on the target machine:

  • char: 1 byte size, 1-byte alignment
  • short: 2 bytes size, 2-byte alignment
  • int: 4 bytes size, 4-byte alignment
  • double: 8 bytes size, 8-byte alignment

The rule states that the address of every primitive data element must be an integer multiple of its size (which is also its alignment requirement in this context). Padding is inserted if needed to satisfy alignment, and the compiler cannot reorder fields.

Calculating the Size of a Single Struct Element

We need to calculate the size of one instance of the struct, taking into account the members, their sizes, and the required padding. We process the members in the order they are declared:

Member Size Alignment Current Address (Bytes) Padding Needed Address After Padding Bytes Occupied New Current Address (Bytes)
s (short) 2 2 0 (assuming struct starts at 0) 0 (0 is multiple of 2) 0 0 - 1 2
c (char) 1 1 2 0 (2 is multiple of 1) 2 2 3
t (short) 2 2 3 1 (3 is not multiple of 2; need address 4) 4 4 - 5 6
d (char) 1 1 6 0 (6 is multiple of 1) 6 6 7
r (double) 8 8 7 1 (7 is not multiple of 8; need address 8) 8 8 - 15 16
i (int) 4 4 16 0 (16 is multiple of 4) 16 16 - 19 20

After placing all members, the struct occupies up to address 19, meaning it currently uses 20 bytes (from 0 to 19).

Applying End-of-Struct Padding

The total size of a struct must be a multiple of its largest member's alignment requirement. In this struct, the largest alignment is 8 bytes (for the double). The current size after placing all members is 20 bytes. We need to add padding at the end so that the total size is a multiple of 8.

The smallest multiple of 8 that is greater than or equal to 20 is 24. Therefore, we need to pad the struct to a total size of 24 bytes.

Padding needed at the end = Total size - Size after placing members = $24 - 20 = 4$ bytes.

So, the total size of one struct element is 24 bytes.

Calculating the Total Size of the Array

The array A contains 10 elements. Each element is a struct of size 24 bytes. The total space consumed by the array is the size of one element multiplied by the number of elements.

Total size = Size of one struct $\times$ Number of elements

Total size = $24 \text{ bytes/struct} \times 10 \text{ structs} = 240 \text{ bytes}$

Therefore, the array will consume 240 bytes of memory.

Revision Table: Struct Memory Layout

Concept Description
Data Type Size The number of bytes a specific data type occupies in memory (e.g., char = 1, int = 4).
Alignment Requirement The constraint that a data element must be placed at a memory address that is a multiple of a specific number (usually related to its size or the machine's architecture).
Padding Empty bytes inserted by the compiler between struct members or at the end of a struct to ensure that subsequent members or the next array element meet their alignment requirements.
Struct Size The total memory occupied by one instance of a struct, including padding. It is often rounded up to a multiple of the largest alignment requirement within the struct.

Additional Information: Compiler Behavior and Optimization

  • Field Reordering: Some compilers can reorder struct fields to minimize padding and reduce the overall struct size. However, the problem explicitly states this is not permitted here.
  • Packed Structs: Compilers often provide options (like pragmas or attributes) to create "packed" structs where padding is minimized or eliminated, potentially saving space but sometimes negatively impacting performance due to unaligned memory access.
  • Largest Alignment: The rule that the total struct size must be a multiple of the largest member's alignment ensures that when an array of structs is created, each element of the array starts at an address that is correctly aligned for all its members, especially the one with the strictest alignment.
  • Impact of Order: If field reordering were allowed, a compiler could potentially arrange the fields (e.g., group smaller fields together) to reduce the total padding needed within the struct, leading to a smaller overall size. For example, placing `char` and `short` fields together might reduce padding compared to interspersing them with `double` or `int`.
Was this answer helpful?

Important Questions from Programming in C

  1. In object-oriented programming, which keyword ensures that a member variable belongs to the class itself rather than to any specific object, thereby allowing all instances to share a single copy of that variable?

  2. The condition num! = 65 cannot be replaced by

  3. Which statement is false?

  4. The three basic structure types used to write the algorithm for a program are sequence, selection and repetition. Which of the following is NOT an advantage of using only these structures when writing the algorithm for a program?

  5. Consider the following recursive Java function f that takes two long arguments and returns a float value:

    public static float f (long m, long n) {

    float result = (float)m / (float)n;

    if(m < 0 | n < 0) return 0.of;

    else result - = f(m * 2,n * 3);

    return result ;

    }

    Which of the following real values best approximates the value of f (1, 3) ?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App