Take the 2-minute tour ×

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;
        globalPrimes = calloc(count>0? count:until, sizeof(int));
        return NULL;
    globalPrimes[0]=2;   //set first 2 prime #'s in list
    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
        else if(globalPrimes[i]==0)  //if it is 0, allocate the extra memory and find next prime
                globalPrimes=(numList)realloc((void *)globalPrimes, sizeof(int)*((count==0)? (until):(count+1)));

        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=(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.

share|improve this question
valgrind is your friend. –  David Schwartz Dec 5 '12 at 0:52
The debugger? Explain –  bathtub Dec 5 '12 at 0:54
What arguments are you passing to getPrimes? (I'm testing this) –  Xymostech Dec 5 '12 at 0:55
Yes, valgrind is a debugger that catches exactly these kinds of problems. –  David Schwartz Dec 5 '12 at 0:56
Any odd number that isn't one or negative as the first argument. Zero for the second argument. –  bathtub Dec 5 '12 at 0:56
show 4 more comments

1 Answer

up vote 1 down vote accepted

The line


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.

share|improve this answer
Thank you. I don't even know why I didn't notice that one. –  bathtub Dec 5 '12 at 2:37
I tried compiling and running the function under VS 2012 to see if the debug CRT would catch the corruption. I was surprised to see that it didn't (calling free() instead of realloc() does notice the corruption). I've opened a bug on connect.microsoft.com about that behavior - I think it's clear that the debug version of realloc() should have noticed the bad write. –  Michael Burr Dec 5 '12 at 3:11
add comment

Your Answer


