c - Packed Structs in (gcc)go -
c - Packed Structs in (gcc)go -
i have old c code makes heavy utilize of packed structures. i'm looking using go wrapper code, having difficulty finding way pass or write definitions these structures.
example:
import "unsafe"; type aligntest struct { c byte; y int16; z int16; q int32; } func main() { vr := new(aligntest); fmt.println(unsafe.sizeof(*vr), "\n"); } returns 12 rather 1+2+2+4 = 9 want packed/unaligned struct.
i know create byte array , parsing manually, seems brittle , error prone...
you may want rethink architecture- seek passing binary input downwards c layer , utilize existing structures (you won't break don't change). i'm assuming construction packing looks this:
#ifdef windows #pragma pack(push) #endif #pragma pack(bytealignment) // e.g. "#pragma pack(1)" or "#pragma pack(8)" //--- packed structs #ifdef windows #pragma pack(pop) #endif #ifdef posix #pragma pack() #endif all underlying or 3rd party libs doing taking void* or const char* , typecasting these. if possible, seek forwarding info c layer (where can pointers) , don't expose structures @ all.
c gcc struct go
Comments
Post a Comment