Search code examples
cnetbeansrealloc

strange realloc behavior odd/even netbeans


I have a prime number generator, which generates unsigned int count prime numbers and stores them in a dynamically allocated array declared globally numList globalPrimes numList is just typedef unsigned int *numList. If the value of count is 0, it is ignored and it calculates prime numbers until the value of the previously calulated prime number surpasses unsigned int until which is ignored unless count is 0.

numList getPrimes(unsigned int count, unsigned int until)
{
    unsigned int i, j;
    unsigned int p = 0;
    char b=1;
    if(globalPrimes==NULL)
    {
        b=0;
        globalPrimes = calloc(count>0? count:until, sizeof(int));
        if(globalPrimes==NULL)
        return NULL;
    }
    globalPrimes[0]=2;   //set first 2 prime #'s in list
    globalPrimes[1]=3;
    for(i=2, p=5; count!=0? (i<count):(globalPrimes[i-1]<=until); p+=2)  //iterate until it finds every prime number. Increments p by 2 starting from the third prime
    {
        if(globalPrimes[i]!=0)  //if the current prime was preordained (a value was set already) skip to the next prime
        {
            p=globalPrimes[i++];
            continue;
        }
        else if(globalPrimes[i]==0)  //if it is 0, allocate the extra memory and find next prime
        {
            if(b)
                globalPrimes=(numList)realloc((void *)globalPrimes, sizeof(int)*((count==0)? (until):(count+1)));
            b=0;
        }

        for(j=0; (p%globalPrimes[j]) && globalPrimes[j]*globalPrimes[j]<p; j++);  //loop through all previous primes until past half of p
        if(p%globalPrimes[j])   //if the prime is still not divisible by the previous prime
            globalPrimes[i++]=p;   // save as the next prime
    }
    globalPrimes[i]=0;
    globalPrimes=(numList)realloc((void *)globalPrimes, (i+(i%2)+1)*sizeof(int));
    return globalPrimes;
}

During some tests, I found a weird error. On the penultimate line with realloc netBeans (gcc compiler) gives me a "signal" which I assume are like runtime exceptions. The dialogue box reads that the error is SIGABRT, and it aborts the program while not in debugging mode. I have found that this error only occurs when count is an odd number. Even when I modify realloc's arguments so that it always passes even numbers, there is still an error. But when I modify count to only be even at the beginning of the function, it works fine. I can't figure out why this strange details cause this strange behavior.


Solution

  • The line

    globalPrimes[i]=0;
    

    just before your last realloc() is corrupting memory - it's a write that's just past the end of the array you've allocated.

    I think you want to allocate the array like so:

    globalPrimes = calloc(count>0? (count + 1):until, sizeof(int));
    

    So far I've only looked at the case where the function is called with a non-zero count; I'm honestly not sure what problems there might be if the function is called with count == 0 and until being some target prime.