Segmentation fault on returning from main (very short and simple code, no arrays or pointers)

Posted by Gábor Kovács on Stack Overflow See other posts from Stack Overflow or by Gábor Kovács
Published on 2011-01-15T23:22:56Z Indexed on 2011/01/15 23:53 UTC
Read the original article Hit count: 210

Filed under:

I've been wondering why the following trivial code produces a segmentation fault when returning from main():

//Produces "Error while dumping state (probably corrupted stack); Segmentation fault"

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

class Test
{
    vector<int> numbers;
};

int main()
{
    Test a;
    ifstream infile;

    cout << "Last statement..." << endl; // this gets executed
    return 0;
}

Interestingly, 1) if only one of the two variables is declared, I don't get the error, 2) if I declare a vector variable instead of an object with a vector member, everything's fine, 3) if I declare an ofstream instead of an ifstream, again, everything works fine. Something appears to be wrong with this specific combination...

Could this be a compiler bug? I use gcc version 3.4.4 with cygwin.

Thanks for the tips in advance.

Gábor

© Stack Overflow or respective owner

Related posts about c++