Data Structure alignment

本文详细介绍了数据结构对齐和填充的概念,它们对于计算机内存管理和CPU性能至关重要。通过示例解释了如何根据字节对齐规则在内存中分配结构体,以及填充字节的作用。同时,通过C++程序展示了不同结构体布局如何影响内存占用。了解这些概念有助于优化程序性能。
摘要由CSDN通过智能技术生成

​​​​​​​reference: Data Structure alignment - GeeksforGeeks

Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment. 
Data alignment: Data alignment means putting the data in memory at address equal to some multiple of the word size. This increases the performance of system due to the way the CPU handles memory. 
Data Structure Padding: Now, to align the data, it may be necessary to insert some extra bytes between the end of the last data structure and the start of the next data structure as the data is placed in memory as multiples of fixed word size. This insertion of extra bytes of memory to align the data is called data structure padding.
Consider the structure as shown below: 
 

struct 
{
    char a;
    short int b;
    int c;
    char d;
}

Now we may think that the processor will allocate memory to this structure as shown below: 

The total memory allocated in this case is 8 bytes. But this never happens as the processor can access memory as fixed word size of 4 bytes. So, the integer variable c can not be allocated memory as shown above. An integer variable requires 4 bytes. The correct way of allocation of memory is shown below for this structure using padding bytes. 
 

The processor will require a total of 12 bytes for the above structure to maintain the data alignment. 
Look at the below C++ program: 

  • CPP

// CPP program to test

// size of struct

#include <iostream>

using namespace std;

// first structure

struct test1

{

    short s;

    int i;

    char c;

};

// second structure

struct test2

{

    int i;

    char c;

    short s;

};

// driver program

int main()

{

    test1 t1;

    test2 t2;

    cout << "size of struct test1 is " << sizeof(t1) << "\n";

    cout << "size of struct test2 is " << sizeof(t2) << "\n";

    return 0;

}

Output: 
 

size of struct test1 is 12
size of struct test2 is 8

For the first structure test1 the short variable takes 2 byte. Now the next variable is int which requires 4 bytes. So, 2 bytes padding is added after short variable. Now, the char variable requires 1 byte but memory will be accessed in word size of 4 byte so 3 byte of padding is added again. So, total 12 bytes of memory is required. We can similarly calculate the padding for the second structure also. Padding for both of the structures is shown below: 
 

struct test1
{
    short s; 
    // 2 bytes
    // 2 padding bytes
    int i;
    // 4 bytes
    char c;
    // 1 byte
    // 3 padding bytes
};

struct test2
{
    int i;
    // 4 bytes
    char c;
    // 1 byte
    // 1 padding byte
    short s;
    // 2 bytes
};

Note :You can minimize the size of memory allocated for a structure by sorting members by alignment.
References : 
1) https://en.wikipedia.org/wiki/Data_structure_alignment 
2) c++ - Why isn't sizeof for a struct equal to the sum of sizeof of each member? - Stack Overflow
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值