Segmentation Fault on Different machines

When I run this code on my Windows machine, it works fine.

When I ssh into a redhat linux machine to run the code, it returns a segmentation fault.

Is there a reason the code would seg fault on a different OS?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include <sstream>
#include<iomanip>

using namespace std;

int random_number(){
  int i = 1 + (rand() % 202);
  return i;
}

class isprime {
  public:
    isprime() { /* initialize our vector to have single element of 2*/ 
		plist.push_back(2);
	}

    bool operator()(int);
	bool primeChecker(int);
  private:
	void expand_plist(int);
    vector<int>plist;
	

};



bool isprime::operator()(int number)
{
    //debugging
	/*for(long unsigned int i = 0; i < plist.size(); i++){
		cout <<"                     "<< "s" << plist.at(i) << "s" <<  endl;
	}*/

	
	vector<int>::iterator it = find(plist.begin(), plist.end(), number);
	if(it == plist.end()){
		expand_plist(number);
	}

	if(it != plist.end()){return true;}
	

	else if(primeChecker(number) == false){
		return false;
	}

	else if(primeChecker(number) == true){
		return true;
	}

}

bool isprime::primeChecker(int number){
	if(number < 2){
		return false;
	}
	if(number == 2){
		return true;
	}
	else{
		int root = ceil(sqrt(number)); 
		for(int i = 2; i <= root; i++){ 
			if((number % i) == 0){			
				return false;
			}
		}
	}
	return true;
}


void isprime::expand_plist(int number)
{
	if(primeChecker(number)){
		plist.push_back(number);
	}	
}

int main(int argc, char *argv[])
{
  isprime pcheck;
  int N = atoi(argv[1]);

  if(!argv[1]){
	  N = 202;
    }

  srand(N);

  vector<bool>boolList(N);
  vector<int>numList(N);

  generate(numList.begin(), numList.end(), random_number);
  //debugging
  int num1 = 0;
  for(long unsigned int i = 0; i < numList.size(); i++){
    cout << numList.at(i) << setw(5) << num1 << endl;
    num1 = num1 + 1;
  }
  
  transform(numList.begin(), numList.end(), boolList.begin(), pcheck);
  //debugging
  int num = 0;
  for(long unsigned int i = 0; i < boolList.size(); i++){
    cout << boolList.at(i) << setw(5) << num << endl;
    num = num + 1;
  }

  int c = count(boolList.begin(), boolList.end(), true);
    
  cout << "Sequence contains " << c << " prime number(s)." << endl;
}
Last edited on
> Is there a reason the code would seg fault on a different OS?
Yes, your program has bugs.

1
2
3
4
5
 int N = atoi(argv[1]);

  if(!argv[1]){
	  N = 202;
    }

You should check argc before trying to access any argv variable.

There is no point checking after you've done a conversion.

When I run this code on my Windows machine, it works fine.

When I ssh into a redhat linux machine to run the code, it returns a segmentation fault.
Commit this to memory. Tattoo it on the inside of your eyelids: Programs work by accident.

In your case, the redhat version is crashing because the code is buggy. The Windows version works entirely by accident. It's a happy coincidence that it doesn't crash on windows.

Whenever a program works in one environment and doesn't in another, the problem is usually a bug, not a problem with the one environment.
the worst thing that can happen is when code with a massive flaw appears to work correctly.
in the basic case of buffer overruns, for example ...
say you have an array variable followed by an integer. If you overflow the array, the integer changes value, probably to something valid but incorrect that may be hard to see when testing that it was incorrect. Everything works fine, sort of, except you have a corrupt value -- if that value is not used for anything that crashes or vital data that the tester will note the bad data, all appears well.... and then your customers have bad data for years until someone notices it and they realize they all owe the IRS another billion bucks and its YOUR fault :)
Last edited on
Topic archived. No new replies allowed.