bugged compiler??

Hi Everyone,
I have a function that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void load_file(string filename) {
    typ_small arr[N];
    for(j = 0; j != N; j++) arr[j] = 0;

    ifstream in (filename.c_str(), ios::in | ios::binary);
    in.read((char *) &arr, sizeof(arr[N]) );

    cout << in.gcount() << " bytes read\n";

    for (int someint = 0; someint != 500; someint++) cout << someint << " ";

    in.close();
}


Note the inclusion of this line: for (int someint = 0; someint != 500; someint++) cout << someint << " ";

The function counts to 282, then returns a segmentation fault. The exact same loop in the main function simply counts to 499 as you would expect. Is this a compiler bug or am I missing something? I'm using code::blocks on ubuntu...

FYI: this also results in a segmentation fault in the function: int blah = 5; cout << blah;
Last edited on
My guess is that you corrupted the stack or the heap at some earlier point and the program crashes at this point by random chance.
Run it under valgrind and see if you've trashed something you really shouldn't have.
I don't understand the 6th line in your function . &arr is pointer to pointer to arr[0] ( because arr is already pointer to arr[0]); Are you trying to load all data from file to arr[0] ? if yes, then you must write in.read( (char*)&arr[0], sizeof( typ_small ))
Ah thanks, yeah I was using an integer to point to a memory location (rather than using a pointer). The compiler response was extremely confusing in this case though... I guess I know now that segmentation errors can't be debugged the way compile-time errors are..
Topic archived. No new replies allowed.