增加堆栈不起作用

发布于 2024-10-05 06:25:55 字数 3327 浏览 0 评论 0原文

如何使用 Bloodshed Dev C++ 或 Code::Block 正确增加程序可用的堆栈?我正在运行简单的冒泡和快速排序,但是当我更改 Code::Block 中的堆栈时(发现了如何这里)它使我的程序崩溃得更快,尽管使用的空间比建议的空间多得多。最初,程序在对 64K 随机整数进行排序时崩溃(使用 rand() 函数)。现在,它在 32K 时崩溃了。我收到错误:进程返回-1073741571 (0xC00000FD)

程序实际上运行得更快,而没有更改堆栈,假设我做得正确。 gcc -Wl,--stack,1099511627776

我不知道如何在 Dev C++ 中更改它,

我该怎么办? 有什么方法可以更改代码本身中的堆栈吗? 这是我用于冒泡和快速排序的代码。每种都有两种:一种是向量,另一种是数组。我认为是冒泡排序。应该是正确的。快速排序,我不太确定。抱歉,如果它有点混乱,

vector <int> v_bubble(vector <int> array){
    // Vector Bubble Sort
    if (array.size() < 2){
        return array;
    }
    int s = 1;
    while (s){
        s = 0;
        for (unsigned int x = 0; x < (array.size() - 1); x++){
            if (array[x] > array[x + 1]){
                int t = array[x];
                array[x] = array[x + 1];
                array[x + 1] = t;
                s = 1;
            }
        }
    }
    return array;
}

void a_bubble(int array[], int size){
    // Array Bubble Sort
    int s = 1;
    while (s){
        s = 0;
        for (int x = 0; x < (size - 1); x++){
            if (array[x] > array[x + 1]){
                int t = array[x];
                array[x] = array[x + 1];
                array[x + 1] = t;
                s = 1;
            }
        }
    }
}

vector <int> v_quick(vector <int> array){
    //Vector Quick Sort
    if (array.size() < 2){
        return array;
    }
    vector <int> left;
    vector <int> right;
    int p_location = array.size() / 2 - 1;
    int pivot = array[p_location];
    for(unsigned int x = p_location; x < array.size() - 1; x++){
        array[x] = array[x + 1];
    }
    array.pop_back();
    for(unsigned int x = 0; x < array.size(); x++){
        if (array[x] <= pivot) {
            left.push_back(array[x]);
        }
        else if (array[x] > pivot){
            right.push_back(array[x]);
        }
    }
    vector <int> p;
    p.push_back(pivot);
    return combine(combine(v_quick(left), p), v_quick(right));
}

int a_quick(int array[], int size, int l_index = -1, int r_index = -1){
    //Array Quick Sort
    if (size < 2){
        return array[size];
    }
    array[size] = array[size];
    int left[size];
    int right[size];
    l_index = 0;
    r_index = 0;
    int p_location = size / 2 - 1;
    int pivot = array[p_location];
    for(int x = p_location; x < size - 1; x++){
        array[x] = array[x + 1];
    }
    size--;
    for(unsigned int x = 0; x < size; x++){
        if (array[x] <= pivot) {
            left[l_index] = array[x];
            l_index++;
        }
        else if (array[x] > pivot){
            right[r_index] = array[x];
            r_index++;
        }
    }
    return a_quick(left, l_index, l_index, r_index) + pivot + a_quick(right, r_index, l_index, r_index);
}

其余的代码只是简单地生成具有 32、64 和 128 k 条目的数组和向量,使用上面的代码对它们进行排序并返回时间。我很确定我没有搞砸那部分,

我的主要内容基本上只是

    start = clock();
    a_quick(array1, 32000);
    end = clock();
    cout << "\nQuick Sort\tArray\t32000\t" << ((double) end - start)/CLOCKS_PER_SEC << " seconds\n";

一遍又一遍

How do can i properly increase the stack available for a program using either Bloodshed Dev C++ or Code::Block? Im running simple bubble and quick sort that work, but when i changed the stack in Code::Block (found out how over here) it made my program crash even faster, despite using much more than suggested space. originally, the program was crashing while sorting 64K of random integers (using the rand() function). Now, its crashing at 32K. im getting the error: Process returned -1073741571 (0xC00000FD)

the program actually runs faster without having the stack changed, assuming im doing it right. gcc -Wl,--stack,1099511627776

i cant figure out how to change it at all in Dev C++

What should i do?
is there any way to change the stack within the code itself?
here is the code im using for bubble and quick sort. there are two of each: one is with vectors, the other is with arrays. i think that the bubble sort. should be correct. the quick sort, im not so sure about. sorry if its a bit messy

