Hi guys:

I wrote this simple code to read a series of numbers from a list and then do some simple aremathic with it. I am not certain where the error could be, I read the code upside down and still can understand where the error lies. Any help would be greatly appreciated.
Gus

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int averageResult(int average, int imageCount)
{
	ifstream in("objv.sky");
	ofstream out("objv.skyc");
	string image;
	int npix;
	double mean, stddev, min,forSum,sum,avgPct;
	vector<double> max (imageCount);
	int subtractionNumber[imageCount];
	getline(in, image); //to skip header
	for (int i=0;i<imageCount;i++) 
	{
		if (in >> image >> npix >> mean >> stddev >> min >> max[i])
		{
			subtractionNumber[i] = average - max[i];
			return(subtractionNumber[i]);
		}
	}
}


int main()
{
	ifstream in("objv.sky");
	ofstream out("objv.skyc");
	string image;
	int npix,imageCount;
	double mean, stddev, min,forSum,sum,average,avgPct;
	vector<double> max (imageCount);
	vector<double> imageDiff (imageCount);
	getline(in, image); //to skip the header
	cout << "How many images in the list: ";
	cin >> imageCount;
	for(int i=0;i<imageCount;i++)
	{
		average=0;
		if(in >> image >> npix >> mean>> stddev >> min >> max[i])
			{
				cout <<"["<<i+1<<"] "<< max[i] << "\n";
				sum += max[i];
			}
	}
	average = sum/imageCount;
	cout << "The average is : " << average << "\n";
	cout << averageResult(average,imageCount) << "\n";
	return 0;
}

The error

% g++ -o test test.cpp
% ./test
How many images in the list: 5
Segmentation fault
%

The file is reading

# IMAGE  NPIX  MEAN  STDDEV MIN    MAX
 0b.imh   1048576 1984.        490.1    1870.    65535.
 3c.imh   1048576 1984.        471.      1875.    65446.
 4g.imh   1048576 1984.        479.1    1872.    65335.
 3s.imh   1048576 1984.        473.8    1869.    65535.
 1z.imh   1048576 1984.        476.3    1869.    65522.

Recommended Answers

All 5 Replies

Use the debugger, it'll take just a minute to find the problem.
Compile with g++ -g -o test test.cpp to include debug information, then run
gdb test

And enter the commands start, then continue.
When it stops due to the segmentation fault, type "where".

The problem is that, when you initialize max, imageCount hasn't yet been assigned. Your compiler is probably defaulting it to zero (although this isn't guaranteed), which means max has size == 0. Then when you try to access it in your loop, you're accessing outside the bounds of the container, causing a segfault.

In general, if you're segfaulting, look for an access outside the bounds of an array or container. This is the usual culprit.

I ran gdb but all i get is this, which leaves me at the same place:

(gdb) start
Breakpoint 1 at 0x100000b0f: file test.cpp, line 24.
Starting program: /Users/gcardonav/Desktop/n6388/02sep09/Raw/test 
Reading symbols for shared libraries ++. done

Breakpoint 1, main () at test.cpp:24
24      int main()
(gdb)

Yeah, when you get there, enter "continue" (or just "c").

Thanks you guys, I was able to run my code once again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.