views:

318

answers:

3

When I run a program using the read part of fstream, I get this return value:

-1073741819

the actual function is part of a corrupt for loop, which I will try to explain:

for(int i = 0; i < vrs_top_i * 3; i += 3)
{
 int X1x = FileRead(file2, i + 1);
 int X1y = FileRead(file2, i + 2);
 char X1sym = FileRead(file2, i + 3);
 viral_data.add(new X1(X1x, X1y, X1sym));
}

vrs_top_i is a variable declared like this: int vrs_top_i = FileRead(file2, 0);

add is a function for a custom list I made, essentially the same as push_back() for vectors. X1 is a class I made with a constructor that takes three arguments, two ints and a char.

Now for the corrupt part of the loop:

now, when I put "exit(0);" under the third line of the loop "char X1sym...+ 3);" (or anywhere else in the loop, for that matter) It does what you expect: ends the program with a return value of zero.

but when I put "if(i == 0)exit(0);", or "if(i == 3)", I get the aforementioned return value.

So I'm guessing that means that i is never 0 or 3.

So does anybody know what the return value means?

NB: FileRead is declared like so:

int FileRead(std::fstream& file, int pos)
{
int data;
file.seekg(file.beg + pos * sizeof(int));
file.read(reinterpret_cast<char*>(&data), sizeof(data));
return data;
}
A: 

At least one the reason your code doesn' work as intended:

Your FileRead reads i-th int from a file. But you are using it to read a char, which breaks the math of calculating your addresses. Your record length is not 3 * sizeof(int), but 2 * sizeof(int) + sizeof(char).

In other words, your seekg will be set to an incorrect position after the reading of a char.

Igor Krivokon
+6  A: 

Convert it to hex and you'll get 0xc0000005. An error code you should get to know well. It's a general protection fault, or dereferencing a garbage pointer in other words.

Paul Mitchell
+12  A: 

-1073741819 is the decimal representation of 0xC0000005. This is not actually a return value--it's a Windows code for STATUS_ACCESS_VIOLATION, which is what happens when your program accesses invalid memory.

Effectively, your program has a bug and is accessing invalid memory and crashes. The code that you're seeing is Windows' way of telling you so.

JSBangs