至少在我这里编译运行起来是没有问题的(g++ 4.8.1),但是在gdb调试的时候有如下状况。或者$在gdb中有什么含义吗?
代码
#include<cstdio>
using namespace std;
int $(int x) { return x; }
int f(int x) { return x; }
int main()
{
return 0;
}
gdb
Breakpoint 1, main () at test.cpp:9
9 return 0;
(gdb) p f(4)
$1 = 4
(gdb) p $(4)
Program received signal SIGSEGV, Segmentation fault.
0x00000004 in ?? ()
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(at 0x0x4) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb)
迷茫2017-04-17 11:20:36
p $(4)
這條指令的意思要這樣解釋:
$代表上一次的歷史值,也就是4,,是f(4)這個函數呼叫的結果,這樣就等同於:
p 4(4)
第一個4作為函數位址,第二個作為函數傳入參數了,相當於:
p 0x4(4)
這樣的一個函數調用,顯然是非法的。
看了下gdb文件並測試下,這算是gdb和g++沒協商好的地方。
由於C、C++不允許變數名稱含有$,所以gdb拉來做關鍵字了,導致gdb的print call等指令無法呼叫函數名為$的函數。 break指令則是可以的。如果gdb不做修改的話,估計這個問題沒辦法解決。
再看下這幾條指令的結果能理解的更好一點:
###p f(7)
= 7
###b $
Note: breakpoint 4 also set at pc 0x400548.
Breakpoint 6 at 0x400548: file strangersys.cpp, line 7.
###b $(int)
Note: breakpoints 2, 3 and 5 also set at pc 0x400533.
Breakpoint 7 at 0x400533: file strangersys.cpp, line 4.
###