char* StrRsever(char *pStr)
{
size_t uiLen = strlen(pStr);
char *strarry1 = new char[uiLen+1];
size_t i = 0;
for (i = 0; i < uiLen; i++)
{
*(strarry1 + i) = *(pStr + uiLen - i - 1);
}
*(strarry1 + i) = 0;
pStr = strarry1;
delete strarry1; //為什麼delete這個報錯,不delete就沒事?
return pStr;
}
pStr = strarry1;
delete strarry1; //為什麼delete這個報錯,不delete就沒事?
return pStr;
你的pStr指向的strarry1的空間,同時函數返回出去給外面的調用,你delete釋放掉。會導致後面訪問出錯。你應該再加一個函數deleteresource等來delete strarry1;
當外面的調用者確定不需要StrRsever返回的指針的時候調用deleteresource函數