用gdb来调试对stl的空vector进行操作而产生的core

        通过前面的学习, 大家应该很了解gdb调试core了, 今天我们用gdb来调试对stl的空vector进行操作而产生的core.


        先不说core, 而是来学学vector的front方法, 如下:

[taoge@localhost test]$ cat main.cpp 
#include <iostream>
#include <vector>

using namespace std;

int main()
{
        vector<int> v;
        v.push_back(1);
        v.push_back(2);

        cout << v.front() << endl;
        v.front() -= 10;

        cout << v[0] << endl;

        return 0;
}

[taoge@localhost test]$ g++ main.cpp 
[taoge@localhost test]$ ./a.out 
1
-9
[taoge@localhost test]$ 
        要注意, v.front()是对vector首元素的引用, 所以可以做左值。  但是, 当vector为空的时候, 就有问题了, 产生了core, 如下:

[taoge@localhost test]$ cat main.cpp -n
     1  #include <iostream>
     2  #include <vector>
     3
     4  using namespace std;
     5
     6  int main()
     7  {
     8          vector<int> v;
     9
    10          cout << v.front() << endl;
    11          v.front() -= 10;
    12
    13          cout << v[0] << endl;
    14
    15          return 0;
    16  }
    17
[taoge@localhost test]$ ls
main.cpp
[taoge@localhost test]$ g++ main.cpp 
[taoge@localhost test]$ ls
a.out  main.cpp
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out  core.2917  main.cpp
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ gdb a.out core.2917 
GNU gdb (GDB) Red Hat Enterprise Linux (7.1-29.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/g
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值