vector <int> v_bubble(vector <int> array){
    // Vector Bubble Sort
    if (array.size() < 2){
        return array;
    }
    int s = 1;
    while (s){
        s = 0;
        for (unsigned int x = 0; x < (array.size() - 1); x++){
            if (array[x] > array[x + 1]){
                int t = array[x];
                array[x] = array[x + 1];
                array[x + 1] = t;
                s = 1;
            }
        }
    }
    return array;
}

void a_bubble(int array[], int size){
    // Array Bubble Sort
    int s = 1;
    while (s){
        s = 0;
        for (int x = 0; x < (size - 1); x++){
            if (array[x] > array[x + 1]){
                int t = array[x];
                array[x] = array[x + 1];
                array[x + 1] = t;
                s = 1;
            }
        }
    }
}

vector <int> v_quick(vector <int> array){
    //Vector Quick Sort
    if (array.size() < 2){
        return array;
    }
    vector <int> left;
    vector <int> right;
    int p_location = array.size() / 2 - 1;
    int pivot = array[p_location];
    for(unsigned int x = p_location; x < array.size() - 1; x++){
        array[x] = array[x + 1];
    }
    array.pop_back();
    for(unsigned int x = 0; x < array.size(); x++){
        if (array[x] <= pivot) {
            left.push_back(array[x]);
        }
        else if (array[x] > pivot){
            right.push_back(array[x]);
        }
    }
    vector <int> p;
    p.push_back(pivot);
    return combine(combine(v_quick(left), p), v_quick(right));
}

int a_quick(int array[], int size, int l_index = -1, int r_index = -1){
    //Array Quick Sort
    if (size < 2){
        return array[size];
    }
    array[size] = array[size];
    int left[size];
    int right[size];
    l_index = 0;
    r_index = 0;
    int p_location = size / 2 - 1;
    int pivot = array[p_location];
    for(int x = p_location; x < size - 1; x++){
        array[x] = array[x + 1];
    }
    size--;
    for(unsigned int x = 0; x < size; x++){
        if (array[x] <= pivot) {
            left[l_index] = array[x];
            l_index++;
        }
        else if (array[x] > pivot){
            right[r_index] = array[x];
            r_index++;
        }
    }
    return a_quick(left, l_index, l_index, r_index) + pivot + a_quick(right, r_index, l_index, r_index);
}

the rest of the code is simply generating arrays and vectors with 32, 64 and 128 k entries, sorting them using the above code and returning the time. that part im pretty sure i did not screw up

my main is basically just

    start = clock();
    a_quick(array1, 32000);
    end = clock();
    cout << "\nQuick Sort\tArray\t32000\t" << ((double) end - start)/CLOCKS_PER_SEC << " seconds\n";

over and over again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-10-12 06:25:56

除非您正在为内存非常有限的嵌入式环境进行编程,否则我怀疑您的排序实现中存在导致堆栈溢出的递归错误。除非您正在处理真正巨大(许多 GB)的数组,否则不需要更改堆栈大小。

介意发布一些代码吗?

Unless you're programming for a very memory-constrained embedded environment, I suspect you've got a recursion bug in your sort implementations that is causing stack overflow. Changing the stack size shouldn't be necessary unless you're dealing with truly enormous (many GB) arrays.

Care to post some code?

撩发小公举 2024-10-12 06:25:56

在函数 int a_bubble(int array[], int size) 中:您返回 array[size] ,这是越界的。 a_quick() 也是如此。

请同时发布您的 main()

编辑:不,它返回数组最后一个元素之后的元素。有些人可能会说即使访问它也是 UB,他们是对的。

您正在运行的是调试版本吗?我认为错误代码对应于“越界异常”,但我不明白为什么它必须如此神秘。

EDIT2:情况更糟。什么int array[1 << 20] 代表什么?旧版本没问题,您只需删除 return array[size] 即可。让函数返回void,你已经在被调用者中拥有数组及其大小。

In function int a_bubble(int array[], int size) : you return array[size], that is out of bound. The same is true for a_quick().

And post also your main() please.

EDIT: no, it returns the element that is after the last element of the array. Some could say that even accessing it is UB, and they would be right.

Is it debug build you are running? I think the error code corresponds to "out of bound exception", but I don't understand why it have to be so cryptic.

EDIT2: it's worse. What int array[1 << 20] stands for? The old version was OK, you just had to remove return array[size]. Make the functions returning void, you already have both, array and its size in the callee.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文