主函数返回后,我收到分段错误。我已经注释掉了屏幕上显示的最后一项下面的所有内容。那是没用的。因此我不知道该怎么办,因为它似乎没有被破坏,我肯定不会超出我的任何数组,因为我很确定我检查过。任何帮助都会很棒。 谢谢, 乔
我的代码是:(抱歉格式化,即使我使用了指定的4个空格,这个网站也搞砸了。)
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;
//Precondition: The List array has been broken down as far as possible.
//The integer arrays start, mid, and end indicate the position in the
//list array that is being merged back together in sorted order.
//The length variable indicates the length of the list array.
//This function combines the pieces of the array that were split apart
//in sorted order.
//Postcondition: It will return a completely sorted list as one array.
void merge(int list[], int start, int mid, int end, int length);
//Precondition: The elements are already in the array list. The length
//integer variable contains the size of the list.
//The function uses the insertion sort algorithm to produce a fully sorted
//array.
//Postcondition: The elements in the list array are completely sorted and stored
//in the array list variable and nothing is returned.
void insertion_sort(int list[], int length, int start);
//Precondition: The elements are already in the array list. The variables start,
//and end contain the bounds for part of the array that is being working on.
//The length integer variable contains the size of the list.
//This function is a hybrid between the merge and insertion sort algorithms. It
//will divide the problem into manageable pieces and then utilize the insertion
//sort algorithm and then merge the sorted pieces back together.
//Postcondition: The list array will be sorted.
void mergeAndInsertion_sort(int list[], int start, int end, int length);
//Precondition: The elements in the list array are all sorted and length is
//the length of the array.
//This function checks to make sure that the array passed to it is sorted in
//ascending order.
//Postcondition: This function returns a boolean value indicating whether or
//not it was in ascending order or not.
bool checker (int list[], int length);
int main(int argc, char *argv[])
{
timespec end, start;
long double difference;
int temp = 0, length = -1, insertion_difference = 0, merge_difference = 0, MI_difference = 0;
cout.setf(ios::fixed);
cout.precision(2);
ifstream inFile;
ofstream outFile, dataFile;
//opens the file and gets the length for the array.
inFile.open(argv[1]);
if(outFile.fail())
{
cout << "The file didn't open." << endl;
exit(1);
}//ends the if statement.
while (! inFile.eof() )
{
inFile >> temp;
length++;
}
inFile.close();
//declares a dynamic array with the correct amount of space.
int* insertion_data = new int[length];
int* merge_data = new int[length];
int* MI_data = new int[length];
//fills the data array from the input text.
inFile.open(argv[1]);
if(outFile.fail())
{
cout << "The file didn't open." << endl;
exit(1);
}//ends the if statement.
for (int i = 0; i < length; i++)
{
inFile >> insertion_data[i];
merge_data[i] = insertion_data[i];
MI_data[i] = insertion_data[i];
}//ends the for loop with i as the counting variable.
//**********************Starts the merge and insertion sort testing.********************************
clock_gettime(CLOCK_REALTIME, &start); //Gets the time before the algorithm starts
mergeAndInsertion_sort(MI_data, 0, (length - 1), length);
clock_gettime(CLOCK_REALTIME, &end); //Gets the time after the algortihm finishes
MI_difference = (end.tv_nsec - start.tv_nsec); //finds how long the function ran
outFile.open(argv[4]);
if(outFile.fail())
{
cout << "The file didn't open." << endl;
exit(1);
}//ends the if statement.
for (int index = 0; index < length; index++)
outFile << MI_data[index] << endl;
outFile.close();
if( !checker(MI_data, length) )
cout << "The merge and insertion sort algorithm is correct for " << length << " data elements." << endl;
else
cout << "The merge and insertion sort algorithm is wrong for " << length << " data elements." << endl;
string mfile = "MICase.dat";
dataFile.open(mfile.c_str(), ios::app);
if(dataFile.fail())
{
cout << "The file didn't open." << endl;
exit(1);
}//ends the if statement.
dataFile << length << " " << MI_difference << endl;
dataFile.close();
delete [] insertion_data;
delete [] merge_data;
delete [] MI_data;
// cout << "merge and insertion sort's running time was " << MI_difference <<" nano seconds."<< endl << endl;
//**********************Ends the merge and insertion sort testing.********************************
return 0;
}//ends the main program
//This code is derived from the book Introduction to Algorithms
//By Thomas H. Corman, Charles E. Leiserson, Ronald L. Rivest
//and Clifford Stein Copyright 2009
void mergeAndInsertion_sort(int list[], int start, int end, int length)
{
int mid = 0;
if(start < end)
{
mid = (start + end) / 2;
if ( (mid - start) <= 10 )
insertion_sort(list, (mid - start), start );
else
mergeAndInsertion_sort(list, start, mid, length);
if ( (end - mid) <= 10 )
insertion_sort(list, (end-mid)+mid+1, mid );
else
mergeAndInsertion_sort(list, (mid + 1), end, length);
merge(list, start, mid, end, length);
}//ends the if statement
}//ends the merge_sort function
//This code is derived from the book Introduction to Algorithms
//By Thomas H. Corman, Charles E. Leiserson, Ronald L. Rivest
//and Clifford Stein Copyright 2009
void merge (int list[], int start, int mid, int end, int length)
{
int length1 = (mid - start + 1);
int length2 = (end - mid);
int* left = new int[length1];
int* right = new int[length2];
int i = 0, j = 0;
for (i = 1; i <= length1; i++)
{
left[i] = list[start + i - 1];
//cout << "left[i] = " << left[i] << endl;
}
cout << endl;
for (j = 1; j <= length2; j++)
{
right[j] = list[mid + j];
//cout << "right[j] = " << right[j] << endl;
}
i = 1;
j = 1;
//cout << "left[] = " << left[i] << " right[] = " << right[j] << endl;
for (int k = start; k<=end; k++)
{
if (i > length1)
{
list[k] = right[j];
j++;
}
else if (j > length2)
{
cout << "in the if for j" <<endl;
list[k] = left[i];
i++;
}
else if (left[i] <= right[j])
{
list[k] = left[i];
i = i + 1;
}//ends the (left[i] <= right[j])
else if (right[j] <= left[i])
{
list[k] = right[j];
j = j + 1;
}//ends the else statement for (left[i] <= right[j])
//cout << "left[] = " << left[i] << " right[] = " << right[j] << " list[k] = " << list[k]<< endl;
}//ends the for loop with k as the counter
for (int k = start; k<=end; k++)
cout << list[k]<<endl;
}//ends the merge function
//This code is derived from the book Introduction to Algorithms
//By Thomas H. Corman, Charles E. Leiserson, Ronald L. Rivest
//and Clifford Stein Copyright 2009
void insertion_sort(int list[], int length, int start)
{
cout << "length = " << length << endl;
cout << "start = " <<start << endl;
for(int i = start; i<length; i++)
cout << "list[i] = " << list[i]<<endl;
cout << endl;
int key = 0, i = 0;
for (int j = start; j < length; j++)
{
key = list[j];
i = j - 1;
while ( (i >= 0) && (list[i] > key) )
{
list[i + 1] = list[i];
i = i - 1;
}//ends the while loop with i as the counting variable
list[i + 1] = key;
}//ends the for loop with index as the counter
}//ends the insertion_sort function
bool checker (int list[], int length)
{
bool issue = false;
for(int i = 1; i < length; i++)
if (list[i - 1] > list[i])
issue = true;
return issue;
}//ends the checker function
答案 0 :(得分:5)
主
如果您有转储的核心,您还可以使用调试器检查核心。
答案 1 :(得分:4)
outFile.open(argv[4]);
Line看起来很可疑,因为没有引用argv [2]或argv [3]。传递少于4个命令行参数肯定会导致程序崩溃。
答案 2 :(得分:2)
就我而言,经过很长时间后,make clean
解决了我的问题。