Structure Types and Memory Alignment in C Language

Structure Definition

Defining Structure Types

The syntax for defining a structure type is:

struct StructName {
    // member variables
    int member1;
    float member2;
    char member3;
};

Declaring Structure Variables

Declaring during definition:

struct Point {
    int x;
    int y;
} p1, p2; // declares two Point variables

Separate declaration:

struct Point p1; // declares one Point variable
struct Point p2; // declares another Point variable

In C, it's necessary to use the struct keyword when declaring structure variables because C separates type namespaces from tag namespaces.

In C++, the struct Point creates a type Point, allowing direct usage without struct.

To achieve similar behavior in C, you can use typedef to create an alias:

typedef struct {
    int x;
    int y;
} Point;

Initializing Structure Variables

Using initializer lists:

struct Point p1 = {10, 20};

While convenient, this method requires initializing all members. If new members are added latter, existing initializers may become incompatible.

Individual member assignment:

p1.x = 10;
p1.y = 20;

Pointer Operations with Structures

Similar to regular variables, pointers to structures can be declared and used:

struct node n = {100, 200};
struct node *p = &n;

// All of these are equivalent:
printf("%d\n", n.a);
printf("%d\n", (*p).a);
printf("%d\n", p->a); // arrow operator accesses members through pointer

Memory Alignment in Structures

Alignment Rules

Memory alignment ensures efficient access to structure members by hardware:

  1. The first member starts at address zero.
  2. Subsequent members are aligned to multiples of thier alignment size.
  3. The total size of the structure is a multiple of the largest alignment requirement.
  4. Nested structures follow the same rules recursively.

Member Alignment Details

Alignment depends on compiler defaults and data type sizes:

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

Example structure:

struct MyStruct {
    char a;   // 1-byte alignment
    int b;    // 4-byte alignment
    double c; // 8-byte alignment
};

Here, the structure aligns to 8 bytes since double requires 8-byte alignment.

Padding Behavior

Compiler inserts padding bytes to maintain proper alignment:

struct MyStruct {
    char a;    // offset 0
    // 3 bytes padding
    int b;     // offset 4
    double c;  // offset 8
    int d;     // offset 16
    // 4 bytes padding
};

Total size becomes 24 bytes due to required padding.

Optimizing Structure Layout

Reordering members can reduce memory consumption:

struct S1 {
    char c1;
    int i;
    char c2;
}; // Size: 12 bytes

struct S2 {
    char c1;
    char c2;
    int i;
}; // Size: 8 bytes

Custom Alignment

Use #pragma pack(n) to control alignment:

#pragma pack(1)
struct MyStruct {
    char a;
    int b;
    double c;
}; // No padding

Alternatively, use GCC-specific attribute:

struct MyStruct {
    char a;
    int b;
    double c;
} __attribute__((aligned(16)));

Purpose of Structure Alignment

  1. Hardware Requirements: Certain architectures require specific memory addresses for data types.

  2. Performence Optimization: Proper alignment reduces memory access time.

  3. Data Transfer Efficiency: Critical for network protocols and file formats.

  4. Portability: Ensures consistent behavior across platforms.

Tags: C Language Structures Memory Alignment data layout Compiler Optimization

Posted on Wed, 08 Jul 2026 17:12:58 +0000 by cocpg