-3

I'm not sure why I am getting this error code. I would like help to fix this problem. It runs just fine on my professor's computer but I am not sure why it won't on mine. It should store the items in an array and print the array out in sorted order but it returns that error code. // // main.cpp

//  Test
//

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main() {
    int SIZE;
    cout << "Enter Array Size: ";
    cin >> SIZE;
//Insert tile into an arrav
    string filename;
// Name of the file
    cout<<"Enter filename:";
    cin>>filename;
    string line;
// To read each line from code
    int i=0;
    //Variable to keen count of each line
    string * arr = new string[SIZE];
// arrav to store each line
    ifstream mFile (filename);
    if (mFile.is_open() ){
        while(!mFile.eof()){
            getline (mFile, line);
            arr[i]=line;
            i++;
        }
        mFile.close();
    }
    else
        cout<<"Couldn't open the file\n";
//sort
    string temp;
    for (int i=0; i < SIZE; ++i){
        for (int j=i+1; j < SIZE; ++j){
            if (arr[i] > arr[j]){
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    for (int i = 0; i < SIZE; i++)
        cout << arr[i] << endl;
    return 0;
}
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – πάντα ῥεῖ Feb 01 '22 at 20:06
  • Your loop filling in the array doesn't make any use of `SIZE` and can easily run off the end. Running off the end of an array is a very frequent cause of 0xC0000005 (access violation). – Ben Voigt Feb 01 '22 at 20:06
  • I see `arr[i]=line;` without any check regarding whether `i` is a valid index. – Drew Dormann Feb 01 '22 at 20:07
  • Your compiler may support [UB](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html) and [Address](https://clang.llvm.org/docs/AddressSanitizer.html) sanitizers. Amazing tools for picking off whole families of programming errors. – user4581301 Feb 01 '22 at 20:20
  • 1
    `string * arr = new string[SIZE];` ---> Change to: `std::vector arr(SIZE);` and then change this: `arr[i]=line;` to this: `arr.at(i)=line;`. Now, does throw a `std::out_of_range` exception? If it does, then there is your answer. Now, before you may say "but I can't use vector because my professor...", no one is looking over your shoulder when you write the code to see if anything is going wrong. Use vector to diagnose the problem, then once fixed, go back to writing the code the way the teacher wants. – PaulMcKenzie Feb 01 '22 at 20:21
  • Oh, and you also need `#include ` – PaulMcKenzie Feb 01 '22 at 20:25
  • @PaulMcKenzie terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 8000) >= this->size() (which is 8000)...This is my new error – Jake Parsons Feb 01 '22 at 21:51
  • @JakeParsons -- Exactly the error that was expected. This proves that `SIZE` was too small (8000), and the number of lines read exceeded that. Your next job is to rewrite that loop so that the exception goes away. Once that happens, then change back to the old code, but with the reworked `for` loop. – PaulMcKenzie Feb 01 '22 at 22:01
  • @PaulMcKenzie -- I'm sorry this style of arrays is new to me and I'm am not sure how to fix it from that error. Which for loop do I edit? – Jake Parsons Feb 01 '22 at 22:03
  • @PaulMcKenzie -- alright so (SIZE) = 8002 actually works so should I just add SIZE + 2 everywhere I see "SIZE"? – Jake Parsons Feb 01 '22 at 22:11
  • You could rewrite the loop to never exceed `SIZE`: `while (i < SIZE && getline(mFile, line))` – PaulMcKenzie Feb 02 '22 at 00:05
  • @PaulMcKenzie That helped me figure it out, thank you so much for helping me. – Jake Parsons Feb 02 '22 at 01:33

1 Answers1

1

0xC0000005 is EXCEPTION_ACCESS_VIOLATION/STATUS_ACCESS_VIOLATION, it means you're trying to access a memory you have no business going into. This usually happens when using arrays and dereferencing pointers.

In this case, your std::string array named arr is being accessed with an invalid index (probably a number higher than its actual size).

I suggest using breakpoints as it makes everything easier to debug, check the index everytime the breakpoint is hit, until it throws the access violation exception.

filmor
  • 30,840
  • 6
  • 50
  • 48