Solving a dynamic memory problem caused by vectors

I have run into a problem with dynamic memory when dealing with vectors. How I know this is that I ran my program via the debugger, and it has at the top of the call stack a method from __cxa_throw(). //In fact, there are methods from new_allocator.h on top of methods from vector!

The weird thing is that this problem only happens for one case, though I think I should post my entire main() where all this code is (I tried to avoid global functions that handle any type of data except for user input to close the program):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
int main()
{
    bool keepLooping = true;    //this will make the program keep asking for a choice when a valid one is not entered
    bool exitCommand = false;   //this will keep track of the user wanting to exit the program
    char userInput;
    vector<int> data;
    int elementCount;   //the amount of elements in data
    cout << "Binary Search Tree Creator" <<endl;
    cout << "==========================\n" << endl;
    while (keepLooping)
    {
        cout << "How would you like to create a binary search tree?" << endl;
        cout << "(1) Direct input" << endl;
        cout << "(2) Random numbers (you decide how many)" << endl;
        cout << "(3) Random numbers (random amount of random numbers" << endl;
        cout << "(0) Exit this program" << endl;
        cout << "\nPlease enter a selection: ";
        userInput = cin.get();  //attempt to get only one character from the user
        //exit the loop on a valid condition
        if ((userInput >= '0') && (userInput <= '3')) keepLooping = false;
        if (userInput == '0') exitCommand = true;
    }

    if (userInput > '0')
    {
        if (userInput < '3')
        {
            //get the specified elementCount from the user
            while (elementCount <= 0)
            {
                cout << "How many random numbers should be in the data? ";
                cin >> elementCount;
                if (elementCount == 0) cout << "Enter a non-zero integer, please..." << endl;
            }
            if (userInput == '1')
            {
                //get that many integers from the user
                data = vector<int>(elementCount, 0);     //initalizing data with elementCount amount of zeroes
                for (int x = 0; x < elementCount; x++)
                {
                    cout << "Enter value for data[" << x << "] (must be an integer): ";
                    cin >> data[x];
                }
            }
            else
            {
                //generate that many random integers
                //data = vector<int>(elementCount, 0);     //initializing data with elementCount amount of zeroes
                srand(time(0));     //seeding the random number generator
                for (int x = 0; x < elementCount; x++)
                {
                    //generating random number between 1 and 1000 and assigning it to data
                    data.push_back(rand() % 1000 + 1);
                }
            }
        }
        else
        {
            srand(time(0));     //seed the random number generator
            elementCount = rand() % 100 + 1; //specify a random elementCount for the data [1,100]
            for (int x = 0; x < elementCount; x++)
            {
                data.push_back(rand() % 1000 + 1);    //push a random number into data [1,1000]
            }
        }
        //make a binary search tree of all this data!
        BSTTest test(data);
        test.printBST();    //print the data
    }
    else
    {
        pressEnterToContinue();
    }
    return 0;
}


I have initially tried to say data = vector<int>(elementCount,0); , but changed it to exactly what I was doing in the random case (too bad I didn't have it to where the same code block executes for either case; I guess you can't have "the best of both worlds"), but for some reason, it still throws some exception! Does anyone have any idea why this might be happening?
closed account (28poGNh0)
I think the problem occurs when you choose the cases 1 and 2 ,the reason for that is that you did not initial elementCount which takes at the first place a huge number

I tried to think about what are you thinking and I think! You want something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# include <iostream>
# include <vector>
# include <ctime>
# include <cstdlib>
# include <conio.h>
using namespace std;

class BSTTest
{
    vector<int> myVect;
    public :
        BSTTest(vector<int>&l){myVect = l;}
        void printBST();
};

void BSTTest::printBST(void)
{
    for(size_t i=0;i<myVect.size();i++)
        cout << myVect[i] << endl;
}

void pressEnterToContinue()
{
    while(getch()=='\n')
        return ;
}

int main()
{
    bool keepLooping = true;    //this will make the program keep asking for a choice when a valid one is not entered
    bool exitCommand = false;   //this will keep track of the user wanting to exit the program
    char userInput;
    vector<int> data;
    int elementCount = 5;   //the amount of elements in data
    cout << "Binary Search Tree Creator" <<endl;
    cout << "==========================\n" << endl;
    while (keepLooping)
    {
        cout << "How would you like to create a binary search tree?" << endl;
        cout << "(1) Direct input" << endl;
        cout << "(2) Random numbers (you decide how many)" << endl;
        cout << "(3) Random numbers (random amount of random numbers" << endl;
        cout << "(0) Exit this program" << endl;
        cout << "\nPlease enter a selection: ";
        userInput = getche();  //attempt to get only one character from the user
        //exit the loop on a valid condition
        if ((userInput >= '0') && (userInput <= '3')) keepLooping = false;
        if (userInput == '0') exitCommand = true;
    }

    if (userInput > '0')
    {
        if (userInput < '3')
        {
            //get the specified elementCount from the user
            while (elementCount <= 0)
            {
                cout << "How many random numbers should be in the data? ";
                cin >> elementCount;
                if (elementCount == 0) cout << "Enter a non-zero integer, please..." << endl;
            }
            if (userInput == '1')
            {
                //get that many integers from the user
                data = vector<int>(elementCount, 0);     //initalizing data with elementCount amount of zeroes
                for (int x = 0; x < elementCount; x++)
                {
                    cout << "Enter value for data[" << x << "] (must be an integer): ";
                    cin >> data[x];
                }
            }
            else
            {
                //generate that many random integers
                //data = vector<int>(elementCount, 0);     //initializing data with elementCount amount of zeroes
                srand(time(0));     //seeding the random number generator
                for (int x = 0; x < elementCount; x++)
                {
                    //generating random number between 1 and 1000 and assigning it to data
                    data.push_back(rand() % 1000 + 1);
                }
            }
        }
        else
        {
            srand(time(0));     //seed the random number generator
            elementCount = rand() % 100 + 1; //specify a random elementCount for the data [1,100]
            for (int x = 0; x < elementCount; x++)
            {
                data.push_back(rand() % 1000 + 1);    //push a random number into data [1,1000]
            }
        }
        //make a binary search tree of all this data!
        BSTTest test(data);
        test.printBST();    //print the data
    }
    else
    {
        pressEnterToContinue();
    }
    return 0;
}
Last edited on
Holy crap, why didn't I see that by myself?! I guess I must have been spoiled by programming in Java too much; //Java auto-initializes EVERYTHING

std::cout << "I need to get back into my roots. Thankfully, that is where this numeric methods course is coming in handy." << std::endl;
Topic archived. No new replies allowed.