介绍
因为c++只规定了 虚继承/ 虚函数/ 多继承/ 的行为, 但将实现方法留给编译器作者. 所以各个平台的实现并不相同, 得出的结果也不尽相同.
经测试, vs和gcc目前比较统一的情况只有2种 :
- 无继承+无虚函数
- 无继承+虚函数
故本文只讨论这2种, 以及了解虚函数和虚继承的含义.
. . .
因为c++只规定了 虚继承/ 虚函数/ 多继承/ 的行为, 但将实现方法留给编译器作者. 所以各个平台的实现并不相同, 得出的结果也不尽相同.
经测试, vs和gcc目前比较统一的情况只有2种 :
故本文只讨论这2种, 以及了解虚函数和虚继承的含义.
. . .
1 | #hello.py |
python作为一种脚本语言,我们用python写的各个module都可以包含以上那么一个累死c中的main函数,只不过python中的这种__main__
与c中有一些区别,类似于php的魔术那一套, 主要体现在:
1、当单独执行该module时,比如单独执行以上hello.py: python hello.py,则输出
1 | This is main of module "hello.py" |
可以理解为"if __name__=="__main__":"
这一句与c中的main()函数所表述的是一致的,即作为入口;
2、当该module被其它module 引入使用时,其中的"if __name__=="__main__":"
所表示的Block不会被执行,
这是因为此时module被其它module引用时,
其__name__
的值将发生变化,__name__
的值将会是module的名字。
比如在python shell中import hello后,查看hello.__name__
:
1 | import hello |
3、因此,在python中,当一个module作为整体被执行时,moduel.name的值将是"__main__";
而当一个module被其它module引用时,module.__name__
将是module自己的名字,
当然一个module被其它module引用时,其本身并不需要一个可执行的入口main了。
#if的后面接的是表达式 :
1 | #if (MAX==10)||(MAX==20) |
它的作用是:如果(MAX==10)||(MAX==20)成立,那么编译器就会把其中的#if 与 #endif之间的代码编译进去(注意:是编译进去,不是执行!!)
#if后面接的是一个宏, 而#if define(x)的使用如下 :
1 | #if defined (x) |
这个#if defined它不管里面的“x”的逻辑是“真”还是“假”它只管这个程序的前面的宏定义里面有没有定义“x”这个宏,如果定义了x这个宏,那么,编译器会编译中间的…code…否则不直接忽视中间的…code…代码。
另外 #if defined(x)也可以取反,也就用 #if !defined(x)
最后强调两点:
在编译可执行文件时需要给 gcc 加上 “-g” 选项,这样它才会为生成的可执行文件加入额外的调试信息。
不要使用编译器的优化选项,比如: “-O”,”-O2”。因为编译器会为了优化而改变程序流程,那样不利于调试。
在 GDB 中执行 shell 命令可以使用:shell command
GDB 命令可以使用 TAB 键来补全。按两次 TAB 键可以看到所有可能的匹配。
GDB 命令缩写:例如 info bre 中的 bre 相当于 breakpoints。
给 test.c 的第10行设置一个断点 :
b test.c:10
断点的删除与断点的设置同样的重要。删除断点的命令有两个:
用法:delete [breakpoints num] [range…]
delete可删除单个断点,也可删除一个断点的集合,这个集合用连续的断点号来描述。
例如:
delete 5
delete 1-10
用法:
clear 删除断点是基于行的,不是把所有的断点都删除。
例如:
clear list_insert //删除函数的所有断点
clear list.c:list_delet //删除文件:函数的所有断点
clear 12 //删除行号的所有断点
clear list.c:12 //删除文件:行号的所有断点
对断点的控制除了建立和删除外,还可以通过使能和禁止来控制,后一种方法更灵活。
断点的四种使能操作:
用法举例:
diable //禁止所有断点
disble 2 //禁止第二个断点
disable 1-5 //禁止第1到第5个断点
GDB的命令很多, 有些用得少的命令记不住的话, 可以在进入GDB之后敲 “help”, 然后再敲 “help + command_class”,
比如 :
(gdb) help
List of classes of commands:
aliases – Aliases of other commands
breakpoints – Making program stop at certain points
data – Examining data
files – Specifying and examining files
internals – Maintenance commands
obscure – Obscure features
running – Running the program
stack – Examining the stack
status – Status inquiries
support – Support facilities
tracepoints – Tracing of program execution without stopping the program
user-defined – User-defined commands
Type “help” followed by a class name for a list of commands in that class.
Type “help all” for the list of all commands.
Type “help” followed by command name for full documentation.
Type “apropos word” to search for commands related to “word”.
Command name abbreviations are allowed if unambiguous.
(gdb) help running
Running the program.
List of commands:
advance – Continue the program up to the given location (same form as args for break command)
attach – Attach to a process or file outside of GDB
continue – Continue program being debugged
detach – Detach a process or file previously attached
detach checkpoint – Detach from a checkpoint (experimental)
detach inferiors – Detach from inferior ID (or list of IDS)
disconnect – Disconnect from a target
finish – Execute until selected stack frame returns
handle – Specify how to handle signals
inferior – Use this command to switch between inferiors
interrupt – Interrupt the execution of the debugged program
jump – Continue program being debugged at specified line or address
kill – Kill execution of program being debugged
kill inferiors – Kill inferior ID (or list of IDs)
next – Step program
nexti – Step one instruction
reverse-continue – Continue program being debugged but run it in reverse
reverse-finish – Execute backward until just before selected stack frame is called
reverse-next – Step program backward
reverse-nexti – Step backward one instruction
reverse-step – Step program backward until it reaches the beginning of another source line
reverse-stepi – Step backward exactly one instruction
run – Start debugged program
…