Home > Backend Development > Golang > Why Does Struct Field Order Affect the Size of Go Structs?

Why Does Struct Field Order Affect the Size of Go Structs?

Mary-Kate Olsen
Release: 2024-12-21 03:39:09
Original
371 people have browsed it

Why Does Struct Field Order Affect the Size of Go Structs?

Different Structural Ordering of Fields Impacts Size

In Go, the order in which struct fields are declared can affect the size of the struct. Why?

Case Study

type A struct {
    a bool
    b int64
    c int
}

type B struct {
    b int64
    a bool
    c int
}
Copy after login

Printing the sizes of these structs reveals:

fmt.Println(unsafe.Sizeof(A{})) // Output: 24

fmt.Println(unsafe.Sizeof(B{})) // Output: 16
Copy after login

Even though they have the same fields, their sizes differ.

Field Alignment and Padding

Whether fields are aligned or start at specific memory addresses depends on the target architecture. For instance, int64 requires 8-byte alignment. In A, the first field is bool, which is 1 byte. To align b (int64) correctly, there is a 7-byte implicit padding after a.

In B, since a is followed by int (4 bytes), only 3 bytes of padding are needed. This explains the difference in sizes.

// Offset (in bytes) for struct A
fmt.Println(unsafe.Offsetof(a.a)) // Output: 0
fmt.Println(unsafe.Offsetof(a.b)) // Output: 8
fmt.Println(unsafe.Offsetof(a.c)) // Output: 16

// Offset for struct B
fmt.Println(unsafe.Offsetof(b.b)) // Output: 0
fmt.Println(unsafe.Offsetof(b.a)) // Output: 8
fmt.Println(unsafe.Offsetof(b.c)) // Output: 12
Copy after login

Zero-Size Structs and Variables

type C struct {}
Copy after login

The size of a zero-size struct is zero, indicating no memory allocation. Despite distinct variables referencing zero-size structs, they may share the same memory address:

a := C{}
b := C{}
c := [0]int{}
d := [3]C{}

fmt.Printf("%p %p %p %p %p", &a, &b, &c, &d, &d[2])
Copy after login

Output:

0x21cd7c 0x21cd7c 0x21cd7c 0x21cd7c 0x21cd7c
Copy after login

All addresses are the same, indicating no memory allocation for these zero-size variables.

Conclusion

Struct field ordering can impact size due to alignment requirements and implicit padding. Zero-size structs optimize memory usage by allocating no memory and potentially sharing the same address for distinct variables.

The above is the detailed content of Why Does Struct Field Order Affect the Size of Go Structs?. 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