Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird behaviour of GDB

Tags:

c++

gdb

I've been trying to debug a little thingie, and almost went insane while trying to do so. After several hours of figuring out the problem, I finally have a snippet of code that is the root of my problem:

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

int main()
{
    std::vector<int> foo = std::vector<int>();

    foo.push_back(0);
    foo.push_back(11);
    foo.push_back(222);
    foo.push_back(3333);

    std::stack<int> bar = std::stack<int>();

    cout << endl << foo.size() << endl << endl;

    return 0;
}

With this compiled, using:

g++ -std=c++11 -ggdb -O0 -pedantic -Wall -Wextra -Wno-nonnull -fkeep-inline-functions

I then try the following:

(gdb) br 18
Breakpoint 1 at 0x40170c: file ./....cpp, line 18.
(gdb) r
Starting program: ...
[New Thread 15620.0x3c3c]

Breakpoint 1, main () at ./....cpp:18
18              cout << endl << foo.size() << endl << endl;
(gdb) p foo.size()
$1 = 4293588256
(gdb) c
Continuing.

4

[Inferior 1 (process 15620) exited normally]
(gdb)

Apparenly, 4 is now equal to 4293588256. What the bloody hell is going on? Also, if I break the program before the stack is created, GDB shows the size properly.

EDIT: I am on windows 8.1, versions of stuffs are: G++ 4.8.1; GDB 7.6.1

like image 402
user1982779 Avatar asked May 03 '15 17:05

user1982779


1 Answers

Turns out this really is a problem of either GDB 7.6.1 or G++ 4.8.1. Updating GDB to 7.8.1 and G++ to version 4.9.2 solved the problem.

like image 163
user1982779 Avatar answered Oct 13 '22 01:10

user1982779