C++动态内存分配造成损坏的堆
不寻找在这一个答案,但一些方向将不胜感激。我所看到的所有地方以及我尝试过的所有答案都不能解决问题。C++动态内存分配造成损坏的堆
我有指令声明“动态分配变量到用户输入的内容,不能使用placeHolder变量(userInput [256}])来捕获输入。
我原来的代码是:
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;
}
我知道堆被损坏,我从我改变了分配内存的方式怀疑。我不确定为什么修复程序无法正常工作,所以这是所有的教程。
下面是我想象与your rubber duck您的通话将走在这一点上:
int num = 10;
int ptrLength = num;
userInput = new char[ptrLength];
你(说你的橡皮鸭子):好了,上面有哪些归结为 的是,我被分配10个字符的缓冲区。
userInput
这里 指向十个字符,userInput[0]
到userInput[9]
。橡皮鸭:好的。
你说:这两个
num
和ptrLength
被设置为10橡胶鸭的价值:对我来说很有意义。
while (cin.peek() != '\n'){
cin >> userInput[ptrLength];
您:那么,我是否读下一个字符是一个换行符,如果没有,我 放在
userInput[ptrLength]
橡胶鸭输入:等等,什么是初始值
ptrLength
?你:10,正如我刚才所说的那样。
橡胶鸭:但你不是刚说,你只有
userInput[0]
通过userInput[9]
,分配给您的缓冲区,并写入 东西userInput[10]
,在这一点上,会破坏堆。
那么,你在这里对你的橡皮鸭的问题有什么回答?
-
userInput[ptrLength]
超出范围,不得在userInput = new char[ptrLength];
之后访问。 - 条件
ptrLength = num
不是一个平等测试,但一个任务,我想这不是你想要的。 - 您忘了在阅读后更新
ptrLength
。 - 您删除了新分配的缓冲区,并使其不可用。
- 您应该删除通过
new
创建的任何内容。 - 必须使用
delete
之前 assgningnullptr
。对于通过new[]
分配的内容,还可以使用delete[]
。 - 经过
num = num*num;
,num/2
一般不会在先num
。你必须计算平方根,从新的num
获得oidnum
。
校正代码:
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
void myVowels(const char *userInput, int ptrLength){
cout << "myVowels(" << userInput << ", " << ptrLength << ")\n";
}
int main(){
char cont = 'y';
char *userInput = nullptr;
while (cont == 'y' || cont == 'Y')
{
int num = 10;
int ptrLength = 0;
userInput = new char[num];
char *temp = nullptr;
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++];
if (ptrLength == num){
int oldNum = num;
num *= num;
temp = new char[num];
for (int i = 0; i < oldNum; i++)
{
temp[i] = userInput[i];
}
delete [] userInput;
userInput = 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;
delete [] userInput;
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
system("pause");
return 0;
}
谢谢。 Duck Conversation帮助我们指出了我的方式中的其他错误。我能够专注并使代码工作超越可擦除性。真的很感谢评论。 – new2Me
'(X^2)/ 2 = x'和'(ptrLength = NUM)=(ptrLength == NUM)'!。并且你释放你的原始和新的字符缓冲区。也许橡皮鸭会帮助。 –
@JamesRoot'(x^2)!=(x * x)'in C++ – MikeCAT