package main
import "fmt"
import "unsafe"
type A struct {
a uint8
b uint64
}
func main() {
a := &A{}
fmt.Println(unsafe.Sizeof(a))
fmt.Println(unsafe.Sizeof(*a))
}
4
16
Program exited.
but when I test the code in my own environment(linux i386 & win32, both), I get a unreasonable result as below:
$ go version
go version go1.8.1 linux/386
$ go run align.go
4
12
Then I test a similar code in C, the result is same as go playground.
typedef struct _aaa { UINT8 a; UINT64 b; }AAA;
printf("%d",sizeof(int*)); // 4
printf("%d",sizeof(AAA)); // 16
uint64 should aligin in 8 bytes, why it aligns in 4 bytes in go1.8.1.linux-386?Could anyone tell me how I can get the same result with go playground?
Best regards,drew-j
GOOS: nacl
GOARCH: amd64p32
the Machine is amd64, but run 32bit GO.
My Machine is amd64 too, I download this version of GO: https://storage.googleapis.com/golang/go1.8.1.windows-386.zip
I think my envvironment is similiar to GO playground.
Maybe my question should be: Keep the pointer size is 4 bytes, Which version of GO binary I shoud use to make uint64 aligns to 8 bytes?
Best Regards,
xjdrew
drew@drew-PC MSYS /e/examples/c
$ cat align.c
#include <stdio.h>
typedef struct _A {
char a;
long long b;
} A;
int main() {
printf("%d\n", sizeof(int*));
printf("%d\n", sizeof(A));
}
drew@drew-PC MSYS /e/examples/c
$ gcc -o align align.c
drew@drew-PC MSYS /e/examples/c
$ ./align.exe
4
16$ gcc -v
Using built-in specs.
COLLECT_GCC=D:\msys64\mingw32\bin\gcc.exe
COLLECT_LTO_WRAPPER=D:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.3.0/lto-wrapper.exe
Target: i686-w64-mingw32DWORD WINAPI FwpmFilterAdd0(
_In_ HANDLE engineHandle,
_In_ const FWPM_FILTER0 *filter,
_In_opt_ SECURITY_DESCRIPTOR sd,
_Out_opt_ UINT64 *id
);I have call with a FWPM_FILTER0 struct as below:
typedef struct FWPM_FILTER0_ {
GUID filterKey;
FWPM_DISPLAY_DATA0 displayData;
UINT32 flags;
GUID *providerKey;
FWP_BYTE_BLOB providerData;
GUID layerKey;
GUID subLayerKey;
FWP_VALUE0 weight;
UINT32 numFilterConditions;
FWPM_FILTER_CONDITION0 *filterCondition;
FWPM_ACTION0 action;
union {
UINT64 rawContext; // makes this union aligns to 8 bytes
GUID providerContextKey;
};
GUID *reserved;
UINT64 filterId; // filterId aligns to 8 bytes
FWP_VALUE0 effectiveWeight;
} FWPM_FILTER0;