c结构体按1字节对齐时, 结构体Fc_tgs_struct与Fc_tgs_struct_2的大小不一致,谁能解答一下
--------------------------------struct.h--------------------------------
#ifndef HEAD_STRUCT_C
#define HEAD_STRUCT_C
#include <stdio.h>
#define BYTE_PACK_ON
#ifdef BYTE_PACK_ON
#pragma pack(1)
#endif
struct Fc_tgs_struct
{
char cFlag;
unsigned int uTime;
char sName[32];
char sPwd[64];
long long llVal;
};
struct Fc_tgs_struct_2
{
unsigned int uTime;
char sName[32];
char sPwd[64];
long long llVal;
char cFlag;
};
#ifdef BYTE_PACK_ON
#pragma pack()
#endif
#endif /// HEAD_STRUCT_C
--------------------------------cCallDemo.go--------------------------------
/**
* Created by huQg on 2018/5/31,031.
*/
package cCallDemo
/*
#include "struct.h"
*/
import "C"
import (
"fmt"
"unsafe"
)
func calccCallStruct() {
ctg := C.struct_Fc_tgs_struct{}
isz := unsafe.Sizeof(ctg)
fmt.Printf("struct Fc_tgs_struct's size = %d\n", isz)
ctg2 := C.struct_Fc_tgs_struct_2{}
isz = unsafe.Sizeof(ctg2)
fmt.Printf("struct Fc_tgs_struct_2's size = %d\n", isz)
}
--------------------------------cCallDemo_test.go--------------------------------
/*
* Created by huQg on 2018/5/31,031.
*/
package cCallDemo
import (
"testing"
)
func TestXYZ(t *testing.T) {
/// struct
calccCallStruct()
}
第 1 条附言 ·
场景是golang调用c的结构体,两个结构体,字段一致,顺序不一样
按1字节对齐的时候,两结构体的size在用c直接调用时是一样的,但是用golang调用时,结果不一样