gpt4 book ai didi

c++ - 增加堆栈不起作用

转载 作者:太空狗 更新时间:2023-10-29 20:18:20 24 4
gpt4 key购买 nike

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

假设我做对了,程序在没有改变堆栈的情况下实际上运行得更快。 gcc -Wl,--stack,1099511627776

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

我该怎么办?有没有办法改变代码本身的堆栈?这是我用于冒泡和快速排序的代码。每个有两个:一个是 vector ,另一个是数组。我认为冒泡排序。应该是正确的。快速排序,我不太确定。对不起,如果它有点乱

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 个条目的数组和 vector ,使用上述代码对它们进行排序并返回时间。那部分我很确定我没有搞砸

我的主要就是

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

一次又一次

最佳答案

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

想发布一些代码吗?

关于c++ - 增加堆栈不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4310080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com