当您去删除指针时,第一个示例不起作用。当我添加空终止符或没有它时,程序要么挂起,要么我得到:
Debug Assertion Failed Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
来自 Visual Studio 2008
//Won't work when deleting pointer:
char *at = new char [3];
at = "tw"; // <-- not sure what's going on here that strcpy does differently
at[2] = '\0'; // <-- causes program to hang
delete at;
//Works fine when deleting pointer:
char *at = new char [3];
strcpy(at,"t");
at[1] = 'w';
at[2] = '\0';
delete at;
那么当我使用双引号而不是 strcpy 时会发生什么?它们都将完美地输出字符串,并且调试器不会显示任何不同。