私は簡単なプログラムを持っています:
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==
メインエラーに clear() 関数がない場合は同じになりますが、40 alloc'd と表示されます :) どんな助けにも感謝します。