gdb - C++中变量、函数等名字中能不能含有$字符?
黄舟
黄舟 2017-04-17 11:18:36
0
1
559

至少在我这里编译运行起来是没有问题的(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)
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
迷茫
p $(4)

The meaning of this command is explained as follows:
$ represents the last historical value, which is 4, which is the result of the function call f(4), which is equivalent to:

p 4(4)

The first 4 is used as the function address, and the second one is used as the function parameter, which is equivalent to:

p 0x4(4)

Such a function call is obviously illegal.

After reading the gdb document and testing it, this is where gdb and g++ have not negotiated well.
Since C and C++ do not allow variable names to contain $, gdb uses it as a keyword. As a result, gdb's print call and other commands cannot call functions named $. The break command is OK. If gdb is not modified, this problem may not be solved.

Look at the results of these commands to understand it better:

###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.
###
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template