EXC_BAD_ACCESS with arrays

Discussion in 'Mac Programming' started by jsmwoolf, Oct 22, 2011.

  1. jsmwoolf, Oct 22, 2011
    Last edited: Oct 22, 2011

    jsmwoolf macrumors regular

    Joined:
    Aug 17, 2011
    #1
    Code:
    void multiplyNumbers(int space,int number[], int number2[], int *toStore)
    {
        int initialSpace[space]; //For temporary numbers
        for(int x = 0; x < space; x++)
        {
            initialSpace[x]=0;
        }
        int runThrough=0; //Responsible for adding zeros.
        for(int i = space - 1; i >=(space - numberLength(space, number2)); --i)
        {
            for(int j = space - 1; i >=(space - numberLength(space, number)); --j)
            {
                for(int k = (space - 1); k >= (k - runThrough); k--)
                {
                    initialSpace[k] = (k==(k-runThrough)?number[j]:0);
    ...
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    1. Post the code for numberLength().

    2. Post the input that causes the crash, i.e. show the actual input data and actual call to multiplyNumbers().

    3. Post a complete multiplyNumbers() implementation, so others can actually compile and run it.

    4. Post the crash-log stack-trace.

    5. What have you tried to debug this?


    If you're consistently having problems with array overflow in C, you might want to switch to Java, at least until your code is working. In Java, when you try to access an element outside the valid range, you'll get an exception and a more easily visible stack trace.
     
  3. dylanryan macrumors member

    Joined:
    Aug 21, 2011
    #3
    From the code you posted, I think your problem is this line:

    Code:
    for(int k = (space - 1); k >= (k - runThrough); k--)
    Unless runThrough is changed in code you didn't post, it will be 0, which looks suspicious. If you replace the condition with k>=k, it's pretty obvious what will happen. Of course, without more code, that's just a blind guess.
     
  4. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #4
    I walked away from the computer for 10 minutes and it somehow just post this without me clicking it. Weird.

    The numberLength code.
    Code:
    int numberLength(int space, int number[])
    {
        for(int digit = 0; digit < space; digit++)
        {
            if(number[digit]!=0)
            {
                 return (space - digit);
            }
        }
        return 0;
    }
    The complete multiplyNumbers code.
    Code:
    void multiplyNumbers(int space,int number[], int number2[], int *toStore)
    {
        int initialSpace[space]; //For temporary numbers
        for(int x = 0; x < space; x++)
        {
            initialSpace[x]=0;
        }
        int runThrough=0; //Responsible for adding zeros within a loop.
        for(int i = space - 1; i >=(space - numberLength(space, number2)); --i) //Grab a digit from the second number
        {
            for(int j = space - 1; i >=(space - numberLength(space, number)); --j)
            {
                for(int k = (space - 1); k >= (k - runThrough); k--)
                {
                    initialSpace[k] = (k==(k-runThrough)?number2[i]:0);
                }
                for(int k = 0; k <= number[j]; k++)
                {
                    addSingleNumbers(space, initialSpace, toStore);
                }
                runThrough = (j == space - numberLength(space, number)? 0: ++runThrough);
            }
        }
    }
    I get Thread 1: Program Received Signal: EXC_BAD_ACCESS. However, if there is a more specific way on how to grab it,can you tell me how?

    The debug says this
    which probably does no good.
     
  5. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #5
    What development environment are you using? Learn how to use the debugger.
     
  6. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
  7. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #7
    Xcode has a powerful graphical front end of gdb, you can set breakpoints and step through the execution line by line and observe values of variables. Most likely one of your index variables gets a value that is out of the legal range of your array.
     
  8. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #8
    It appears that k is way out of range which is probably the culprit of my problem.
    It should be between (space - 1) and 0, not -2096645. Why is it -2096645?
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    This code sears the retinas.

    Compare:
    Code:
    initialSpace[k] = (k==(k-runThrough)?number2[i]:0);
    
    vs.
    Code:
    initialSpace[k] = 0;
    if(runThrough == 0) {
      initialSpace[k] = number2[i];
    }
    or

    Code:
    runThrough = (j == space - numberLength(space, number)? 0: ++runThrough);
    
    vs.
    Code:
    int digitLength = space - numberLength(space, number);
    if(digitLength == j) {
      runThrough = 0;
    } else {
      runThrough++;
    }
    
    The problem is:
    Code:
    k >= (k - runThrough)
    This can be rewritten as:
    k + runThrough >= k
    then:
    runThrough >= 0

    When does runThrough go negative? Ever? Is runThrough changed in that loop? If not, the loop will run forever (or until you crash).

    -Lee
     
  10. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    Is that the initial value of k? This (from your third for loop) looks fishy to me:

    Code:
    k >= (k - runThrough); k--
    
    It seems like k is always going to be larger or equal to itself - runThrough.
     
  11. jsmwoolf, Oct 22, 2011
    Last edited: Oct 22, 2011

    jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #11
    It never does. It's only positive any only effects the third nested first loop of the second loop. My intention was to subtract k from runThrough so that it can add the appropriate amount of zeros. (k-runThrough) when runThrough = 0 is only supposed to put one digit in there. Additional amount would add zeros until k-runThrough is equal to k.

    runThrough It's changed in the end of the second loop as the third nested first loop of the second loop is supposed to put a digit associated with the position of what digit in the first is being dealt with.

    Now that I think of it, you are right.

    I changed it to this instead: space-runThrough;

    Thanks it works, but I have some work to do on the algorithm. If I'm at a dead end, I'll post back.
     
  12. chown33, Oct 23, 2011
    Last edited: Oct 23, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    You didn't post your code that actually sets up the data and then calls multiplyNumbers(). That was point #2 in my first reply:
    Also post the code for this function:
    Code:
                    addSingleNumbers(space, initialSpace, toStore);
    
    In other words, post the complete compilable program you're running.


    If you expect others to explain your program to you, they should be able to run exactly the same program you're running. The reason for this should be obvious: they need to observe the actual program you're running, rather than having to guess what you're running.

    If you expect others to debug your program for you, the logical way for them to do that is to use the debugger. But without a complete runnable program, it's impossible to use the debugger.

    The only way for them to observe or debug the actual program you're running is for you to post the complete code of that program. This includes the main() function and any other files needed to run the program. We can't read your mind. We can't see your screen. We can't read files from your disk. If you don't post it, we can't see it.
     

Share This Page