'Repeated Expansion of Dynamic String Array Breaks When Array Size >= 3 [closed]
I am working on a project for a class I am in, and I am at a loss as to why it works at first but then stops working once arraySize hits 3.
The point of the program is to have a list of strings, where you can add or remove elements using dynamic strings. The program should loop indefinitely until the user inputs 9 to exit.
But, once the user inputs a string while arraySize is 3 or above, it quits. The program is not complete, but in order to move on, I need to know what I am doing wrong.
Note: The professor doesn't want us using vector, and the function prototypes are the ones that were given.
#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
int choice, size, i, item, count, arraySize, elementsToAdd;
string array, newArray, oldArray, newElement;
bool operationSuccess;
string arr[1];
void printMenu();
void menuInputValidation(int &choice);
void inputValidation(int choice, string &newElement);
string *expand(string *oldArray, int &arraySize);
void printList(string *arr, int count);
bool insertAtEnd(string *arr, string newElement, int arraySize);
int main()
{
cout << "Please choose from the following menu choices" << endl;
do
{
printMenu();
cin >> choice;
menuInputValidation(choice);
//Choices
if(choice == 1)
{
//Insert a new element(s) to the end of the list
expand(arr, arraySize);
cout << "Please input element to add to the end of the list." << endl << ">";
cin.ignore();
cin.clear();
cin >> newElement;
inputValidation(choice, newElement);
operationSuccess = false;
insertAtEnd(arr, newElement, arraySize);
if(operationSuccess == 0)
cout << "Error. Operation failed. " << endl;
if(operationSuccess == 1)
cout << endl << "Element successfully added." << endl;
}
else if(choice == 8)
{
//Print the contents of the list
printList(arr, count);
}
else if(choice == 9)
{
cout << "Exiting.";
}
if(choice != 9)
{
cout << "What would you like to do next?" << endl;
}
}
while(choice != 9);
return 0;
}
//Functions
void printMenu()
{
cout << "1) Insert a new element to the end of the list." << endl;
cout << "2) Insert a new element at the beginning of the list." << endl;
cout << "3) Insert an element into the list at a given index." << endl;
cout << "4) Remove an element from the end of the list." << endl;
cout << "5) Remove an element from the beginning of the list." << endl;
cout << "6) Remove an element from the list at a given index." << endl;
cout << "7) Sort the list." << endl;
cout << "8) Print the contents of the list." << endl;
cout << "9) Exit." << endl;
cout << ">";
}
void menuInputValidation(int &choice)
{
while(choice < 1 ||choice > 9)
{
cout << "Please input a number between 1 - 9. ";
if(choice != 0)
cout << "Enter 0 to see menu." << endl << ">";
else
cout << endl << ">";
cin.clear();
cin.ignore();
cin >> choice;
if(choice == 0)
{
printMenu();
}
}
}
void inputValidation(int choice, string &newElement)
{
if(choice == 1 || choice == 2)
{
while(cin.fail())
{
cout << "Please only input letters " << endl << ">";
cin.ignore();
cin.clear();
cin >> newElement;
}
}
else if (choice)
{
}
}
string *expand(string *oldArray, int &arraySize)
{
arraySize += 1;
string *newArray = new string[arraySize];
newArray = oldArray;
return newArray;
delete[] newArray;
}
void printList(string *arr, int count)
{
for(i = 0; i < arraySize; i++)
{
cout << *(arr + i) << endl;
}
cin.ignore();
cout << "Press enter to continue. ";
cin.get();
}
bool insertAtZero(string *arr, string newElement, int count)
{
return operationSuccess;
}
bool insertAtEnd(string *newArray, string newElement, int arraySize)
{
cout << ">" << arraySize << "<";
*(newArray + arraySize - 1) = newElement;
return operationSuccess = true;
}
Solution 1:[1]
Your main bug in your expand function is here: newArray = oldArray; This sets the newArray pointer to point to the same location as the oldArray pointer and in the process it throws away the pointer to the new array you just allocated which leaks the memory allocated because there is no way to free that allocation since you no longer have a pointer that points to it.
Instead of setting newArray to point to the same location as oldArray you need to copy the elements from the oldArray to the new array.
string *expand(string *oldArray, int &arraySize)
{
string *newArray = new string[arraySize+1];
// Copy the array elements from the old to the new
for(int i=0; i < arraySize;++i) {
newArray[i] = oldArray[i];
}
arraySize++;
delete[] oldArray;
return newArray;
}
You had an additional bug in the last 2 lines of expand():
return newArray;
delete[] newArray;
The delete here will never be executed because once execution hits a return the function is finished. This is a good thing since if it would have executed the delete[] newArray; this would have freed the same memory you return a pointer to.
As @PaulMcKenzie just commented the initial string arr[1]; can not be used to pass to this function since this allocation was not allocated by new[] you can't use it with your expand function.
Instead you need:
string* arr = new string[1];
Instead of:
string arr[1];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
