由于Go不支持打包结构,因此我找到了这篇很棒的文章,并举例说明了如何在go中使用打包结构。 https://medium.com/@liamkelly17/working-with-packed-c-structs-in-cgo-224a0a3b708b
问题是,当我尝试用char *代替[10] char时,它不起作用。我不确定此转换如何与[10] char一起使用,而不与char *一起工作。这是从上面的文章中提取并用char *修改的示例代码。
package main
/*
#include "stdio.h"
#pragma pack(1)
typedef struct{
unsigned char a;
char b;
int c;
unsigned int d;
char *e; // changed from char[10] to char *
}packed;
void PrintPacked(packed p){
printf("\nFrom C\na:%d\nb:%d\nc:%d\nd:%d\ne:%s\n", p.a, p.b, p.c, p.d, p.e);
}
*/
import "C"
import (
"bytes"
"encoding/binary"
)
//GoPack is the go version of the c packed structure
type GoPack struct {
a uint8
b int8
c int32
d uint32
e [10]uint8
}
//Pack Produces a packed version of the go struct
func (g *GoPack) Pack(out *C.packed) {
buf := &bytes.Buffer{}
binary.Write(buf, binary.LittleEndian, g)
*out = *(*C.packed)(C.CBytes(buf.Bytes()))
}
func main() {
pack := &GoPack{1, 2, 3, 4, [10]byte{}}
copy(pack.e[:], "TEST123")
cpack := C.packed{} //just to allocate the memory, still under GC control
pack.Pack(&cpack)
C.PrintPacked(cpack)
}
我是第一次与cgo合作,因此如果我在任何时候错了,请纠正我。
最佳答案
您正在将十(零)个GoPack.e
字节写入packed.e
类型的char *
中。这将不起作用,因为根据您的系统,指针将为4或8个字节,因此即使字节代表有效的指针,也会溢出分配的内存量。
如果要使用有效的packed.e
字段创建有效的结构,则需要在C堆中分配10个字节的内存,将字节复制到其中,然后将packed.e
指向此分配的内存。 (释放相应的packed
结构时,您还需要释放此内存)。您不能使用binary.Write
直接执行此操作。
您可以以此为起点:
buf := &bytes.Buffer{}
binary.Write(buf, binary.LittleEndian, g.a)
binary.Write(buf, binary.LittleEndian, g.b)
binary.Write(buf, binary.LittleEndian, g.c)
binary.Write(buf, binary.LittleEndian, g.d)
binary.Write(buf, binary.LittleEndian, uintptr(C.CBytes(g.e))
*out = *(*C.packed)(C.CBytes(buf.Bytes()))
函数C.CBytes(b)
在C堆中分配len(b)
字节,并将字节从b
复制到其中,并返回unsafe.Pointer
。请注意,我已从代码中复制了
*out = *(*C.packed)...
行。这实际上会导致内存泄漏和不必要的副本。最好使用直接将字节写到out
指向的内存中的写程序。也许这个?
const N = 10000 // should be sizeof(*out) or larger
buf := bytes.NewBuffer((*[N]byte)(unsafe.Pointer(out))[:])
这将使bytes.Buffer
直接写入out
结构,而无需经过任何中间存储器。请注意,由于不安全的恶作剧,如果您写入的数据字节数超过out
所指向的字节数,则这很容易发生缓冲区溢出。警告:这很讨厌,并且容易出现与C中相同的问题,并且您需要检查cgo指针规则以确保您不容易受到垃圾回收交互的影响。意见建议:鉴于您说“没有太多有关指针和内存分配的经验”,您可能应避免编写或包含这样的代码,因为它可能会带来麻烦,并且可能不会立即显现出来。
关于c++ - 如何在cgo中从打包的struct处理char *?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63296692/