我有以下代码:
struct Foo {
uint16_t a:5;
uint16_t b:5;
uint16_t c:5;
};
uint16_t bits = 0xdae;
const Foo& foo = *reinterpret_cast<const Foo*>(&bits);
printf("%x %x %x\n", foo.a, foo.b, foo.c);
Run Code Online (Sandbox Code Playgroud)
当我在纸上完成时,输出就是我所期望的:
第3版
如果我使用uint32_t而不是uint16_t位域,我会得到相同的结果.
但是当我使用uint8_t位域而不是uint16_t时,我得到一个不一致的结果:
第6版
要么
第16版
两者都不正确.为什么使用uint8_t进行位域会导致这种奇怪的行为?
我在 golang 中有一些 Linux 代码:
import "syscall"
var info syscall.Sysinfo_t
err := syscall.Sysinfo(&info)
totalRam := info.Totalram
Run Code Online (Sandbox Code Playgroud)
我想将其移植到 Mac OS X。我看到 Sysinfo 在 Linux 上可用,但在 Mac 上不可用:
Linux:
$ go list -f '{{.GoFiles}}' syscall | sed -e "s/[][]//g" | xargs fgrep --color -Iwn Sysinfo
syscall_linux.go:962://sysnb Sysinfo(info *Sysinfo_t) (err error)
zsyscall_linux_amd64.go:822:func Sysinfo(info *Sysinfo_t) (err error) {\
Run Code Online (Sandbox Code Playgroud)
苹果电脑:
$ go list -f '{{.GoFiles}}' syscall | sed -e "s/[][]//g" | xargs fgrep --color -Iwn Sysinfo
# No results
Run Code Online (Sandbox Code Playgroud)
在 Mac 上获取系统 RAM 信息的正确方法是什么?