| | |
Segfault in Primes Program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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)
c Syntax (Toggle Plain Text)
#include <stdio.h> int main() { int maxprimes; int count = 1; int value = 1; int primes[maxprimes]; int composite; int i = 1; int j; printf ("How many primes? "); scanf ("%d", &maxprimes); printf ("2 is prime\n"); primes[0] = 2; while (count < maxprimes){ composite = 0; value += 2; for (j = 1 ; j < maxprimes ; j++) { if (value % primes[j] == 0) { composite = 1; break; } } if (composite = 0){ printf ("%d is prime", value); primes[j] = value; count += 1; j++; } } }
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.
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
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
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 :
And ya I haven't checked your algorithm "Sieve of Eratosthenes" hope you haven't done any mistake in it.
!!!
c Syntax (Toggle Plain Text)
int maxprimes; int count = 1; int value = 1; int primes[maxprimes]; int composite; int i = 1; int j; printf ("How many primes? "); scanf ("%d", &maxprimes);
c Syntax (Toggle Plain Text)
int primes[maxprimes];
What you can do is to take the value of maxprimes and then allocate memory for array,but in C
c Syntax (Toggle Plain Text)
int maxprimes; int count = 1; int value = 1; int composite; int i = 1; int j; printf ("How many primes? "); scanf ("%d", &maxprimes); int primes[maxprimes];
Now coming to the solution : You need to use dynamic allocation of memory Do this :
c Syntax (Toggle Plain Text)
//Within main function int maxprimes; int count = 1; int value = 1; int *primes; int composite; int i = 1; int j; printf ("How many primes? "); scanf ("%d", &maxprimes); //your solution is this statement primes = (int *)malloc(maxprimes * sizeof(int));
!!! Last edited by csurfer; Jun 13th, 2009 at 11:19 pm.
I Surf in "C"....
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:
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:
c Syntax (Toggle Plain Text)
#include <stdio.h> /*The maximum prime that the Program supports*/ #define MAXPRIMES 100 int main() { int max; int count = 1; int value = 1; /*note below, here I passed a constant value,100 as size*/ int primes[MAXPRIMES]; int composite; int i = 1; int j; /*I warned the user that maximum primes supported by this program*/ printf ("How many primes?(Maximum %i)",MAXPRIMES); scanf ("%d", &max); /*I am too tired to check your progarm logic. As you are a programmer, SHould I trust you? Anyways, if you can make it run, get us back*/ printf ("2 is prime\n"); primes[0] = 2; while (count < maxprimes){ composite = 0; value += 2; for (j = 1 ; j < maxprimes ; j++) { if (value % primes[j] == 0) { composite = 1; break; } } if (composite = 0){ printf ("%d is prime", value); primes[j] = value; count += 1; j++; } } }
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
(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
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
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
(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
•
•
Join Date: Oct 2008
Posts: 45
Reputation:
Solved Threads: 11
If max is the number of primes ,you originally want then why are you looping to maxprimes (or is it MAXPRIMES)
( @siddhant3s : )
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?
( @siddhant3s :
•
•
•
•
if you can make it run, get us back.
C Syntax (Toggle Plain Text)
//while (count < maxprimes) //shouldn't it be (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?
C Syntax (Toggle Plain Text)
if (composite = 0)
@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
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
(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
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.
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
![]() |
Similar Threads
- I have a seg fault (can't find new thread button) (C)
- primes program (C)
- Compiling/Linking problem (C)
- newbie here, help requested, prime number finder using factoring (C++)
- Weird segfault involving popen (C)
Other Threads in the C Forum
- Previous Thread: read data from file, then add into tree
- Next Thread: How to index the void pointer buffer in VC++?
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi





