开发者

Segmentation Fault After Main Returns

开发者 https://www.devze.com 2023-02-12 00:48 出处:网络
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 bec

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 = "MICas开发者_开发知识库e.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


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.


    outFile.open(argv[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.


In my case, after many long hours, doing a make clean solved my problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号