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 8710167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:33:20+00:00 2026-06-13T04:33:20+00:00

The purpose of my program is to find the prime indicated by user input.

  • 0

The purpose of my program is to find the prime indicated by user input. I set up an array to store primes as it finds them. As p (the integer under test) increments and the tests restart, processing is saved by only dividing by the elements within the array. It works great up until the 44030th prime. I’m using GCC to compile. Why’s it giving me a segmentatio fault?

 //Prime Finder
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i=4;
int p=7;
int j=1;
int cap;
printf("\nWhich prime number would you like to see? ");
scanf("%i",&cap);
long *array=malloc(cap);
array[0]=1;
array[1]=2;
array[2]=3;
array[3]=5;
while(i<=cap)
{
if (array[j]>=p/2) // if true then p is prime
{
    j=1;
    array[i]=p;
    p++;
    i++;
}
else if (p%array[j]==0) // if true then p is not prime
{
    p++;
    j=1;
}
else // in this case p is still under test
    j++;
}
printf("\nHere it is! %i\n\n",array[cap]);
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-06-13T04:33:21+00:00Added an answer on June 13, 2026 at 4:33 am

    You are allocating cap bytes rather than cap*sizeof(long) bytes in your malloc() call. So you are overwriting other parts of memory; exactly when depends on what value you provide to cap.

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

Sidebar

Related Questions

I've written several variations of a program. The purpose of the program is to
I have a general purpose Utility dll file used in my WPF program. The
I have an OCR program, written with C# (WinForms app). So it's main purpose
I try to call the esriRegAsm.exe with arguments from a C# program. The purpose
In my PyGTK application, I am asking a user to find a file so
Purpose I am writing a network program in C (specifically gnu89 ) and I
The purpose of the code is to find a portion of a string (from
I am working on a program which includes a general purpose engine, some program
I have created a matlab program to find word bigrams and their frequencies in
My program that I am writing's purpose arose with this issue: There are two

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.