匿名函数调用方式
匿名函数调用方式有两种,第一种是作为返回值的匿名函数,第二种是直接调用匿名函数。上面的例子是作为返回值的匿名函数。
作为返回值的匿名函数
我们首先使用 GBD 调试上面的例子。
$ gdb test
$ l
$ l
$ b 16 #test()返回匿名函数f,f可以看作一个指针指向main.test.func1函数
$ r #执行到断点位置
$ info locals #查看局部变量
z = 4567424
f = {void (int, int *)} 0xc420043f68
可以看到f
指向一个函数指针,签名{void (int,int *)}
和函数func(x int) int
不一样,执行的地址是运行期的地址。
$ info address main.test.func1 #查看main.test.func1函数是编译期地址
Symbol "main.test.func1" is a function at address 0x450a40.
main.test.func1
函数的地址是0x450a40
,而f = {void (int, int *)}
地址是0xc420043f68
,这个地址不是在编译期生成的是运行期的地址。因为编译期地址都是短地址,而且main.t