0

I am trying to modify a value in an array using the C programming language and I seem to be hitting a blank wall with this seemingly easy operation. Please see code snippet below:

while(1) {
        printf("Current prime candidate is %i\n",nextPrimeCandidate);
        int innerSieve;//=2;
        int currentPrimeCandidate=0;

        for (innerSieve=2;innerSieve<SIEVELIMIT;innerSieve++) {
            currentPrimeCandidate = nextPrimeCandidate * innerSieve;
            //printf("Inner Sieve  is b4 funny place %i,%i\n",innerSieve,currentPrimeCandidate);

            //initArray[currentPrimeCandidate]=5;
            //VERY UNIQUE LINE
            myArray[currentPrimeCandidate] = 0;



            //printf("Inner Sieve after funny place is %i,%i \n",innerSieve,currentPrimeCandidate);

        }
        nextPrimeCandidate=getNextPrimeCandidate(myArray,++nextPrimeCandidate);
        if ((nextPrimeCandidate^2) > SIEVELIMIT ) break;

    }

The problem is with the line highlighted with the VERY UNIQUE LINE comment. For some reason, when the innerSieve variable reaches 33 and gets to that line, it sets the contents of the innerSieve variable to the value of that line ( which currently is 0) and basically forces the loop into an infinite loop( the SIEVELIMIT variable is set at 50). It seems that there is some funny stuff going on in the registers when I checked using the Eclipse Debug facility but I am not too sure what I should be looking for.

If you need the whole code listing, this can be provided.( with a particular variable which is not yet initialised in the code being initialised at the precise point that the innerSieve variable hits 32)

Any help will be greatly appreciated.

2
  • 2
    And what does if ((nextPrimeCandidate^2) > SIEVELIMIT ) break; means? Commented Nov 30, 2010 at 0:44
  • I apparently was trying to check if nextPrimeCandidate raised to the power of 2 was greater than SIEVELIMIT but after reading Adam Rosenfields answer below, I should really forget my JAVA experience when dealing with C Commented Nov 30, 2010 at 16:39

2 Answers 2

4

Guessing that currentPrimeCandidate is greater than the maximum index of myArray, and you're overwriting innerSieve (which likely follows myArray on the stack).

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, in your inner loop try limiting by SIEVE_LIMIT (that should be the actual length of the array!) the value of currentPrimeCandidate , and not of innerSieve .
4

@ruslik hit on it in the comment. The problem is this line:

if ((nextPrimeCandidate^2) > SIEVELIMIT ) break;

In C, the ^ operator is not the power operator, it is the bitwise xor operator. You're iterating far too many times than you intend, which is resulting in an array-index-out-of-bounds error, so you're overwriting random memory and getting strange results.

There is no power operator in C (though there is the pow function). Since you're just squaring a number, the simplest fix is to multiply the number by itself:

if ((nextPrimeCandidate * nextPrimeCandidate) > SIEVELIMIT ) break;

1 Comment

I actually did not know that C has no explicit power operator! I had actually fixed the problem with tomlogics answer but now that you have added this, I might need to go back and review what I did. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.