0

I need help with figuring out what exactly is this error and why is it occuring. When I debud the program everything seems to be working perfectly fine! I do not get this.

Code:

 void WordsRandom()
{
    srand((unsigned)time(NULL));
    n = rand() % 10;
    checkRandom[k] = n;  //ERROR HERE -->  THREAD 1: EXC_BAD_ACCESS
    k ++; //k is set to 0 originally
    rowRandom = getRandomNumber();
    colRandom = getRandomNumber();
}

I do not know why this is happening. As far as I know, in this case there is no need to use the %d specifier right? I f you require further code just let me know.

WordsRandom is being called by other methods:

void horizontalOrientation()
{
    size = (int)strlen(words[n]);
    if((colRandom + (size - 1)) <= 9)
    {
        for(int j = 0; j < (strlen(words[n])); j ++)
        {
            puzzle[rowRandom][colRandom] = words[n][j];
            colRandom ++;
        }
    }
    else
    {
        do
        {
            WordsRandom();
            horizontalOrientation();
        }while((colRandom + (size - 1)) > 9);
   }
}

I have other functions similiar to this for vertical and diagonal orientation.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • What is the declaration of `checkRandom`? How many times are you calling `WordsRandom`? – Aasmund Eldhuset Dec 08 '15 at 18:05
  • Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Dec 08 '15 at 18:07
  • @AasmundEldhuset `int checkRandom[4];` I edited the post to answer your second questions – Larry King Dec 08 '15 at 18:08
  • What has `%d` to do with the code or question in hand? That is a bit random, so to speak. – Clifford Dec 08 '15 at 18:11
  • You should respond to requests for clarification by editing the question rather than providing information essential to the question in comments. The declaration of `checkRandom` should be included in the question. Other people answering should not have to read the comments too. – Clifford Dec 08 '15 at 18:14
  • @LarryKing: `n = rand() % 10;` will cause `n` to get some value between 0 and 9 (inclusive) - so there's a 60% chance that the number you get will be outside of the bounds of the array (valid indices are 0 through 3). Either increase the size of the array, or reduce the size of the right operand of `%`. – Aasmund Eldhuset Dec 08 '15 at 18:14
  • @Clifford Because the last time I had this error was because I forgot to put that specifier in a `scanf` function lol – Larry King Dec 08 '15 at 18:15
  • 1
    There is no scanf() in this code! Most likely k > 3. Call `srand()` once and only once - not every time you call `rand()` – Clifford Dec 08 '15 at 18:17

1 Answers1

0

Two things are troublesome here:

I do not see declarations for either the size of your array (checkRandom[]) or the integer value that you're using to index it (k). If you're receiving an access error, then chances are that you're indexing past the boundaries of your array into memory that isn't yours.

I'd try refactoring your code so that you can check you aren't passing the maximum index of your array (size -1)

J. Murray
  • 1,460
  • 11
  • 19
  • Ok, I understand the error clearer now and I think I know what is causing it. I created that `checkRandom[]` array to avoid generating the same word more than once (BDW the purpose of this code is a word search program). What I think is happening is when the word does not fit the grid, another word is being generated, and thus another index is being generated and at times it may exceed the `checkRandom[]` size. – Larry King Dec 08 '15 at 18:14
  • @LarryKing: If you think that's possible, then add an output statement (cout, printf) so that you can inspect the value of k when its in your function WordsRandom(). It might give you insight that you can use to find the real cause of your problem. – J. Murray Dec 08 '15 at 18:20
  • Yep that's what I am doing... The problem seems to be that! Now I have to try and find another algorithm to implement this no duplication thing. Thanks for your help. – Larry King Dec 08 '15 at 18:27
  • No problem. If this was helpful and you feel the question is answered, I'd probably mark this as the answer so the community can see that it is "Closed" – J. Murray Dec 08 '15 at 18:29