在Go语言中,布尔类型的大小为4个字节。

huangapple go评论87阅读模式
英文:

Bool showing size of 4 bytes in Go

问题

strict属性的类型为bool,显示了两个不同的大小。下面是代码片段及其输出。

案例1:

package main

import (
	"fmt"
	"unsafe"
)

type Encoding struct {
	encode    [64]byte
	decodeMap [256]byte
	padChar   rune
	strict    bool
}

func main() {
	e := Encoding{}

	fmt.Printf("%d", unsafe.Sizeof(e))
}

输出:328

预期输出:输出应该是325字节(64 + 256 + 4 + 1)

案例2:

package main

import (
	"fmt"
	"unsafe"
)

type Encoding struct {
	// encode    [64]byte
	// decodeMap [256]byte
	// padChar   rune
	strict bool
}

func main() {
	e := Encoding{}

	fmt.Printf("%d", unsafe.Sizeof(e))
}

输出:1

英文:

strict property of type bool is showing 2 different sizes. Code snippets and their output are present below.

Case 1:

package main

import (
	"fmt"
	"unsafe"
)

type Encoding struct {
	encode    [64]byte
	decodeMap [256]byte
	padChar   rune
	strict bool
}

func main() {
	e := Encoding{}

	fmt.Printf("%d", unsafe.Sizeof(e))
}

Output: 328

Expected: Output should have been 325 (64 + 256 + 4 + 1) bytes

Case 2:

package main

import (
	"fmt"
	"unsafe"
)

type Encoding struct {
	// encode    [64]byte
	// decodeMap [256]byte
	// padChar   rune
	strict bool
}

func main() {
	e := Encoding{}

	fmt.Printf("%d", unsafe.Sizeof(e))
}

Output: 1

答案1

得分: 1

type Encoding struct {
encode [64]byte
decodeMap [256]byte
padChar rune
strict bool
}


Sizeof(bool) 是1。你的计算是错误的。

结构体的大小是其组成字段的大小,根据字段对齐进行填充。结构体对齐到其最严格的字段对齐方式:rune(int32)或4个字节。所以我们有:

64 + 256 + 4 + 1 = 325

然后,结构体的大小根据数组元素对齐进行填充,以保持不变式

Sizeof(array) = len(array) * Sizeof(struct)

所以,对于4字节的结构体对齐,向上取整到最近的4,我们有:

(325 + (4 - 1)) / 4 * 4 = 328

package main

import (
    "fmt"
    "unsafe"
)

type Encoding struct {
    encode    [64]byte
    decodeMap [256]byte
    padChar   rune
    strict    bool
}

type Encodings [2]Encoding

func main() {
    var e Encoding
    fmt.Printf("%d\n", unsafe.Sizeof(e))
    var es Encodings
    fmt.Printf("%d == %d * %d\n", unsafe.Sizeof(es), len(es), unsafe.Sizeof(e))

    fmt.Println()
    fmt.Println("byte", unsafe.Sizeof(byte(0)))
    fmt.Println("rune", unsafe.Sizeof(rune(0)))
    fmt.Println("bool", unsafe.Sizeof(bool(false)))
}

https://go.dev/play/p/CZT-5v41SfL

328
656 == 2 * 328

byte 1
rune 4
bool 1
英文:

> type Encoding struct {
> encode [64]byte
> decodeMap [256]byte
> padChar rune
> strict bool
> }


The Sizeof(bool) is one. Your computation is incorrect.

The size of a struct is the size of its component fields adjusted with padding for field alignment. The struct is aligned to its most restrictive field alignment: rune (int32) or 4 bytes. So we have:

64 + 256 + 4 + 1 = 325

The size of the struct is then adjusted with padding for array element alignment to preserve the invariant

Sizeof(array) = len(array) * Sizeof(struct)

So, for 4 byte struct alignment, rounding up to the nearest 4, we have:

(325 + (4 - 1)) / 4 * 4 = 328

package main

import (
    "fmt"
    "unsafe"
)

type Encoding struct {
    encode    [64]byte
    decodeMap [256]byte
    padChar   rune
    strict    bool
}

type Encodings [2]Encoding

func main() {
    var e Encoding
    fmt.Printf("%d\n", unsafe.Sizeof(e))
    var es Encodings
    fmt.Printf("%d == %d * %d\n", unsafe.Sizeof(es), len(es), unsafe.Sizeof(e))

    fmt.Println()
    fmt.Println("byte", unsafe.Sizeof(byte(0)))
    fmt.Println("rune", unsafe.Sizeof(rune(0)))
    fmt.Println("bool", unsafe.Sizeof(bool(false)))
}

https://go.dev/play/p/CZT-5v41SfL

328
656 == 2 * 328

byte 1
rune 4
bool 1

huangapple
  • 本文由 发表于 2022年11月4日 15:33:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/74313571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定