Question

I'm using a function to fill a vector with random template (testing using doubles) values:

    template <typename T>
    void setList(vector<T> & theList)
    {
        srand(time(NULL));          

        int howMany, max, min;
        cout << "How many items should be generated in the list? ";
        cin >> howMany;
        cin.ignore();

        cout << "Max value of list? ";
        cin >> max;
        cin.ignore();

        cout << "Min value of list? ";
        cin >> min;
        cin.ignore();

        theList.resize(howMany);

        T randValue;
        for (int i = 0; i < howMany; ++i)
        {
            randValue = (((double) rand() / (double) RAND_MAX) * (max - min)) + min;
            theList[i] = randValue;
        }
    }

The function is called in main on a vector:

    vector<double> listA;
    setList(listA);

No matter what value "howMany" is, when I reach subscript howMany-2, I am getting a subscript out of range error.

Stepping through the debugger, my index is within the subscript range.

For example, if user enters 10 for howMany, when the loop tries to enter

theList[8] = randValue;

The error is thrown.

Any ideas?

Was it helpful?

Solution

I have tried this code in a Solaris envioronment and I have not got the out of range index exception.

This is my code:

 template <typename T>
 void setList(std::vector<T> & list)
 {
    srand(time(NULL));

    int howMany, max, min;
    std::cout << "How many items should be generated in the list? ";
    std::cin >> howMany;
    std::cin.ignore();

    std::cout << "Max value of list? ";
    std::cin >> max;
    std::cin.ignore();

    std::cout << "Min value of list? ";
    std::cin >> min;
    std::cin.ignore();

    list.resize(howMany);

    T randValue;
    for (int i = 0; i < howMany; ++i)
    {
       randValue = (((T) rand() / (T) RAND_MAX) * (max - min)) + min;
     list[i] = randValue;
    }
    // I Have added this feature for visualization purposes
    for (int i = 0; i < list.size(); ++i)
    {
       std::cout << "list[" << i << "]=" << list[i] << std::endl;
    }
  }

And my main() looks like

 int main(int argc, char** argv) {
     std::vector<int> lista;
     lista.clear();
     setList(lista);
 }

My output is:

My execution output

By the way I have noticed that if you define a vector of integers, the content of the vector is always the min value. This is because you need to cast the intermediate values to double and cast finally the result to type T. (In the intege type the "/" operator is interpreted as an integer division).

You could do this.

 for (int i = 0; i < howMany; ++i)
 {
     randValue = static_cast<T>((static_cast<double>((T) rand()) / static_cast<double>((T) RAND_MAX)) * (max - min)) + min;
     list[i] = randValue;
 }

OTHER TIPS

The answer to my dilemma was provided by @SingerOfTheFall.

Specifically, getting a nonsensical error when debugging (in this case, an out of range while actually within the range) calls for testing different avenues.

If loading in a different compiler shows the code runs fine, the issue is then not necessarily with the code.

Recompiling the code from scratch ("cleaning") the code did the trick in this situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top