如何将C.int地址复制到cgo中的C.char中



使用cgo我正在调用c函数。我遇到了必须将C.int地址复制到C.char[4]中的情况。我能在go中做到吗?

代码片段C-结构:

struct data
{
char    *a_add; 
unsigned int length;
}

Go Code

func main() {
var d[3] C.data
var filedescriptor C.int
d[0].a_add = &filedescriptor 
d[0].length = 4
}

问题是a_add是一个字符*。但我需要传递int变量地址。c代码是一个遗留代码,我现在无法修复数据类型。其他C模块使用它,它在C中工作时会发出警告。哪里是错误。

有没有任何方法可以将int变量的地址复制到cgo中的char*数组中。

更新:我尝试了d[0].a_add=(*C.char((不安全.Pointer(&filedescriptor((,

获取错误:panic:运行时错误:cgo参数具有指向Go指针的Go指针

我错过了什么?

您遇到的问题之一是,在对C代码的调用中,您可能无法将指针传递给Go指针。变量filedescriptor是一个C.int,但&filedescriptor是一个Go指针,因此不能使用它(或者更确切地说,不能将a_add字段中的用作值(。

关于你的C代码有很多我不清楚的地方,但你可能可以使用下面的代码。请注意,对于您的特定情况,此代码可能有些过头了。它并不意味着特别高效或好,就像clear一样,同时非常灵活——例如,它可以从压缩C结构中读取和写入。

package main
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
//
// struct data {
//     char *a_add;
//     unsigned int length;
// };
//
// void f(struct data *p) {
//    printf("p->a_add = %p, p->length = %un", p->a_add, p->length);
//    printf("p->a_add as an int: %dn", *(int *)p->a_add);
//    *(int *)p->a_add = 0x12345678;
// }
import "C"
import (
"fmt"
"unsafe"
)
const cIntSize = C.sizeof_int
// Produce a Go int64 from a C int.  The caller passes the address
// of the C int.
func int64FromCInt(ci unsafe.Pointer) int64 {
// Get a slice pointing to the bytes of the C int.
sci := (*[cIntSize]byte)(ci)[:]
switch {
case cIntSize == unsafe.Sizeof(int64(0)):
var gi int64
sgi := (*[unsafe.Sizeof(gi)]byte)(unsafe.Pointer(&gi))[:]
copy(sgi, sci)
return gi
case cIntSize == unsafe.Sizeof(int32(0)):
var gi int32
sgi := (*[unsafe.Sizeof(gi)]byte)(unsafe.Pointer(&gi))[:]
copy(sgi, sci)
return int64(gi)
case cIntSize == unsafe.Sizeof(int(0)):
var gi int
sgi := (*[unsafe.Sizeof(gi)]byte)(unsafe.Pointer(&gi))[:]
copy(sgi, sci)
return int64(gi)
default:
panic("no Go integer size matches C integer size")
}
}
// Write C int (via an unsafe.Pointer) from Go int.  The caller
// passes the address of the C int.
func writeCIntFromInt(gi int, ci unsafe.Pointer) {
// Get a slices covering the bytes of the C int.
sci := (*[cIntSize]byte)(ci)[:]
switch {
case cIntSize == unsafe.Sizeof(gi):
sgi := (*[unsafe.Sizeof(gi)]byte)(unsafe.Pointer(&gi))[:]
copy(sci, sgi)
case cIntSize == unsafe.Sizeof(int64(0)):
// Copy value to int64 for copying purposes.
// Since int64 holds all int values, this always works.
gi2 := int64(gi)
sgi := (*[unsafe.Sizeof(gi)]byte)(unsafe.Pointer(&gi2))[:]
copy(sci, sgi)
case cIntSize == unsafe.Sizeof(int32(0)):
// Copy value to int32 for copying purposes.
// Panic if we destroy the value via truncation.
gi2 := int32(gi)
if int(gi2) != gi {
panic(fmt.Sprintf("unable to send Go value %x to C: size of Go int=%d, size of C int=%d", gi, unsafe.Sizeof(gi), cIntSize))
}
sgi := (*[unsafe.Sizeof(gi)]byte)(unsafe.Pointer(&gi2))[:]
copy(sci, sgi)
default:
panic("no Go integer size matches C integer size")
}
}
func main() {
b := C.malloc(cIntSize)
defer C.free(b)
writeCIntFromInt(32767, b)
d := C.struct_data{a_add: (*C.char)(b), length: cIntSize}
fmt.Println("calling C.f(d)")
C.f(&d)
result := int64FromCInt(unsafe.Pointer(d.a_add))
fmt.Printf("result = %#xn", result)
}

最新更新