我现在正在编写一个程序来改变一个C_String
使用指向字符串的指针。我有一个运行良好的实现。我遇到的唯一问题是,当我到达程序末尾时,如果我尝试删除指针,我会收到错误消息。
我的代码:
void CStringSwitcher()
{
string input = "";
char* cStringArray = new char[ASIZE];
char* reversed = new char[ASIZE];
const char* originalReversed = reversed;
char* pointers[POINTER_SIZE];
memset(reversed, '\0', ASIZE);
memset(cStringArray, '\0', ASIZE);
memset(pointers, '\0', POINTER_SIZE);
char* current = cStringArray;
cout << "Enter a sentence on each line. Input a 0 to stop." << endl;
// Receives input from the user and stores it into cStringArray
int i = 0;
do
{
cout << ">";
cin.clear();
fflush(stdin);
input = "";
getline(cin, input);
if (input == "0")
break;
else
{
input.append("\n");
pointers[i] = current;
_CRT_SECURE_STRCPY(pointers[i], ASIZE - 1, input.c_str());
current += input.length();
i++;
}
} while(i < POINTER_SIZE);
char* end = current;
--i;
do
{
/// Check if done
if(i < 0)
break;
/// Copy from current to end
current = pointers[i];
do
{
*reversed++ = *current++;
}while(current < end);
/// Update end
end = pointers[i];
/// Update i
--i;
}while(true);
*reversed = '\0';
cout << endl << originalReversed << endl;
system("PAUSE");
//delete[] originalReversed;
//delete[] cStringArray;
return;
}
正如上面所写的,代码工作正常,但是如果我在返回之前取消注释两个删除行,我会收到一个错误:
Project_06.exe 已启动断点
程序崩溃。奇怪的是我只是再次运行程序以获得错误消息的确切措辞并且它运行时没有错误?关于为什么会这样的任何想法?