0
我有簡單的程序:C++的valgrind錯誤
class stack{
public:
void push(int a);
void pop();
int isempty();
void init();
void clear();
int* stos;
int size;
private :
int top;
};
void stack::init(){
this->top=0;
this->size=10;
this->stos= reinterpret_cast<int*>(malloc(sizeof(int)*size));
}
void stack::push(int a){
this->top++;
this->stos[top-1]=a;
if((this->top)>(this->size))
{
this->size=2*(this->size);
this->stos=reinterpret_cast<int*>(realloc(this->stos,sizeof(int)*(this->size)));
}
}
void stack::pop()
{
this->top--;
this->stos[this->top]=0;
}
void stack::clear(){
free(this->stos);
this->top=0;
}
int stack::isempty(){
if((this->top)!=0)
return -1;
else return 1;
}
int main(int argc, char** argv) {
stack s1;
s1.init();
s1.clear();
printf("%d",s1.stos[12]);
return 0;
}
我在CPP初學者,和Valgrind的返回這樣的錯誤:
==4710== Invalid read of size 4
==4710== at 0x80486D7: main (main.cpp:69)
==4710== Address 0x4325058 is 8 bytes after a block of size 40 free'd
==4710== at 0x402B06C: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4710== by 0x8048686: stack::clear() (main.cpp:51)
==4710== by 0x80486CF: main (main.cpp:68)
==4710==
如果存在不會是清楚()在主誤差函數將是相同的,但會說,40 alloc'd :) 我會很高興的任何幫助,謝謝。
感謝:P這只是從測試程序留下的東西:P我是愚蠢的 – user2511527