Well for one thing, this is problematic:
int sum_array(int num_array[]) {
int current_total = 0;
int *iptr = num_array;
while(*iptr) {
current_total += *iptr;
iptr++;
}
return current_total;
}
What this says is start at the beginning of the array you are given. While the value in a memory block the size of an int isn't zero, move to the next memory block. What happens here is it'll keep going past the end of your array, eventually messing with memory it shouldn't which is what causes the segfault. The reason it seems random is that sometimes the memory at the end of the array IS empty, and this'll work fine. But sometimes it's not, and then it accesses memory it shouldn't and CRASH.
That might not be the only problem but it was the first one I noticed. To fix it, keep track of the size of your array, pass it to the function, and then use a for loop to iterate up to it rather than using pointers. Like this:
int sum_array(int num_array[], int arraySize) {
int current_total = 0;
for(int i = 0; i < arraySize; i++) {
current_total += num_array[i];
}
return current_total;
}
I'll look again to see if there's anything else.
Edit:
On a second look, you do the same thing in two other places. Here:
while(*prime) {
cout << *prime << " ";
prime++;
}
And here:
while(*prime) {
*prime = 0;
prime++;
}
In both places I'd bet dollars to donuts you are overrunning your array and that's what's causing the segfaults. If you're new I'd strongly recommending against using pointer arithmetic to traverse your arrays. Stick to good old for loops and keep track of the end of the for loops.
Other folks have suggested allocating the array from the heap instead of the stack. That's a good idea for a huge array, however, in my experience at least, stack overruns don't usually cause segfaults. The compiler will usually notice when you allocate more space than is in the stack. Still I'd suggesting using a vector (for big extra credit on your homework see if you can figure out how to implement your own vector using a double pointer allocated as an array of pointers from the heap ;) ) or just use the std::vector. It's an expandable array and that will let you add primes to your array as you find them rather than allocating a whole ton of space that you don't necessarily need.