Struct Size Discrepancy with Field Reordering
In Go, structs can exhibit different sizes when their fields are arranged in different orders.
Issue 1: Erroneous Struct Sizes
type A struct {<pre class="brush:php;toolbar:false">a bool b int64 c int
}
type B struct {
b int64 a bool c int
}
When running unsafe.Sizeof(A{}), the result is 24 bytes, while unsafe.Sizeof(B{}) prints 16 bytes. Despite having the same set of fields, their differing order leads to this size variation.
Explanation: Implicit Padding
The sizes of structs are influenced by alignment constraints. int64 values require alignment on 8-byte addresses. In structure A, after the bool field (1 byte), 7 bytes of implicit padding are added to ensure alignment for the following int64 field (8 bytes). This explains the 24-byte size.
In structure B, however, only 3 bytes of padding are necessary after the bool field as the int field is only 4 bytes. Thus, the overall size becomes 16 bytes.
Issue 2: Zero-Sized Struct C
type C struct {<br>}<br>
The size of struct C is 0, as it has no fields with a size greater than zero. However, this does not necessarily imply that no memory is allocated for an instance of type C.
Explanation: Implementation Optimization
According to the Go specification, zero-size values may share memory addresses. While the spec suggests using the same address, it is not a requirement. Current Go implementations utilize this optimization to conserve memory for variables with a size of zero. Hence, despite C having a size of zero, the system may still allocate some memory for a variable of type C.
The above is the detailed content of Why Do Go Structs Have Different Sizes Depending on Field Order?. For more information, please follow other related articles on the PHP Chinese website!

This article explains Go's package import mechanisms: named imports (e.g., import "fmt") and blank imports (e.g., import _ "fmt"). Named imports make package contents accessible, while blank imports only execute t

This article details efficient conversion of MySQL query results into Go struct slices. It emphasizes using database/sql's Scan method for optimal performance, avoiding manual parsing. Best practices for struct field mapping using db tags and robus

This article explains Beego's NewFlash() function for inter-page data transfer in web applications. It focuses on using NewFlash() to display temporary messages (success, error, warning) between controllers, leveraging the session mechanism. Limita

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article details efficient file writing in Go, comparing os.WriteFile (suitable for small files) with os.OpenFile and buffered writes (optimal for large files). It emphasizes robust error handling, using defer, and checking for specific errors.

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
