### 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):

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````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 data; int elementCount; //the amount of elements in data cout << "Binary Search Tree Creator" <= '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(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(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?
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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102`` ``````# include # include # include # include # include using namespace std; class BSTTest { vector myVect; public : BSTTest(vector&l){myVect = l;} void printBST(); }; void BSTTest::printBST(void) { for(size_t i=0;i data; int elementCount = 5; //the amount of elements in data cout << "Binary Search Tree Creator" <= '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(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(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.