Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jsmwoolf

macrumors regular
Original poster
Aug 17, 2011
123
0
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);
...
 
Last edited:
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.
 
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.
 
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
Current language: auto; currently minimal
which probably does no good.
 
Xcode 4.2 is the development environment.

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.
 
It appears that k is way out of range which is probably the culprit of my problem.
space int 6
number int * 0x7fff5fbff8f0
number2 int * 0x7fff5fbff8d0
toStore int * 0x7fff5fbff8b0
i int 5
j int 5
k int -2096645
runThrough int 0
k int 0
x int 6
It should be between (space - 1) and 0, not -2096645. Why is it -2096645?
 
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
 
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.
 
When does runThrough go negative? Ever?
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.

Is runThrough changed in that loop? If not, the loop will run forever (or until you crash).
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.

It seems like k is always going to be larger or equal to itself - runThrough.
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.
 
Last edited:
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?

You didn't post your code that actually sets up the data and then calls multiplyNumbers(). That was point #2 in my first reply:
2. Post the input that causes the crash, i.e. show the actual input data and actual call to multiplyNumbers().
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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.