vector.size() 的 GDB 值错误

GDB wrong values for vector.size()(vector.size() 的 GDB 值错误)
本文介绍了vector.size() 的 GDB 值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的 vector.push_back() 会导致我的代码出现一些错误:

A simple vector.push_back() causes some error in my code:

#include <vector>
using namespace std;

int main(int argc, const char *argv[])
{
    vector<unsigned> stack;
    stack.push_back(1);
    stack.push_back(1); //stack.size() becomes 467369971 after this
    stack.push_back(1);
    stack.push_back(1);
    ... more push_back()s ...
    return 0;
}

我正在使用 GDB 来检查它的行为……奇怪的是 stack.size() 在第二个 push_back() 之后出错了.变成467369971!可能有什么问题?我在 Win7 64 位上,我正在使用带有 G++ 4.7.0 的 MinGW

I'm using GDB to check its behavior... and the weird thing is that stack.size() goes wrong after the second push_back(). It becomes 467369971! What may be wrong? I'm on Win7 64-bit, and I'm using MinGW with G++ 4.7.0

下面是 GDB 的输出:

Below is the output of GDB:

(gdb) n
5                       std::vector<unsigned> sta
(gdb) n
6                       stack.push_back(1);
(gdb) display stack.size()
1: stack.size() = 0
(gdb) n
7                       stack.push_back(1);
1: stack.size() = 1
(gdb)
8                       stack.push_back(1);
1: stack.size() = 467369971 //goes wrong here
(gdb)
9                       stack.push_back(1);
1: stack.size() = 467369971
(gdb)
10                      stack.push_back(1);
1: stack.size() = 4         // gets "normal"
(gdb)
11                      stack.push_back(1);
1: stack.size() = 467369971 // wrong again
(gdb)
12                      stack.push_back(1);
1: stack.size() = 6
(gdb)
13                      stack.push_back(1);
1: stack.size() = 7
(gdb)
14                      stack.push_back(1);
1: stack.size() = 8
(gdb)
15                      stack.push_back(1);
1: stack.size() = 467369971
(gdb)
16                      stack.push_back(1);
1: stack.size() = 10
(gdb)
17                      stack.push_back(1);
1: stack.size() = 11
(gdb)
18                      stack.push_back(1);
1: stack.size() = 12
(gdb)
19                      return 0;

推荐答案

可能出了什么问题?

What may be wrong?

这是您的编译器或 GDB 中的错误.它确实使用 g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3 和 GDB 7.4 在 Linux 上重现:

It's a bug in your compiler, or in your GDB. It does not reproduce on Linux using g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3 and GDB 7.4:

(gdb) n
7       stack.push_back(1);
1: stack.size() = 0
(gdb) 
8       stack.push_back(1); //stack.size() becomes 467369971 after this
1: stack.size() = 1
(gdb) 
9       stack.push_back(1);
1: stack.size() = 2
(gdb) 
10      stack.push_back(1);
1: stack.size() = 3
(gdb) 
11      stack.push_back(1);
1: stack.size() = 4
(gdb) 
12      stack.push_back(1); //stack.size() becomes 467369971 after this
1: stack.size() = 5
(gdb) 
13      stack.push_back(1);
1: stack.size() = 6
(gdb) 
14      stack.push_back(1);
1: stack.size() = 7
(gdb) 
15      return 0;
1: stack.size() = 8
(gdb) 
16  }
(gdb) q

不幸的是,要找出应该归咎于哪个工具会有些困难:您需要检查生成的调试信息.相反,您可以尝试使用不同版本的 GCC 和 GDB 重现该问题.如果不同的 GCC 导致错误消失,则可能是 GCC 中的错误.如果不同的 GDB 版本使 bug 消失,那可能是 GDB 的 bug.

Unfortunately, figuring out which tool is to blame here will be somewhat hard: you'll need to examine the debuginfo generated. Instead you may try to reproduce the problem with different versions of GCC and GDB. If varying GCC causes the bug to disappear, it's probably a bug in GCC. If varying GDB version makes the bug disappear, it's probably a GDB bug.

这篇关于vector.size() 的 GDB 值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)