Segfault in Primes Program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Segfault in Primes Program

 
0
  #1
Jun 13th, 2009
okay, so I wrote a program in Perl to find prime numbers and, when learning C, I decided to translate it into C. The thing is that...well...it doesn't work. No matter what I do, I get the same message when I say ./primes.c at the terminal: Segmentation fault. I know what this is, but I just can't put my finger on where it came from in my program. It would be much appreciated if anyone could figure this out for me, or help me to find it.
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int maxprimes;
  5. int count = 1;
  6. int value = 1;
  7. int primes[maxprimes];
  8. int composite;
  9. int i = 1;
  10. int j;
  11.  
  12. printf ("How many primes? ");
  13. scanf ("%d", &maxprimes);
  14.  
  15. printf ("2 is prime\n");
  16. primes[0] = 2;
  17. while (count < maxprimes){
  18. composite = 0;
  19. value += 2;
  20. for (j = 1 ; j < maxprimes ; j++) {
  21. if (value % primes[j] == 0) {
  22. composite = 1;
  23. break;
  24. }
  25. }
  26. if (composite = 0){
  27. printf ("%d is prime", value);
  28. primes[j] = value;
  29. count += 1;
  30. j++;
  31. }
  32. }
  33. }
To explain the code a little, it's based on a Sieve of Eratosthenes algorithm, that's what the array is for. It's also probably the source of the segfault (the array, that is)
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Segfault in Primes Program

 
0
  #2
Jun 13th, 2009
C runs on some rules,you need to learn them before trying out something,and ya you cannot just translate a code to C and expect it to work.
  1. int maxprimes;
  2. int count = 1;
  3. int value = 1;
  4. int primes[maxprimes];
  5. int composite;
  6. int i = 1;
  7. int j;
  8. printf ("How many primes? ");
  9. scanf ("%d", &maxprimes);
This is wrong. You are trying to take the value of maxprimes from the user and trying to allocate that much memory for the array primes. But as you can see compiler would have already allocated memory for that array and at that time
  1. int primes[maxprimes];
doesn't have any meaning,so the segmentation fault.

What you can do is to take the value of maxprimes and then allocate memory for array,but in C
  1. int maxprimes;
  2. int count = 1;
  3. int value = 1;
  4. int composite;
  5. int i = 1;
  6. int j;
  7. printf ("How many primes? ");
  8. scanf ("%d", &maxprimes);
  9. int primes[maxprimes];
something like this is not allowed.Because in C all data variable's should be declared and initialized before the first operational statement(say an assignment or a check.).Its memory also needs to be allocated priorly if you are allocating it statically.

Now coming to the solution : You need to use dynamic allocation of memory Do this :
  1. //Within main function
  2. int maxprimes;
  3. int count = 1;
  4. int value = 1;
  5. int *primes;
  6. int composite;
  7. int i = 1;
  8. int j;
  9.  
  10. printf ("How many primes? ");
  11. scanf ("%d", &maxprimes);
  12.  
  13. //your solution is this statement
  14. primes = (int *)malloc(maxprimes * sizeof(int));
And ya I haven't checked your algorithm "Sieve of Eratosthenes" hope you haven't done any mistake in it. !!!
Last edited by csurfer; Jun 13th, 2009 at 11:19 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Re: Segfault in Primes Program

 
0
  #3
Jun 13th, 2009
Thanks for the solution, but now it won't compile. Doesn't int *primes have to be an array?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Segfault in Primes Program

 
0
  #4
Jun 14th, 2009
Arrays and pointer are closely related in C. But first tell me what book are you using?
Anyways, one advice: your scripting languages like Python and Perl makes you feel that programming is fun, this is not the case with C. I am sure you will have to do quite low level things in C. Know that C is statically typed.

If you are just beginning to C, don't use the above said solution. It will tend to confuse you.
You must know that you must explicitly state the size of an array which should be a CONSTANT. Thats the most important luxury you miss when coming from perl. In C we have fixed sized array. ( we too have hacks for dynamically sized array but that would be covered later).
So, the solution would seem like this:
  1. #include <stdio.h>
  2. /*The maximum prime that the Program supports*/
  3. #define MAXPRIMES 100
  4. int main() {
  5. int max;
  6. int count = 1;
  7. int value = 1;
  8. /*note below, here I passed a constant value,100 as size*/
  9. int primes[MAXPRIMES];
  10. int composite;
  11. int i = 1;
  12. int j;
  13. /*I warned the user that maximum primes supported by this program*/
  14. printf ("How many primes?(Maximum %i)",MAXPRIMES);
  15. scanf ("%d", &max);
  16.  
  17.  
  18. /*I am too tired to check your progarm logic.
  19.   As you are a programmer, SHould I trust you?
  20.   Anyways, if you can make it run, get us back*/
  21. printf ("2 is prime\n");
  22. primes[0] = 2;
  23. while (count < maxprimes){
  24. composite = 0;
  25. value += 2;
  26. for (j = 1 ; j < maxprimes ; j++) {
  27. if (value % primes[j] == 0) {
  28. composite = 1;
  29. break;
  30. }
  31. }
  32. if (composite = 0){
  33. printf ("%d is prime", value);
  34. primes[j] = value;
  35. count += 1;
  36. j++;
  37. }
  38. }
  39.  
  40. }
Last edited by siddhant3s; Jun 14th, 2009 at 12:44 am.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Re: Segfault in Primes Program

 
0
  #5
Jun 14th, 2009
I get a floating point exception now. I'll revisit this when I learn about memory allocation...by the way, I'm using the massive book called the Internet
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Segfault in Primes Program

 
0
  #6
Jun 14th, 2009
It is really when people say : `` I am using Internet as a book". Internet is no book. Internet can be as closely be thought as network of books. Nothing more.
A book is a book. Don't wander much here and there. Stick to one resource for learning. (though you may search as much as you like for a particular reference)

Another thing is that : When you say ``I have a error", do mention the line number where the compiler complaints.
Anyways, do have a look at :http://www.dreamincode.net/code/snippet528.htm
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Segfault in Primes Program

 
0
  #7
Jun 15th, 2009
If max is the number of primes ,you originally want then why are you looping to maxprimes (or is it MAXPRIMES)
( @siddhant3s :
if you can make it run, get us back.
)
  1. //while (count < maxprimes)
  2. //shouldn't it be
  3. (count < max)

But even after warning the user that the maximum limit is MAXPRIMES ,no checks are done whether actually the value entered
by scanf is within range or not.

How about this line , something's missing,no?
  1. if (composite = 0)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Segfault in Primes Program

 
0
  #8
Jun 15th, 2009
@zalezog
The code below line 21 has not been touched by me.

>If max is the number of primes ,you originally want then why are you looping to
>maxprimes
Because the OP had maxprimes as his variable. I changed it to max for simplicity. It was the OP's responsibility to convert each occurrence of maxprime to max.

>shouldn't it be .....
Yes it should, but thats OPs job, why are you getting dirty in a dirty code.

Rest of all your suggestion are for (the benefit of ) OP
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Re: Segfault in Primes Program

 
0
  #9
Jun 15th, 2009
Thanks for the other error catch. Like I said, I'm going to wait to come back to this problem when I know about memory allocation. If I have to limit the amount of primes that I can find, why make it in the first place?
By the way, about having no checks for it, I was getting to that. Once the code compiled, I would put the checks in there.
Also, I didn't give the compiler message and line # because, as I've mentioned twice already, I realize that I have a lot more to learn before attempting this again.
Last edited by orwell84; Jun 15th, 2009 at 3:36 pm. Reason: last two sentences
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC