Compiler & Variable problems

So currently I am having issues with two things at the moment.

While working on a program for one of my CS classes I used my macbook to compile and run the program. For some reason when I run the program, it won't display the correct answer. But, when I compile and run the program on VM Ubuntu 14.04 it shows the correct answer. (Using Kadane's Algorithm to find the maximum subarray)

Now for the second issue, while using my mac to compile I can declare my variables as such; int max, sum, temp = 0; and it'll run smoothly. When I use Ubuntu to compile, it'll compile the program but when I run it, it'll give me a segmentation fault unless I change variables as such; int max = 0; int sum = 0; int temp = 0;

Anyone know why this is happening?
Probably just the way the operating systems juggle values around varies a bit. However, if you post the code, people might be able to help a little easier.

Don't forget to use code tags. :)

Edit: What compilers on each? Is it Clang on Mac and g++ on Ubuntu? It sounds like it could possibly be an issue with compilers and uninitalized variables. Whenever, a variable is uninitialized, upon compiling the compiler is free to interpret that however it wants without violating the ANSI standards. This could be anything from setting them safely to 0 or having demons fly out of your nasal passages. Of course, if your code has no uninitialized variables, my theory goes out the window... or maybe I just wanted an excuse to talk about nasal demons.
Last edited on
The two formulas are located at the bottom; maxSubsequenceSumSlow and Fast

Also once you run the program; ./a.out -s -l 1000
The example gives the answer; 11687 and Ubuntu also gives the answer 11687.
But when I run it on my mac, it gives 16165

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;

int maxSubsequenceSumSlow (int [], int, int &, int &);
int maxSubsequenceSumFast (int [], int, int &, int &);


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

int main(int argc, char *argv[])
{
	enum	algorithmOption {SLOW, FAST, PRINT, TEST, NONE};

	string	stars;
	stars.append(60,'*');
	stringstream ss;

	int	ans1, start1, stop1;
	int	ans2, start2, stop2;
	int	length = 0;

	int	arrTest[] = {-2, 11, -4, 13, -5, 2};
	int	lenTest = sizeof(arrTest) / sizeof(arrTest[0]);

	algorithmOption	userChoice = NONE;

// ------------------------------
//  Verify command line arguments.
//	Error out if bad, select user selection if good.

	if (argc == 1) {
		cout << "Usage: maxSeq <-t|-s|-f|-p> -l <length>" << endl;
		return 0;
	}

	if (argc < 2 || argc > 4 || argc == 3) {
		cout << "Error, invalid command line options." << endl;
		return	0;
	}

	if (string(argv[1]) == "-t")
		userChoice = TEST;

	if (string(argv[1]) == "-s")
		userChoice = SLOW;

	if (string(argv[1]) == "-f")
		userChoice = FAST;

	if (string(argv[1]) == "-p")
		userChoice = PRINT;

	if (userChoice == NONE) {
		cout << "Error, invalid command line options." << endl;
		return	0;
	}

	if (userChoice != TEST) {

		if (argc == 2) {
			cout << "Error, must provide length." << endl;
			return	0;
		}

		if (string(argv[2]) != "-l") {
			cout << "Error, invalid length specifier." << endl;
			return	0;
		}

		if (string(argv[3]) != "") {
			ss << argv[3];
			ss >> length;
			if (length < 5 || length > 1000000) {
				cout << "Error, invalid length." << endl;
				return	0;
			}
		}
	}

// ------------------------------
//  Command line args ok, display initial headers.

	cout << stars << endl;
	cout << "CS 302 - Assignment #1" << endl;
	cout << "Maximum Contiguous Subsequence Sum Problem." << endl;
	cout << endl << endl;

// ------------------------------
//  Generate random values.
//	dynamically creates array of required size.

	int * myArr = new int[length];

	for (int i=0; i<length; i++)
		myArr[i] = (rand() % 1000) - 500;

// ------------------------------
//  Select appropriate option.

	switch (userChoice) {

		case PRINT:
			if (length < 1000) {
				for (int i=0; i<length; i++)
					cout << myArr[i] << " ";
				cout << endl;
				cout << endl;
			} else {
				cout << "Umm, I don't think so..." << endl;
			}

			break;

		case TEST:

			ans1 = maxSubsequenceSumSlow(arrTest, lenTest, start1, stop1);

			cout << endl;
			cout << "Max Sum 1 = " << ans1 << endl;
			cout << "Start 1 = " << start1 <<
				"   Stop 1 = " << stop1 << endl;
			cout << "Sequence 1 = ";
			for (int i=start1; i<=stop1; i++)
				cout << arrTest[i] << " ";
			cout << endl;

			ans2 = maxSubsequenceSumFast(arrTest, lenTest, start2, stop2);

			cout << endl;
			cout << "Max Sum 2 = " << ans2 << endl;
			cout << "Start 2 = " << start2 <<
				"   Stop 2 = " << stop2 << endl;
			cout << "Sequence 2 = ";
			for (int i=start2; i<=stop2; i++)
				cout << arrTest[i] << " ";
			cout << endl;

			break;

		case SLOW:

			ans1 = maxSubsequenceSumSlow(myArr, length,
							start1, stop1);

			cout << endl;
			cout << "Max Sum = " << ans1 << endl;
			cout << "Start = " << start1 <<
				"   Stop = " << stop1 << endl;
			cout << "Sequence = ";
			for (int i=start1; i<=stop1; i++)
				cout << myArr[i] << " ";
			cout << endl;

			break;

		case FAST:

			ans1 = maxSubsequenceSumFast(myArr, length,
							start1, stop1);

			cout << endl;
			cout << "Max Sum = " << ans1 << endl;
			cout << "Start = " << start1 <<
				"   Stop = " << stop1 << endl;
			cout << "Sequence = ";
			for (int i=start1; i<=stop1; i++)
				cout << myArr[i] << " ";
			cout << endl;

			break;

		case NONE:
			break;
	}

// ------------------------------
//  Done, terminate program.

	cout << endl;
	cout << "Game over, thanks for playing." << endl;

}

// ****************************************************************
//  Cubic maximum contiguous subsequence sum algorithm.
//	Assignment algorithm 1
//	seqstart and seqEnd represent the actual best sequence.

int maxSubsequenceSumFast(int myArr[], int length, int& start, int& stop)
{
    int sum = 0;
    int max = 0;

	for(int i=0; i < length; i++)
		for(int j=i; j < length; j++)
		{
			sum = 0;
			for(int k=i; k <= j; k++)
				sum += myArr[k];
			if(sum > max)
				{
					max = sum;
					start = i;
					stop = j;
				}
		}
	return max;
}


// ****************************************************************
//  Linear maximum contiguous subsequence sum algorithm.
//	Assignment algorithm 2
//	seqStart and seqEnd represent the actual best sequence.


int maxSubsequenceSumSlow(int myArr[], int length, int& start, int& stop)
{
	int max = 0;
	int sum = 0;
	int temp = 0;

	for(int j=0; j < length; j++)
	{
		sum += myArr[j];
		if(sum < 0)
		{
			sum = 0;
			temp = j + 1;
		}
		if(max < sum)
		{
			max = sum;
			start = temp;
			stop = j;
		}
		else if(max < 0)
			max = 0;
	}
	return max;
}
Last edited on
On the mac I'm using terminal and while using the g++ command it does show up as 'clang' and on Ubuntu it's the normal g++.

What do you mean uninitialized variables?
Hmmm... I'm not sure about the first issue with the answers being different. However, I am pretty sure about the second one.

Whenever you say int a, b, c = 0;, only c gets set to 0. The other two are uninitialized which means that have no predictable and useable value. The compiler usually warns you, but the runtime environment absolutely hates this because it doesn't know what to do. Whenever you tell it to do a calculation with a or b, it won't know how because they have no value. It is always best to initialize all of your variables to 0 manually.

In fact, this is a long shot and I'm just pissing in the wind here, but make sure all of your variables are initialized and see if that fixes the first issue too. :P
Okay. Is it proper etiquette to initialize all your data; int a = 0; int b = 0; int c = 0;? If so, then I'll just start doing it that way.

EDIT: Alright, i'm in the process of changing the way my mac links when using g++. Currently changing it to gcc49.

EDIT2: Changed it from clang to cc1plus by installing gcc49 via Homebrew and it still doesn't show up correctly.
Last edited on
Is it proper etiquette to initialize all your data; int a = 0; int b = 0; int c = 0;?

Yes. That is correct. You could even do this: int a = 0, b = 0, c = 0;

As for your compiler, I would go back to clang. Clang is a very good compiler. I am really not certain what your problem is to make the Mac compute a different value. Did you change all of your code to have initialized variables to make sure that that is not the problem?
The fact that you're getting the right answer on Ubuntu is purely coincidental. The fact is that when you use an uninitialized variable, the behavior is undefined. To put it another way, on Ubuntu, the program works by accident.
Topic archived. No new replies allowed.