0
string guessingWord[89];
fstream wordList;



wordList.open("wordlist.txt");

for (int i = 0; i < 90; ++i){
    wordList >> guessingWord[i];
}

    wordList.close();

is there anything wrong with this code, when i try to test this code, the Access violation writing location shows up.

user3444448
  • 1
  • 1
  • 1
  • 3
    I smell off-by-one error. – Mysticial Mar 21 '14 at 00:36
  • 1
    @Mysticial This would be consistent with a sort of "canary" address left over by the compiler just outside the reach of the array. And of course the code accesses `guessingWord[89]`. – cnicutar Mar 21 '14 at 00:36
  • 1
    0xCCCCCCCC is uninitialized memory [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](http://stackoverflow.com/q/370195/995714) – phuclv Apr 28 '15 at 15:45

2 Answers2

5

Declaring an array of size 89 means there are 89 elements and those valid indicies are 0-88. Your loop goes from 0-89; hence, you're writing past the end of the array.

That said, you really should be using vector<string>:

vector<string> guessingWord;
string temp;
while ( wordList >> temp )
    guessingWord.push_back( temp );
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
0

Array guessingWord has only 89 elements with the range of indexes [0, 88]

string guessingWord[89];

So this loop is invalid

for (int i = 0; i < 90; ++i){
    wordList >> guessingWord[i];
}

There should be

for (int i = 0; i < 89; ++i){
    wordList >> guessingWord[i];
}

To avoid such errors always use symbolic names for magic numbers. It would be enough to define

size_t N = 89;

string guessingWord[N];

//...

for (int i = 0; i < N; ++i){
    wordList >> guessingWord[i];
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • In case you're wondering why 0xCCCCCCCC, it's usually because the program was compiled in debug mode, and in debug mode, code is added to initialize memory to 0xCCCCCCCC at the start of each function to catch these type of errors. – rcgldr Mar 21 '14 at 00:50