Не ища ответа на этот вопрос, но какое-то направление будет оценено. Везде, где я смотрел, и все ответы, которые я пробовал, не решают проблему.Распределение динамической памяти C++, вызвавшее поврежденную кучу
У меня есть инструкции, в которых говорится: «Динамически выделять переменную точно, что вводит пользователь. Невозможно использовать переменную placeHolder (userInput [256}]), чтобы поймать вход. Append '\ 0' to end."
Мой первоначальный код:
int main(){
char cont = 'y';
char *userInput = nullptr;
while (cont == 'y' || cont == 'Y')
{
int ptrLength = 0;
userInput = new char[ptrLength];
cout << "Please enter a word or phrase: ";//2. Asks the user to enter any string (any sequence of characters)
while (cin.peek() != '\n'){
cin >> userInput[ptrLength];
ptrLength++;
}
//1. You must use a pointer to a C-string and dynamically allocate just enough memory to store all the characters entered by the user PLUS the ‘\0’ char than must be appended at the end of the C-string.
userInput[ptrLength] = '\0';
cout << endl;
myVowels(userInput, ptrLength);
cout << endl << endl;
//delete [] userInput; //deleting here breaks the program. Not sure why right now.
//userInput = nullptr;
//5. The user must be asked if he/she wants to continue entering values or quit.
cout << endl << "To enter another phrase press Y. To exit press any key." << endl;
cin >> cont;
cin.clear();
cin.ignore(256, '\n');
}//end while cont = Y
delete userInput;
userInput = nullptr;
system("pause");
return 0;
}
Обновленный код:
int main(){
char cont = 'y';
char *userInput = nullptr;
while (cont == 'y' || cont == 'Y')
{
int num = 10;
int ptrLength = num;
userInput = new char[ptrLength];
char *temp = nullptr;
cout << "Please enter a word or phrase: ";//2. Asks the user to enter any string (any sequence of characters)
//FIX I FOUND, BUT IT DOES NOT WORK AT ALL
while (cin.peek() != '\n'){
cin >> userInput[ptrLength];
if (ptrLength = num){
num *= num;
temp = new char[num];
for (int i = 0; i < num/2; i++)
{
temp[i] = userInput[i];
}
delete [] userInput;
userInput = temp;
delete [] temp;
}
}
//1. You must use a pointer to a C-string and dynamically allocate just enough memory to store all the characters entered by the user PLUS the ‘\0’ char than must be appended at the end of the C-string.
userInput[ptrLength] = '\0';
cout << endl;
myVowels(userInput, ptrLength);
cout << endl << endl;
//userInput = nullptr;
//delete [] userInput; //This works, but by switching to nullptr I am not deleting the memory allocated. If I just have the delete with or without [] the program breaks. Tried with user input declared inside and outside of the WHILE statement. Heap is being corrupted.
//5. The user must be asked if he/she wants to continue entering values or quit.
cout << endl << "To enter another phrase press Y. To exit press any key." << endl;
cin >> cont;
cin.clear();
cin.ignore(256, '\n');
}//end while cont = Y
delete userInput;
userInput = nullptr;
system("pause");
return 0;
}
Я знаю, что куча коррумпируется, я подозреваю, что от того, как я меняюсь выделенную память. Я не уверен, почему исправление не работает во всех учебниках таким образом.
'(х^2)/2 = x' и' (ptrLength = Num) = (ptrLength == Num) '!. И вы освобождаете как исходные, так и новые буферы символов. Может быть, резиновая утка поможет. –
@JamesRoot '(x^2)! = (X * x)' в C++ – MikeCAT