1 Star 0 Fork 0

LynnYuan/golang

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
unsafe_cgo.go 910 Bytes
一键复制 编辑 原始数据 按行查看 历史
lynnyuan 提交于 2021-11-11 08:48 +08:00 . commit history
package main
import (
"fmt"
"unsafe"
)
/*
#include <stdio.h>
#include <stdlib.h>
unsigned char cBytes[16]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
static unsigned char *AllocateBytes() {
printf("--------allocate--------\n");
for(int i = 0; i < 16; i++){
printf("cBytes[%d]=%c\t", i, cBytes[i]);
}
printf("\n--------%p--------\n", &cBytes);
return cBytes;
}
*/
import "C"
func main() {
// 1. 定义声明GO byte[] 切片
goBytes := make([]byte, 16)
for i := 0; i < cap(goBytes); i++{
goBytes[i] = byte(i)
}
// 2. 输出该字节数组
for _, value := range goBytes {
fmt.Printf("%b ", value)
}
fmt.Println()
cBytes := C.AllocateBytes()
_ = cBytes
fmt.Printf("address = %v\n", cBytes)
unsafeBytes := (*[16]byte)(unsafe.Pointer(cBytes))[:]
for i := 0; i < 16; i++{
fmt.Printf("%b ", unsafeBytes[i])
}
fmt.Println()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jianyun/golang.git
git@gitee.com:jianyun/golang.git
jianyun
golang
golang
dc61740a3abe

搜索帮助