github.com/fananchong/cstruct-go@v0.0.0-20220616060855-b65d9a2f2e17/example/README.md (about)

     1  # 例子说明
     2  
     3  演示 C struct 与 Go struct 内存分布一致
     4  
     5  - main.cpp
     6  
     7    C++ 结构体内存按 1 对齐,并输出结构体对象内存分布
     8  
     9  - main.go
    10  
    11    Go 结构体定义与 C++ 结构体一致,并做序列化,然后输出序列化数据的内存分布
    12  
    13  # 输出结果
    14  
    15  ```shell
    16  D:\golang\src\github.com\fananchong\cstruct-go\example>main.exe
    17  len(b) = 76
    18  struct data len =  47
    19  struct data is:
    20  127 56 231 3 0 0 0 1 2 3 4 184 34 154 153 177 66 56 231 3 0 0 0 1 2 3 4 56 231 3 0 0 0 1 2 3 4 56 231 3 0 0 0 1 2 3 4
    21  D:\golang\src\github.com\fananchong\cstruct-go\example>main_cpp.exe
    22  len(b) = 47
    23  struct data len = 47
    24  struct data is:
    25  127 56 231 3 0 0 0 1 2 3 4 184 34 154 153 177 66 56 231 3 0 0 0 1 2 3 4 56 231 3 0 0 0 1 2 3 4 56 231 3 0 0 0 1 2 3 4
    26  ```
    27  
    28  可以看到,原始 Go 结构体大小为 76
    29  
    30  序列化后,大小为 47 ,且内存分布与 C++ 结构体内存分布一致