benjylxwang
benjylxwang

Reputation: 29

Unhandled exception at 0x00C61540 in Project3.exe: 0xC0000005: Access violation reading location 0x00000003

I'm getting an error whenever run this program a second time. Basically what it does is it writes some information to a file, creating the file if it doesn't exist. However, when it runs the second time, it needs to read the previously created file, which for some reason throws an exception, and I get this error:

Unhandled exception at 0x00C61540 in Project3.exe: 0xC0000005: Access violation reading location 0x00000003.

The debugger points to line 15 in the function getBalance as the place where the exception is thrown. However, I actually call this function more than once, and it only throws the exception the second time I call it.

int getBalance(int lineno){ // Funciton to convert strings in file to ints
    string balance;
//Getting information from the file about locations
int *pointer;
pointer = findNewLines();
static int linenopos[10];

for (int i = 0; i < 11; i++){
    linenopos[i] = *(pointer + i);
}

int balanceInt;

//Opening file
balanceFile.open("E:\\MoneyStuff\\balance.txt", ios::in | ios::out); //Exception is thrown here

//Getting Balances
balanceFile.seekg(linenopos[(lineno - 1)], ios::beg);
getline(balanceFile, balance);

balanceFile.close();

stringstream convert(balance);//Variable to convert string balance to integer balance

//Converting balance string to int
convert >> balanceInt;

//Setting balanceInt to 0 if the file doesn't exist
if (balanceInt < -30000)
    balanceInt = 0;

return balanceInt;
}

Edit: Okay so I corrected the part in my code where I was trying to access an element in an array that doesn't exist, but I'm still getting the same exception. The exception only seems to happen when I try to read from a previously created file, so could it have to do with permissions?

Note: This is my first time asking a question here, so if I need to give you guys more code or more information, please tell me! Thanks

Upvotes: 2

Views: 8778

Answers (4)

karlphillip
karlphillip

Reputation: 93478

When you declare an array with 10 elements, it means your indexes goes from 0 to 9.

Adjust the loop to:

for (int i = 0; i < 10; i++)

Trying to access an element that doesn't exist in the array will cause weird behaviors, and crash is one of them.

As for the exception caused by open(), catch it and print to the screen to have some clues of what's going on:

try {
    balanceFile.open("E:\\MoneyStuff\\balance.txt", ios::in | ios::out);
}
catch (const std::exception& e ) {
     std::cout << e.what() << std::endl;
}

Upvotes: 4

Korvo
Korvo

Reputation: 9753

Your for-loop has 11, see:

static int linenopos[10];

for (int i = 0; i < 11; i++){

the correct would be "10":

static int linenopos[10];

for (int i = 0; i < 10; i++){

But the best way is to count the items before the loop (any chance you will upgrade your vector), try:

static int linenopos[10];

int size = sizeof(linenopos) / sizeof(linenopos[0]);//count items

for (int i = 0; i < size ; i++){

or use vector size, like this:

const int size = 10;//vector size

static int linenopos[size];

for (int i = 0; i < size ; i++){

Upvotes: 1

Oleg
Oleg

Reputation: 1036

In your code

static int linenopos[10];
for (int i = 0; i < 11; i++)

you iterate past the end of the linenopos array.

You should have

for (int i = 0; i != 10; ++i)

Upvotes: 0

Santiclause
Santiclause

Reputation: 870

If the error code is saying "Access violation reading location 0x00000003", then that means you're trying to read a value from null+3.

Your findNewLines() function probably isn't returning what you want it to.

Upvotes: 1

Related Questions