主返回后出现分段错误

2024-03-21

主函数返回后我收到分段错误。我已注释掉屏幕上显示的最后一项下方的所有内容。那是没有用的。因此,我不确定该怎么做,因为堆栈似乎没有损坏,而且我绝对不会超出任何数组的范围,因为我很确定我已经检查过这一点。任何帮助都会很棒。 谢谢, 乔

我的代码是:(对格式感到抱歉,尽管我使用了它指定的 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

分段错误aftermain return 语句很可能是由 main 中的局部变量或应用程序中的静态变量的析构函数引起的。最快的方法是在调试器内启动应用程序,调试器将指向程序出现段错误时的确切堆栈跟踪。

如果您转储了核心,您还可以使用调试器检查核心。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

主返回后出现分段错误 的相关文章

随机推荐

  • Firebase 中 IN 关键字的替代是什么?

    我在 Firebase 中的节点为 users uid phone name 我正在寻找IN执行这里 我想检查我的数据库中存在电话号码列表中的哪些条目 然后获取这些条目 在 SQL 数据库中 等效查询可能是 select phone fro
  • 为什么Webpack的DefinePlugin要求我们将所有内容都包装在JSON.stringify中?

    new webpack DefinePlugin PRODUCTION JSON stringify true VERSION JSON stringify 5fa3b9 BROWSER SUPPORTS HTML5 true TWO 1
  • 在 Hive HQL 中将字符串转换为时间戳

    我有一个像 08 03 2018 02 00 00 这样的字符串 我试图将其转换为时间戳值 我正在使用下面的代码 unix timestamp 08 03 2018 02 00 00 yyyy MM dd T HH mm ss SSSXXX
  • 仅更改所选选项的颜色

    我有一个选择器位于表格单元格中 表格行有颜色 因此使用 CSS 我可以将下拉菜单的背景更改为相同的颜色background color inherit 但是 它会更改所有选项的整个框的颜色 是否可以仅更改所选选项的颜色 如果不使用 CSS
  • Elasticsearch 范围查询和范围过滤的区别

    我想查询某个日期范围内的elasticsearch文档 我现在有两个选择 两个都适合我 两个都测试过 1 范围查询 2 范围过滤器 由于我现在的数据集很小 因此我无法测试它们的性能 这两者有什么区别 哪一种会导致更快的文档检索和更快的响应
  • OpenCv:查找多个匹配项

    我有以下内容 但我不知道如何找到源图像中的所有匹配项 static void Main using var template Cv LoadImage images logo png LoadMode GrayScale using var
  • 如何通过Intent接收int

    我通过 Intent 传递一个 int 但我不知道如何接收它 因为我必须从 OnCreate 方法接收一个 Intent 但如果我将它放在那里 我无法将它与代码其余部分中的另一个 int 进行比较 我在这里发送意图 public class
  • NUnit 与 Debug.Assert 冲突

    我正在使用 NUnit 为我的同事编写的库编写单元测试 他的库包含大量在无效输入时触发的 Debug Asserts 当我编写单元测试并向他的库提供无效输入时 他的 Debug Assert 会弹出一个消息框 抱怨输入错误 我觉得他的库对无
  • 从线程返回值

    在 Python 中 如何让线程将元组或我选择的任何值返回给父级 我建议你实例化一个队列 队列 http docs python org library queue html highlight queue Queue Queue在启动线程
  • Android JUnit4 测试

    我想运行一些 JUnit4 测试 该代码依赖于一些Android库 Android XML解析器 但不创建任何activites等 当我尝试运行测试时 我发现找不到我需要的 Android 类 有没有办法使用 Android 代码运行 JU
  • Makefile 在目标体中设置全局变量

    我想通过一个配方设置一个全局变量 然后在另一个配方中引用该变量独立的 recipe 下面的代码是在配方内设置变量的示例代码 但如果在配方外部引用 则变量将保持初始值 ACTIVE a switch ifeq ACTIVE b ACTIVE
  • 使用 NLTK 解析 CoNLL-U 文件

    我知道有CoNLL U https universaldependencies org docs format htmlPython 中的解析器 我只是想得到确认NLTK没有解析 CoNLL U 或具有依赖语法的其他 CoNLL 格式 的本
  • Slack 的 Azure DevOps 扩展是否会受到更改 Azure DevOps URL 的影响?

    在公司 我们将 Azure Devops 工作区的 URL 从https oldname visualstudio com to https dev azure com newname 我们使用两种方法与 Azure DevOps 中的 S
  • 单核上的多线程有什么意义?

    我最近一直在研究 Linux 内核 并回顾了大学操作系统课程的时代 就像那时一样 我正在玩线程之类的东西 一直以来我一直假设线程是自动在多个核心上同时运行但我最近发现您实际上必须显式编写代码来处理多个核心 那么单核上的多线程有什么意义呢 我
  • Spring Cloud 2020.0 不再处理 bootstrap.yml 配置

    In my Spring Boot 项目 https github com pavankjadda HashiCorpVault SpringCloud 我定义了4个配置文件 demo dev test prod 在启动过程中 YAML 文
  • Web服务/API来抓取另一个网站的屏幕截图? (已知的解决方案太慢)[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 选择另一个相关列的总计为 0 的列

    我有一张桌子叫xDays设置如下 Project Name Date Hours proj1 2010 03 03 00 00 00 000 0 proj1 2010 03 04 00 00 00 000 0 proj1 2010 03 0
  • Sitecore - 多站点错误页面处理

    我有一个多站点 Sitecore 项目 我知道如何通过 webconfig 的 ErrorPage ItemNotFoundUrl LayoutNotFoundUrl LinkItemNotFoundUrl 处理一个站点的错误页面 但是 我
  • Symfony 4:加载 Web 调试工具栏时发生错误

    我在 CentOS 上工作 我已经按照教程进行操作 http symfony com doc current best practices creating the project html http symfony com doc cur
  • 主返回后出现分段错误

    主函数返回后我收到分段错误 我已注释掉屏幕上显示的最后一项下方的所有内容 那是没有用的 因此 我不确定该怎么做 因为堆栈似乎没有损坏 而且我绝对不会超出任何数组的范围 因为我很确定我已经检查过这一点 任何帮助都会很棒 谢谢 乔 我的代码是