Search code examples
cmemoryrealloc

Realloc pointer points to random memory, crashes program


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

Solution

  • 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);