Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:19:32+00:00 2026-06-15T10:19:32+00:00

I have a prime number generator, which generates unsigned int count prime numbers and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:19:33+00:00Added an answer on June 15, 2026 at 10:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of prime numbers from 2 to 997. How do you
I have a python class that generates the nth prime number by starting at
All, I have implemented a code that generates 2 random prime numbers and the
I have seen that a prime number implementation of the GetHashCode function is being
I have a function it returns prime factors of a number but when I
I have a function is_prime(n) which returns True if n is prime and False
I have a simple spinner which has several number options. I want to just
How will you test if the random number generator is generating actual random numbers?
I have a basic program in C++ which lists a given number of primes.
I have a set of prime numbers and I have to generate integers using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.