c - 为什么在下面的代码中将 Go 字节传递给 C,字节长度与 cgo 中的字节长度不匹配?
问题描述
将 golang 字节传递给 C 时,字节长度不匹配。
结果 strlen(key) 和 keylen 不匹配。
使用“go build file.go”构建
您可以在此处下载下面的 go 文件: https ://pastebin.com/raw/hnMfXJKq <- 有谁知道为什么我的 cgo 调用 []bytes 到 c 有错误?为什么 strlen 不匹配?
预期的输出应该是相同的密钥长度。有时工作,有时不工作。
package main
/*
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
void replaceK(void *key, size_t keylen, void *value, size_t valuelen);
void replaceK(void *key, size_t keylen, void *value, size_t valuelen)
{
printf("replaceK : key = %s = %lu = %lu = %s = %lu\n",(char*) key,keylen,strlen(key),(char*) value,valuelen);
if (keylen != strlen(key)){
printf("ERROR!!! keylen : %lu != strlen(key) : %lu!!!\n",keylen,strlen(key));
exit(1);
}
}
*/
import "C"
import (
"fmt"
"unsafe"
"math/rand"
"time"
)
func Set(key,value []byte) {
cKey := cByteSlice(key)
cValue := cByteSlice(value)
C.replaceK(unsafe.Pointer(cKey),C.ulong(len(key)),unsafe.Pointer(cValue),C.ulong(len(value)))
C.free(unsafe.Pointer(cKey))
C.free(unsafe.Pointer(cValue))
}
func byteToChar(b []byte) *C.char {
var c *C.char
if len(b) > 0 {
c = (*C.char)(unsafe.Pointer(&b[0]))
}
return c
}
var letterRunes = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) []byte {
randNum := rand.Intn(n)+1
b := make([]byte, randNum)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return b
}
func cByteSlice(b []byte) *C.char {
var c *C.char
if len(b) > 0 {
c = (*C.char)(C.CBytes(b))
}
return c
}
func main() {
rand.Seed(time.Now().UnixNano())
var key []byte
var value []byte
for i:=0;i<10000000;i++ {
key = RandStringRunes(10)
value = RandStringRunes(20)
randnum := 1
if randnum == 1 {
fmt.Printf(">>> = %s = %s\n",key, value)
Set(key,value)
}
}
}
解决方案
Cstrlen
函数用于以空字符结尾的字符串,而不是指针+长度字符串。
printf
您可以使用 C函数使用%.*s
而不是打印指针+长度字符串%s
。由于 Gostring
和[]byte
变量都使用指针+长度编码,这可能是打印从 Go 获得的字符串的正确方法。
推荐阅读
- reactjs - Material-UI 面包屑导航与 react-router 集成
- java - 与多个 testng 测试用例共享相同的数据提供程序,在从 excel 中读取第二个测试用例的数据时给出空指针异常
- owin - 如何使用 OWIN 托管多个 .Net-Core WebApi 在相同的 URL 下,所有 API 使用相同的中间件管道
- javascript - 当我从第一个表格中选择文本时更改另一个表格的背景
- c++ - Visual Studio C++ 仅使用环境变量添加标准标头位置
- php - 刷新页面后 Ajax 不适用于第一次 wordpress/woocommerce
- mysql - Laravel QueryBuilder - 相同查询的 `where()` 和 `whereRaw()` 结果不同
- java - 为什么“selectedCountry”、“selectedAge”、“selectedGender”返回null?
- javascript - 为每个数组项添加单击
- j - 这个空列表的第一个元素如何为 0?