0

So I recently switched from Java to C++ and am building an example of selection sort as a way to get to know the Vector library a little bit better. However, when I try to run the program, there are no build errors but there are two debug errors.

enter image description here

enter image description here

After clicking ignore, I get a ton of warnings like this one:

enter image description here

I am still pretty new to C++ so I have no idea what is causing these errors and any help would be greatly appreciated. Here is the code I have written, thank you in advance. If you would like any more information please ask and I will provide what is needed.

#include <stdlib.h>
#include <vector>
#include <iostream>
using namespace std;

int findLowest(vector<int> in) {

    int min = in[0];
    int index = 0;

    for (int i = 1; i < in.size(); i++) {
        if (in[i] < min) {
            min = in[i];
            index = i;
        }
    }

    return index;
}

void printVector(vector<int> in) {
    vector<int>::iterator v = in.begin();
    while (v != in.end()) {
        cout << *v << endl;
        v++;
    }
}

vector<int> selectionSort(vector<int> toSort) {
    vector<int> temp;

    for (int i = 0; i < toSort.size(); i++) {
        int tempIndex = findLowest(toSort);
        temp.push_back(toSort[tempIndex]);
        temp.erase(temp.begin() + tempIndex);
    }

    return temp;
}

vector<int> randomArray(int size) {
    vector<int> temp;
    for (int i = 0; i < size; i++) {
        temp.push_back(rand() % 100);
    }
    return temp;
}

void main() {
    vector<int> toSort = randomArray(20);
    printVector(toSort);

    vector<int> sorted = selectionSort(toSort);
    printVector(sorted);

    cin.ignore();
}
Em Eldar
  • 686
  • 1
  • 8
  • 25
  • 1
    You should be running this in debug mode with the debugger enabled. please see [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Random Davis Jan 04 '17 at 19:36
  • 1
    I would also like to point that you are probably making unnecessary copy of your arrays. Unlike Java, all parameters, regardless of its type, are passed by value. In your case, your toSort array is *copied* when `selectionSort` is invoked. If all you need is a reference (or better yet: a constant reference), you need to do it explicitly, e.g. `selectionSort(const vector& toSort)` – CygnusX1 Jan 04 '17 at 19:53
  • To build on what @CygnusX1 said, see [this example of pass-by-value and pass-by-reference](http://ideone.com/IXr4xz). – Justin Time - Reinstate Monica Jan 04 '17 at 21:58
  • Also note that `const` can be placed before or after the actual type. `const int` and `int const` are both "constant `int`", for example, just as `const int&` and `int const&` are both "reference to `const int`". While this is mainly a coding style thing, it becomes important when pointers are involved: `int const *` and `const int *` are both "pointer to `const int`", but `int * const` is "`const` pointer to `int`". The distinction also matters with references to pointers: `const int*&` is "reference to `const int*`", but `int* const&` is "`const` reference to `int*`". – Justin Time - Reinstate Monica Jan 04 '17 at 22:17
  • (In case you haven't encountered this yet, I should also clarify that you can bind a less-cv-qualified variable to a more-cv-qualified pointer/reference, but not the other way around; `const int*` can hold the address of `int` or `const int`, and `const int&` can reference `int` or `const int`, but `int*` can only hold the address of `int`, and `int&` can only reference `int`. [\["cv-qualified" means "`const`-qualified, `volatile`-qualified, or `const volatile`-qualified."; cv-unqualified (no cv-qualifiers) is thus the least cv-qualified option.\]](http://stackoverflow.com/a/15413274/5386374)) – Justin Time - Reinstate Monica Jan 04 '17 at 22:28

2 Answers2

4

Click on break so that it can show you where the program encountered the error.

It will show you the call stack: enter image description here

Double click on a line that is not greyed out (your code).

Then you will see your code and you can add some data views (or add a watch, or just hover with your mouse): enter image description here

From here you can see that the temp vector has a size of 1, but the tempIndex variable had a value of 3 which means that the call to erase() is out of range as the error message mentioned.

Community
  • 1
  • 1
wally
  • 10,717
  • 5
  • 39
  • 72
2

Your selectionSort function is wierd, I would use std::sort.

Anyway, replace:

for (int i = 0; i < toSort.size(); i++) {
    int tempIndex = findLowest(toSort);
    temp.push_back(toSort[tempIndex]);
    temp.erase(temp.begin() + tempIndex);
}

by:

size_t size = toSort.size();
for (size_t i = 0; i < size; i++) {
    int tempIndex = findLowest(toSort);
    temp.push_back(toSort[tempIndex]);
    toSort.erase(toSort.begin() + tempIndex);
}

to fix your seg fault and have your container sorted.

Note that by saving size in a variable, you make sure you do 20 iterations...not 10...

Also, as commented by Adrian size_t (unsigned) should also be used rather than int (same comment for findLowest)

Next time, use a debugger, it should have been easy to spot this out by doing so...;-)

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • That was a good catch, I changed the code to that and all of the errors have disappeared. However, the sort function isn't sorting correctly haha. I'll mark your answer correct once it allows me. Thank you! – Em Eldar Jan 04 '17 at 19:38
  • 1
    If you ever want to notch up the compiler warning level, you'll have to get out of the habit of using `int`s for the sizes of standard containers. In 32-bit, you're implicitly converting between signed an unsigned integers. In 62-bit, you're also truncating. – Adrian McCarthy Jan 04 '17 at 19:54
  • @AdrianMcCarthy: Totally agree, did not want to change the code too much. As this `int` usage was not the cause of the problem. Updated my post anyway. – jpo38 Jan 04 '17 at 20:02