简体   繁体   English

调试时我的代码运行正常,但是一旦运行它就会崩溃

[英]My code runs fine when I debug it, but crashes as soon as I run it

I have been asked to built a K sorted array by my professor and then sort it using minheap. 我被要求由我的教授构建一个K排序数组,然后使用minheap对其进行排序。 A k sorted array is basically an array where each element is at most K positions away from the location it would be if the array was fully sorted in ascending order. k排序的数组基本上是一个数组,其中每个元素最多K个位置,远离数组按升序完全排序的位置。 For instance, if K=3, the element at position i=8 can be at locations 5, 6, 7, 8, 9, 10, or 11 in the fully sorted arrayI'm stuck in the first part of the problem. 例如,如果K = 3,则位置i = 8处的元素可以位于完全排序的阵列中的位置5,6,7,8,9,10或11处于问题的第一部分中。 Requirements: 要求:

  1. Allow the user to enter a number of (N) elements and the number K. 允许用户输入多个(N)元素和数字K.

  2. Produce a K-sorted array based on the user input (more than one K-sorted array can be produced; randomly pick one). 根据用户输入生成K排序数组(可以生成多个K排序数组;随机选择一个)。 Display it. 显示它。

After rigorous thinking I built the below code for K Sorting: 经过严格的思考后,我为K排序构建了以下代码:

void kSortedArray:: displayKSorted()
{
    cout<<"Please enter the no. of elements you want to KSort: ";
    cin>>N;
    cout<<"Please enter the value for K: ";
    cin>>K;
    int i=0;

    while (i<N)
    {
        cout<<"Enter element "<<i+1<<" : ";
        cin>>arr[i++];

    }
    insertion_sort(arr,N);
    srand(time(NULL));
    int output=0;
    i=0;
    int maxRand=0;
    int minRand=0;
    int arr2[N];
    int a=0;
    int found=0;
    int found1=0;
    int range=0;
    int count1=0;
    vector<int>:: iterator it1;
    vector<int>:: iterator it2;
    vector<int> arr3;
    while(kSortedIndex.size()<N)
    {
        minRand=range-K;
        maxRand=range+K;
        output = minRand + (rand() % (maxRand - minRand + 1));
        if (output<0)
        {
            output=N+output;
        }
        if(output>N-1)
        {
            output=output-N;
        }
        //arr2[i]=output;
        for(it1=kSortedIndex.begin();it1!=kSortedIndex.end();it1++)
        {
            if(*it1==output)
            {
                found=1;
                for(it2=arr3.begin();it2!=arr3.end();it2++)
                {
                    if(*it2==output)
                    {
                        found1=1;
                        break;
                    }
                }

                    if(found1==0)
                    {
                        arr3.push_back(output);
                        count1++;
                    }
                break;

            }
        }

        if(found==0)
        {
            kSortedIndex.push_back(output);
            count1=0;
            arr3.clear();
            range++;
        }
        found=0;
        found1=0;

        if (count1>((2*K)+1))
        {
            range--;
            count1=0;

        }
        if(range>(N-1))
            range--;



    }
    vector<int>::iterator it;
    int j=0;
//    i=0;

    for(it=kSortedIndex.begin();it!=kSortedIndex.end();it++)
    {
        arr2[*it]=arr[j];
        j++;
        //cout<<arr2[j++];
    }

    for(j=0;j<N;j++)
    {
        cout<<arr2[j];
    }

}

Below is the header file: 以下是头文件:

class kSortedArray
{
    public:
        kSortedArray();
        void displayKSorted();


    protected:

    private:
        int N=0;
        int K=0;
        int* arr=new int [N];
        createKSorted();
        void insertion_sort(int arr[],int length);
        vector <int> kSortedIndex;

};

Could someone please help me to figure out why my code crashes when I run it and runs fine when I debug it. 有人可以帮我弄清楚为什么我的代码在运行时崩溃并在我调试时运行正常。 Is it a memory leak issue? 这是内存泄漏问题吗? I even tried deleting arr, arr2 and clearing arr3 but that isn't working (code still cashes). 我甚至尝试删除arr,arr2和清除arr3,但这不起作用(代码仍然是兑现)。 Your help much appreciated. 非常感谢您的帮助。

The declaration int* arr=new int [N]; 声明int* arr=new int [N]; in the header is wrong. 在标题中是错误的。 You cannot allocate memory for the array here because you do not yet know the value for N . 您无法在此为阵列分配内存,因为您还不知道N的值。 That value will be known only after cin>>N; 只有在cin>>N;之后才知道这个值cin>>N; in the kSortedArray:: displayKSorted function. kSortedArray:: displayKSorted函数中。 Therefore you need to do allocate memory there: 因此你需要在那里分配内存:

void kSortedArray:: displayKSorted()
{
    cout<<"Please enter the no. of elements you want to KSort: ";
    cin>>N;

    arr = new int [N];                     // <<< add this line

    cout<<"Please enter the value for K: ";
    cin>>K;
    int i=0;
    ...

Header: 标题:

   ...
   private:
        int N=0;
        int K=0;
        int* arr;    // =new int [N];         <<< remove the allocation
        createKSorted();
        void insertion_sort(int arr[],int length);
        ...

Disclaimer: This is non tested non error checking code, and there may be more problems. 免责声明:这是未经测试的非错误检查代码,可能存在更多问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 .exe 在 Qt Creator 中运行后立即崩溃 - My .exe crashes as soon as I run it in Qt Creator 如果调试运行正常但释放崩溃怎么办 - what to do if debug runs fine, but release crashes 代码通过调试器运行良好,但在执行时崩溃 - Code runs fine through a debugger but crashes on execution MKL 示例代码使用 cmake 编译良好,但运行时崩溃 - MKL example code compiles fine with cmake but crashes when run SFML 2.1程序在调试模式下运行良好,但在发布模式下崩溃 - SFML 2.1 program runs fine in debug mode, but crashes in release mode 我的程序运行良好,我可以复制对象,但是当我使用复制分配 (=) 时,它仍然运行良好。 为什么不报错? - My program runs fine and I am able copy the object however when I used the copy assignment (=) it still runs fine. Why is it not giving error? 当我为调试编译我的程序时,Lua 运行正常,但为什么我为发布编译它,我得到一个 c0000005 错误 - When I compile my program for Debugging the Lua runs fine but why I compile it for release I get a c0000005 error 代码::阻止调试模式:如果构建和运行,我的代码会崩溃,但如果调试/继续,则不会崩溃 - Code::Blocks debug mode: my code crashes if build and run, but not if debug/continue 代码在代码块上运行良好,但无法在SPOJ上运行 - The code runs fine on codeblock but fails to run on SPOJ Mac os x应用程序捆绑包在下载并运行时崩溃,但在终端或更改Info.plist时运行正常 - Mac os x app bundle crashes when downloaded and run, but runs fine in the terminal or when changing Info.plist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM