0

My realloc statement in the second function works up until a point, then the pointer seems to point to random memory all of a sudden. Could someone please explain how I could fix this issue? Take a look at the output to possibly save yourself some time. Thank you.

int main()
{

    int testCases, i, n;
    int* primeArray;
    int* size;

    primeArray = malloc(sizeof(int));
    primeArray[0] = 2;
    size = 1;

    int number = 2;
    while(number < 1000){
        number = nextPrime(number, primeArray, &size);
        printf("Prime Array at %d is %d, size is %d, number is %d \n", 0, primeArray[0], size, number);
    }


    scanf("%d", &testCases);
    for(i = 0; i < testCases; i++){
        scanf("%d", n);

    }

    free(primeArray);
    free(size);
    return 0;
}

Second Function:

int nextPrime(int number, int* primeArray, int* size){
    int foundPrime = 0, num = number, i;
    while(!foundPrime){
        num++;
        int allNums = 0;
        //printf("Size: %d \n", *size);
        for(i = 0; i < *size; i++){
            //printf("%d mod %d \n", num, primeArray[i]);
            if(num % primeArray[i] != 0){
                allNums += 0;
            }
            else {
                allNums = 1;
                break;
            }
        }
        if(allNums == 0){
            *size+=1;
            //printf("Size: %d \n", *size);
            foundPrime = 1;
            primeArray = realloc(primeArray, *size * sizeof(int) );
            primeArray[*size-1] = num;
            //printf("%d \n", primeArray[*size-1]);
            return num;
        }
    }
}

Output:

Prime Array at 0 is 2, size is 2, number is 3  
Prime Array at 0 is 2, size is 3, number is 5  
Prime Array at 0 is 2, size is 4, number is 7  
...  
Prime Array at 0 is 2, size is 94, number is 491  
Prime Array at 0 is 2, size is 95, number is 499  
Prime Array at 0 is 2, size is 96, number is 503  
Prime Array at 0 is 16852008, size is 97, number is 509
2
  • 1
    Certainly int* size;... size = 1; generated a compiler warning. Compile again with all warnings enabled. Commented Jan 23, 2017 at 4:56
  • Off topic - your loop in nextPrime could be made much faster if you skipped all the even numbers. That is increment, num by 2 instead of num++ on each iteration. You can also terminate your loop early when num > sqrt(primeArray[last] Commented Jan 23, 2017 at 5:21

2 Answers 2

1

primeArray, even though it's a pointer, is getting passed by value to your nextPrime function. So if realloc changes the pointer value (as it can and often will), main doesn't get that value reflected back to it when nextPrime returns. A quick fix will be to change your nextPrime to take a pointer to a pointer parameter instead of just an array pointer.

Here's a quick fix where I modified the function signature of nextPrime and added code to the start and and end of the function.

int nextPrime(int number, int** ptrToPrimeArray, int* size){

    int* primeArray = *ptrToPrimeArray;  // primeArray is the deferenced value of ptrToPrimeArray

    int foundPrime = 0, num = number, i;
    while(!foundPrime){
        num++;
        int allNums = 0;
        //printf("Size: %d \n", *size);
        for(i = 0; i < *size; i++){
            //printf("%d mod %d \n", num, primeArray[i]);
            if(num % primeArray[i] != 0){
                allNums += 0;
            }
            else {
                allNums = 1;
                break;
            }
        }
        if(allNums == 0){
            *size+=1;
            //printf("Size: %d \n", *size);
            foundPrime = 1;
            primeArray = realloc(primeArray, *size * sizeof(int) );
            primeArray[*size-1] = num;
            //printf("%d \n", primeArray[*size-1]);

            *ptrToPrimeArray = primeArray;   // return the changed value of primeArray back to the caller

            return num;
        }
    }
}

And then invoke it in main as follows:

number = nextPrime(number, &primeArray, &size);
Sign up to request clarification or add additional context in comments.

Comments

1

You are assigning the value returned by realloc() to a local variable, which is of course gone when the function nextPrime() returns. The value of primeArray in main() never changes.

Comments

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.