开发者

Increasing stack not working

开发者 https://www.devze.com 2023-01-27 07:26 出处:网络
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:

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 a开发者_运维技巧rray;
}

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


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?


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.

0

精彩评论

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