3

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

Community
  • 1
  • 1
calccrypto
  • 8,583
  • 21
  • 68
  • 99
  • 2
    How have you implemented these algorithms? A correct bubble sort implementation should not be recursive and a correct recursive Quicksort implementation should require on average O(lg n) depth on the call stack (the worst case space complexity is technically linear if you fail to choose good pivot values or you have truly perverse data). If you are running out of space on the stack with a recursive algorithm, your best bet is to reimplement it in an iterative form or fake the recursion using your own stack that you can allocate on the heap. – James McNellis Nov 30 '10 at 02:48
  • i think i did bubble sort right. quick sort i just learned today, so im not so sure about it. in my hw instructions, it specifically says that i will need to change the stack – calccrypto Nov 30 '10 at 02:53
  • There is no way the functions you show are causing you to run out of space on the stack. Neither of them calls any functions and neither of them allocates any large objects on the stack. – James McNellis Nov 30 '10 at 02:58
  • 1
    Note that `v_bubble` does not give the correct result even for a small number of elements and it does not correctly handle an empty `std::vector`. – James McNellis Nov 30 '10 at 03:00
  • oops. Do you have any idea what is crashing? i doubt very much that it is the part of the code that shows how long it takes to finish sorting – calccrypto Nov 30 '10 at 03:01
  • @James but how about vectors that are transmitted by value? – ruslik Nov 30 '10 at 03:01
  • @ruslik: `std::vector` stores its underlying array on the heap. The `std::vector` object itself merely maintains pointers to the heap-allocated array. – James McNellis Nov 30 '10 at 03:02
  • @calccrypto: I have to ask, are you implementing a Quicksort you saw in a book or some reference pseudo-code? There are a lot of things wrong with `a_quick()` including the `return` statement at the end. – Blastfurnace Nov 30 '10 at 03:17
  • Wikipedia. im not really expecting it to be right, although it would be nice if you can point out all the errors. i think i know how to do it by hand, im not very good with recursion, so my code is not that great – calccrypto Nov 30 '10 at 03:19
  • The error in this code is that the algorithm is incorrectly implemented. Presumably this is an assignment for a class, in which case, it would behoove you to study the algorithm, work through it step by step on paper with a small set of sample data, and implement it one step at a time. It is very beneficial for you to work through these problems yourself; they are very good exercises from which you can learn how to write code to implement algorithms. – James McNellis Nov 30 '10 at 03:25
  • @calccrypto: In a_quick() it looks like you are trying to concatenate the arrays left[] and right[] with a `+` operator. Arrays simply don't work that way. In addition, the function return type is `int`. That means it returns a single `int` value, not an array. You are missing some of the real basics of how C and C++ arrays work. – Blastfurnace Nov 30 '10 at 03:27
  • sorry. im a python person. EDIT: oh shoot. i see – calccrypto Nov 30 '10 at 03:29
  • To add to what @Blastfurnace says, the Wikipedia description of the algorithms may not be the best; both Bubble Sort and Quicksort should be in-place algorithms. I'd consult your algorithms textbook for a good description of the Quicksort algorithm. – James McNellis Nov 30 '10 at 03:30
  • You're trying to set the stack HOW BIG? That number looks to me like 1 **tera**byte (240 bytes). – Ben Voigt Nov 30 '10 at 03:31
  • im just trying anything. my hw paper says to set it to 1 billion. that number up there is just because i was bored – calccrypto Nov 30 '10 at 03:31
  • @calccrypto: There's nothing wrong with Python. It's just that your Python instincts have lead you astray in C++. They are different enough that you probably want to get a good C++ reference and work on some simpler test programs to learn the differences. – Blastfurnace Nov 30 '10 at 03:34

2 Answers2

6

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?

Drew Hall
  • 28,429
  • 12
  • 61
  • 81
1

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.

ruslik
  • 14,714
  • 1
  • 39
  • 40
  • how would i fix that? would i just give array a large initial size? – calccrypto Nov 30 '10 at 03:12
  • @calccrypto: No, you aren't returning the array; you are returning the `size`-th element of the array; since there is no element at index `size` (by definition), the results are undefined. – James McNellis Nov 30 '10 at 03:15
  • is my a_bubble better now? as to the program im running, im just pressing the "build and run" button. thats the only error on the screen. – calccrypto Nov 30 '10 at 03:25
  • yes, but it fails after a while. it runs through all 4 sorts for the 32k lists, but fails at the second 64k sort: quicksort with arrays. im afraid i dont know how to change a_quick – calccrypto Nov 30 '10 at 03:29
  • wait a sec... its running, but its not stopping after it finishes the 128k sort. weird – calccrypto Nov 30 '10 at 03:33
  • the debugger is still crashing – calccrypto Nov 30 '10 at 03:35