Home > Backend Development > C++ > body text

Why does `sizeof(myStruct)` return 6 bytes instead of 8 bytes for a struct with three `unsigned short` members?

Susan Sarandon
Release: 2024-10-26 19:17:02
Original
691 people have browsed it

Why does `sizeof(myStruct)` return 6 bytes instead of 8 bytes for a struct with three `unsigned short` members?

Memory Alignment in C Structs: Understanding the Size Discrepancy

When working with C structs, memory alignment plays a crucial role in determining the actual size of the struct in memory. Memory alignment refers to the placement of data structures in memory addresses that are divisible by specific boundaries. This ensures efficient data access and performance optimization.

Consider a 32-bit machine where the memory alignment is typically set to 4 bytes. In this context, a struct composed of several unsigned short members, such as:

typedef struct {
    unsigned short v1;
    unsigned short v2;
    unsigned short v3;
} myStruct;
Copy after login

One would expect the memory size of this struct to be 8 bytes (3 x 2 bytes). However, the sizeof(myStruct) operator returns only 6 bytes. This discrepancy can be attributed to the alignment requirement.

In the first example, each short member occupies 2 bytes. Since the alignment boundary is 4 bytes, no padding is inserted between the members. Thus, the total size remains at 6 bytes.

In contrast, introducing an int member to the struct, as seen below, alters the alignment behavior:

typedef struct {
    unsigned short v1;
    unsigned short v2;
    unsigned short v3;
    int i;
} myStruct;
Copy after login

In this case, the int member requires a 4-byte alignment boundary. To ensure meeting this requirement, 2 bytes of padding are inserted between the last unsigned short member (v3) and the int member (i). This results in a total size of 12 bytes (6 bytes of data in shorts, 2 bytes of padding, and 4 bytes of data in int).

Therefore, the difference in memory size between the two structs arises from the alignment requirements of their member types. In the first struct, the alignment boundary is met by the short members themselves, while in the second struct, additional padding is required to meet the alignment boundary of the int member.

The above is the detailed content of Why does `sizeof(myStruct)` return 6 bytes instead of 8 bytes for a struct with three `unsigned short` members?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!