主返回后出现分段错误
主函数返回后我收到分段错误。我已注释掉屏幕上显示的最后一项下方的所有内容。那是没有用的。因此,我不确定该怎么做,因为堆栈似乎没有损坏,而且我绝对不会超出任何数组的范围,因为我很确定我已经检查过这一点。任何帮助都会很棒。 谢谢, Joe
我的代码是:(对格式感到抱歉,尽管我使用了它指定的 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
I receive a segmentation fault after the main function returns. I have commented out everything below the last item that displays on the screen. That was of no use. Therefore I'm not sure what to do because it doesn't seem that the stack is corrupted and I am definitely not going out of bounds on any of my arrays because I'm pretty sure that I checked that. Any help would be great.
Thanks,
Joe
My code is: (sorry about the formatting, this website messed it all up even though i used the 4 spaces it specified.)
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
主返回语句之后的分段错误很可能是由 main 中的局部变量或应用程序中的静态变量的析构函数引起的。最快的方法是在调试器中启动应用程序,调试器将在程序发生段错误时指向确切的堆栈跟踪。
如果您转储了核心,您还可以使用调试器检查核心。
A segmentation fault after the main return statement is most probably caused by the destructor of either a local variable in main, or a static variable in the application. The fastest thing to do is launch the application inside a debugger, the debugger will point at the exact stack trace when the program segfaulted.
If you have cores dumped, you can also inspect the cores with the debugger.
该行看起来很可疑,因为没有引用 argv[2] 或 argv[3]。传递少于 4 个命令行参数肯定会导致程序崩溃。
Line looks suspicious since there is no reference to argv[2] or argv[3]. Passing in less than 4 command line params is certainly going to cause your program to crash.
就我而言,经过很长的时间后,执行
make clean
解决了我的问题。In my case, after many long hours, doing a
make clean
solved my problems.