我运行此代码后,拿到这本 “heap corruption detected
” 消息:检测到堆损坏| C++
uli& uli::operator =(char* n)
{
char* buffer = new char[strlen(n)];
char* p;
int op;
int coef;
strcpy(buffer, n);
while(*buffer)
{
op = strlen(buffer) - 5;
p = (op >= 0) ? op+buffer : buffer;
coef = atoi(p);
if(coef > 65535)
coef = atoi(++p);
push(head, coef);
*p = '\0';
}
delete buffer; // <- heap corruption detected
return *this;
}
这是我如何调用该方法:
uli x;
x = "9876123";
什么是 “检测堆损坏” 是什么意思?
使用'delete [] buffer'。 – 2012-03-04 03:02:32
你有没有使用一个对象来处理内存。也许是一个std :: string而不是char *。 – 2012-03-04 03:14:06
我不是那么好 – Jonas 2012-03-04 03:29:05