Segmentation fault in Linux but in Wine it works.

I wasn't sure what forum to put this in. Well, when I compiled my program in wine, and run it everything worked. But when I compiled it in Linux it the Fibonacci part of my program didn't work. Sometimes it would say nothing and sometimes it says "Segmentation fault" where the numbers were supposed to be.

Wine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
me@mycomputer:~/wine/bin/$ wine g++.exe /home/me/homework/ass4.cp -o /home/me/homework/ass4.exe
me@mycomputer:~/homework$ wine ass4.exe 

The highest common divisor of 43 and 5 is 1

The highest common divisor of 56 and 4 is 4

The highest common divisor of 15 and 5 is 5

The highest common divisor of 90 and 30 is 30

The highest common divisor of 8 and 2 is 2

First Twenty Fibonacci numbers
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 

Linux:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
me@mycomputer:~/homework$ g++ ass4.cp -o ass4.out
me@mycomputer:~/homework$ ./ass4.out 

The highest common divisor of 43 and 5 is 1

The highest common divisor of 56 and 4 is 4

The highest common divisor of 15 and 5 is 5

The highest common divisor of 90 and 30 is 30

The highest common divisor of 8 and 2 is 2

First Twenty Fibonacci numbers
Segmentation fault


Code:
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
121
122
123
124
125
126
127
128
129
130
131
132
133
/*****************************

 *  Made by me               *

 *  Assignment 4             *

 *  ece270                   *

 *****************************/



#include <iostream>

#include <fstream>

#include <string>

using namespace std;



int gcd(int,int);

int fib(int);



int main()

{

    string in_file = "a4.txt";

    string out_file = "a4-2.txt";

    

    ifstream in_data;

    ofstream out_data;

    

    // GREATEST COMMON DIVISOR

    in_data.open(in_file.c_str());

    while (in_data)

    {

          int numberA = 0;

          int numberB = 0;

          in_data >> numberA;

          in_data >> numberB;

          if ((numberA == 0) && (numberB == 0)) break;

          cout << "\nThe highest common divisor of "

               << numberA << " and " << numberB << " is "

               << gcd(numberA, numberB) << "\n";

    }

    in_data.close();
    cout << "\n";

    

    // FIBONACCI

    cout << "First Twenty Fibonacci numbers\n";

    out_data.open(out_file.c_str());

    for(int i; i<=19; i++)         // starts at zero...

    {

            cout << fib(i) <<" ";

            out_data << fib(i) << " ";

    }

    out_data.close();
    cout << "\n";

    

    return 0;

}



int fib(int n)

{

    if (n==0 || n==1)

       return n;

    else

        return(fib(n-1)+fib(n-2));

}



int gcd(int a, int b)

{

    if (b==0)

       return a;

    else

        return(gcd(b,a%b));

}

You should check that out_data.open() succeeded before writing to it.

Change line 86 to cout << fib(i) <<" " << flush; and see if the output changes.

No, I still get the same problem. It's ok though as long as it works in Dev C++. It's just odd that it works with Windows' version of gcc, but not Linux's version.
On line 82, what value do you intend "i" to take on the first and subsequent iterations?
In my linux, it works:

$ ./a.out

First Twenty Fibonacci numbers
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

and my version:

2.6.22-gentoo-r2-build-general-dl140
PanGalactic's post shows the problem.

@flykobe: you are just getting lucky that i is initialized to zero.
Topic archived. No new replies allowed.