Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgo调用c函数时,调用栈发生了什么? #4

Open
xpzouying opened this issue Aug 17, 2020 · 0 comments
Open

cgo调用c函数时,调用栈发生了什么? #4

xpzouying opened this issue Aug 17, 2020 · 0 comments

Comments

@xpzouying
Copy link
Owner

xpzouying commented Aug 17, 2020

cgo调用c函数时,调用栈发生了什么?

参考源码:源码

前言

分别有2个程序,完成相似的功能,其中一个是纯Go函数,另外一个是Go调用C函数,对于函数调用栈有什么区别?

1、cgo程序

package main

/*
int sum(int a, int b) { return a+b; }
*/
import "C"

func main() {
	a, b := C.int(1), C.int(2)
	C.sum(a, b)
}

2、Go程序

package main

//go:noinline
func sum(a, b int) int {
	return a + b
}

func main() {
	a, b := 1, 2
	sum(a, b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant