vector_02_为什么会出现segment fault?

书接上回,运行执行文件后,出现了segment fault错误,为什么?

gdb调试以下,看一下原因。

执行以下命令:

root@localhost:/home/root/testWorkSpace/CPP11/vector$ gdb ./build/move 

进入gdb命令窗口,显示源代码, 并再第8行设置一个断点:

(gdb) list
1       #include 
2       #include 
3       #include 
4       using namespace std;
5       typedef unsigned int uint32;
6       int main(void)
7       {
8           std::vector<uint32> vec_u32;
9           cout<<vec_u32[0]<<endl;
10          return 0;
(gdb) b 8

运行程序,并执行下一步指令,提示如下:

(gdb) next

Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a5f in main () at /home/lki8szh/testWorkSpace/CPP11/vector/main.cpp:9
9           cout<<vec_u32[0]<<endl;
1: vec_u32[0] = <error: Cannot access memory at address 0x0>
2: vec_u32 = std::vector of length 0, capacity 0
(gdb) bt full
#0  0x0000555555554a5f in main () at /home/lki8szh/testWorkSpace/CPP11/vector/main.cpp:9
        vec_u32 = std::vector of length 0, capacity 0

vec_u32 = std::vector of length 0, capacity 0, 该数组未初始化,不能访问,length:0, capcity: 0, 因此出现段错误。

你可能感兴趣的:(C++,c++)