Hi, I'm back again with my mastermind program. It compiles, but when I attempt to run it,
bad_alloc
is thrown. I attempted to debug it with
nothrow
, but it's replaced by the backwards-incompatible
__nothrow
in Visual Studio. (Express 2012 on Windows 7) I know the problem occurs somewhere in the constructor of the menu class, but I can't determine the exact location. From what I've researched,
bad_alloc
is thrown when the program runs out of memory, which may somehow be happening in the loop. I don't delete the array here as I need it for another function. Though I do so in the destructor, I doubt it pertains to the issue as the object can't even get through it's own constructor, none the less go out of scope.
This is the menu class' constructor...
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
|
menu::menu(int constructor_number_list_items, std::string title, std::string constructor_input_prompt, std::string constructor_bad_input, ...) {
va_list prompt_data; /* in the form of:
* - std::string : a handle string to the list item
* - std::string : the list item
* - int : the return value of "menu::return_input" if the input points to the respective list item
*/
number_list_items = constructor_number_list_items;
prompt += title + "\n"; // The prompt will include the title...
va_start(prompt_data, constructor_input_prompt);
a_list_item_data_ptr = new list_item_data[number_list_items]; // initializing an array of list item data w/ an element for each list item
for(int list_item = 0; list_item < number_list_items; list_item++) { // for each list item:
std::string buffer; // the first argument of each list item
buffer = va_arg(prompt_data, std::string);
prompt += " " + buffer + ". "; // ...title, (looped) a 4-space indent, a handle string, a period, another 4-space indent...
a_list_item_data_ptr[list_item].string_handle = buffer; /* initializing the string handle of the list item data of the respective list
* item w/ the first argument
*/
prompt += va_arg(prompt_data, std::string) + "\n"; // ...indent, and a list item.
a_list_item_data_ptr[list_item].return_value = va_arg(prompt_data, int); // [...] the return value [...] the next argument
}
va_end(prompt_data);
input_prompt = constructor_input_prompt;
bad_input = constructor_bad_input;
}
|
...and this is how the first object is initialized:
|
menu main_menu(3, "Main Menu:", "input: ", " error : invalid input\n\n", 1, "New Game", 1, 2, "Exit", 2, 3, "About", 3); // an object of "menu" for the main menu
|
Part of the header file:
1 2 3 4 5 6 7 8 9 10
|
struct list_item_data {
std::string string_handle;
int return_value;
};
list_item_data * a_list_item_data_ptr;
// ...
menu(int, std::string, std::string, std::string, ...);
|
I also tried using smart pointers, but with little success, as I was unable to find the header file.
Also, about the destructor, is this the correct way to free the memory?
1 2 3 4 5 6
|
menu::~menu() {
for(int element = 0; element < number_list_items; element++)
delete &(a_list_item_data_ptr[element]);
delete[] a_list_item_data_ptr;
}
|
Thanks.