gdb调试和编译后运行结果不一致

今天在看代码时,遇到这么一段代码,我但是用g++编译了,运行发现有Segmentation fault。

然后就用gdb跟进去看看,可是gdb却正常执行了。不知道什么原因。

#include <iostream>
using namespace std;

int main(){
	char a[] = "hello";
	a[0] = 'x';
	cout << a << endl;
	char *p = "world";
	p[0] = 'x';
	cout << p << endl;

	return 0;
}  

以下为运行的情况

stephen@DESKTOP-6NDH281:/mnt/h/dev/c++$ ./a.out
xello
Segmentation fault (core dumped)

以下为gdb跟进去的情况,打印出来了xorld

Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) list
1       #include <iostream>
2       using namespace std;
3
4       int main(){
5               char a[] = "hello";
6               a[0] = 'x';
7               cout << a << endl;
8               char *p = "world";
9               p[0] = 'x';
10              cout << p << endl;
(gdb) break 10
Breakpoint 1 at 0x40090b: file testpointer.cpp, line 10.
(gdb) run
Starting program: /mnt/h/dev/c++/a.out
xello

Breakpoint 1, main () at testpointer.cpp:10
10              cout << p << endl;
(gdb) s
xorld
12              return 0;
(gdb) quit

费解啊。后面再看吧。

猜你喜欢

转载自www.cnblogs.com/javametro/p/9593186.html
今日推荐