Go语言和C语言之间实现内存共享的坑

摘要

在Go和C内存共享时,可能会出现错误cgo argument has Go pointer to Go pointer。该问题出现的主要原因是在Go1.6.2版本之后,GO语言的rumtime检查机制对C和GO之间的指针传递行为进行了强制检查,不允许传递指向Go内存地址的指针。

正文

在Go1.6.2版本之后,Go的runtime加入了指针违规传递检测机制。该机制主要针对Go向C传递带有指向其他Go内存的地址,具体见文献检查机制

  • 情况1 不能传递带有初始化内存空间的指针切片
package main

//#cgo CFLAGS: -std=c99
/*
#include <stdio.h>
#include <stdlib.h>

void array(int** arr,int n){
	for (int i=0;i<n;i++){
		*arr=(int*)malloc(sizeof(int));
		**arr=i+i;
		arr++;
	}
}
*/
import "C"
import (
	"fmt"
	"runtime"
	"unsafe"
)

func main() {
	fmt.Printf("Allocate a memory through Golang coding.\n");
	c:=make([]*C.int,3);
	for i,_:=range c{
		fmt.Println(i,c[i])
	}
	//"Pass the first pointer of the memory to C code to process.
	C.array((**C.int)(unsafe.Pointer(&c[0])),3);
	fmt.Printf("Display the processing result.\n")
	for i,_:=range c{
		fmt.Println(i,c[i],*c[i]);
	}
	runtime.GC();
}

执行结果:
在这里插入图片描述

package main

//#cgo CFLAGS: -std=c99
/*
#include <stdio.h>
#include <stdlib.h>

void array(int** arr,int n){
	for (int i=0;i<n;i++){
		*arr=(int*)malloc(sizeof(int));
		**arr=i+i;
		arr++;
	}
}
*/
import "C"
import (
	"fmt"
	"runtime"
	"unsafe"
)

func main() {
	fmt.Printf("Allocate a memory through Golang coding.\n");
	c:=make([]*C.int,3);
	for i,_:=range c{
		c[i]=new(C.int)
		fmt.Println(i,c[i])
	}
	//"Pass the first pointer of the memory to C code to process.
	C.array((**C.int)(unsafe.Pointer(&c[0])),3);
	fmt.Printf("Display the processing result.\n")
	for i,_:=range c{
		fmt.Println(i,c[i],*c[i]);
	}
	runtime.GC();
}

执行结果:
在这里插入图片描述

  • 情况2 Go不能向C传递带有初始化内存空间的结构体
package main

//#cgo CFLAGS: -std=c99
/*
#include <stdio.h>
#include <stdlib.h>

typedef struct{
	int* Number;
}A;


void transfer(A *a){
	if (a->Number==NULL){
       a->Number=(int*)malloc(sizeof(int));
	}
	*(a->Number)=4;
}

*/
import "C"
import (
	"fmt"
	"unsafe"
)

func main() {
	var S A
	fmt.Printf("*****before being transfered to C!\n");
	S.Number=new(C.int)
	fmt.Println(S.Number)
	C.transfer((*C.A)(unsafe.Pointer(&S)))
	fmt.Printf("*****after being transfered to C!\n");
	fmt.Println(*S.Number)

}

type A struct{
	Number *C.int
}

执行结果
在这里插入图片描述

  • 情况3 不能传递指向已知内存的指针给C语言
package main

//#cgo CFLAGS: -std=c99
/*
#include <stdio.h>
#include <stdlib.h>

typedef struct{
	int* Number;
}A;


void transfer(int **a){
     printf("*=%p,**a=%d\n",*a,**a);
	 printf("fix value of point to 16\n");
	 **a=16;
}

*/
import "C"
import "fmt"

func main() {
	point:=new(C.int);
	*point=4;
	fmt.Println("******before being transfered to C Code!")
	fmt.Printf("point=%p,*point=%d\n",point,*point)
	C.transfer(&point);
	fmt.Printf("point=%p,*point=%d\n",point,*point)
}

执行结果为
在这里插入图片描述

解决办法

解决办法主要有两类,临时性解决和永久性解决。临时性解决主要针对某次编译绕开检测,缺点是每次执行都要带参数GODEBUG=cgocheck=0这个标识;永久性解决则将该GODEBUG=cgocheck=0做成系统环境变量,使每次运行都自动调用该参数。

  • 临时性解决

在编译开始时,加入GODEBUG=cgocheck=0,绕开rutime的编译检查机制,具体执行方法如下所示:
在这里插入图片描述

  • 永久性解决

将GODEGUG=cgocheck=0,加入Linux系统的~/.bashrc文件的末尾,实现启动默认添加该变量。
在这里插入图片描述
执行结果为
在这里插入图片描述

总结

在CGO交互中,出现报错cgo argument has Go pointer to Go pointer情形的主要原因是,GO语言的rumtime检查机制对C和GO之间的指针传递行为进行了强制检查。绕开检查机制是目前已知的处理方式。其实,为了避免导致不必要的内存泄露,尽量不用上述的指针传递方式进行C语言和GO的内存共享。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值