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

  • Home
  • SEARCH
  • 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 6733187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:44:21+00:00 2026-05-26T10:44:21+00:00

I read up on the sieve of Eratosthenes while solving a question on Project

  • 0

I read up on the sieve of Eratosthenes while solving a question on Project Euler. I’m sure you guys know which question im talking about.
So here’s the thing. My code manages to show all the primes under 1 million correctly.
However when i try the same implementation for 2 million it’s giving me a segmentation fault…
I have a certain idea of why the error is coming but don’t know how to correct it…
Here’s the code for primes under 1 million.

#include<stdio.h>
int main(void)
{
   int i,k=2;
   int j;
   int n=1000000;
   int prime[2000000]={};
   for(i=0;i<n;i++) // initializes the prime number array
   {
      prime[i]=i;
   }
   for(i=2;i<n;i++) // Implementation of the Sieve
   {
      if(prime[i]!=0)
      { 
         for(j=2;j<n;j++)
         {
            {
               prime[j*prime[i]]=0;
               if(prime[i]*j>n)
                  break;    
            }
         }
      }
   }
   for(i=0;i<n;i++) // Prints the prime numbers
      if(prime[i]!=0)
      {
         printf("%d\n"prime[i]);
      }
      return(0);
   }
}
  • 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-05-26T10:44:21+00:00Added an answer on May 26, 2026 at 10:44 am

    You’re allocating a huge array in stack:

    int prime[2000000]={};
    

    Four bytes times two million equals eight megabytes, which is often the maximum stack size. Allocating more than that results in segmentation fault.

    You should allocate the array in heap, instead:

    int *prime;
    prime = malloc(2000000 * sizeof(int));
    if(!prime) {
        /* not enough memory */
    }
    /* ... use prime ... */
    free(prime);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Read about Server push here . I want to push data to client from
Read about the issue in this stackoverflow question . Still have the same issue
I have read about Sieve of Atkin on Wikipedia but the wiki is limited
Today I read a paper: O'Neill, Melissa E., The Genuine Sieve of Eratosthenes, Journal
When verbally talking about methods, I'm never sure whether to use the word argument
I read an article about a regular expression to detect base64 but when I
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
read a lot about DataAdapter, DataTable ,.. to reach to this code, in the
I'm trying to implement sieve of erathostenes for school project and I've decided to
Read a question on stack overflow sometime back with the following syntax In [1]:

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.