У меня простая программа:
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==
Если функция wont be clear() в основной ошибке будет такой же, но скажет, что 40 alloc'd :) Буду рад за любую помощь, спасибо.
Вы получаете доступ к памяти, которую вы не выделили, когда вы это делаете:
printf("%d",s1.stos[12]);
В первом случае вы вызываете s1.clear()
, который освобождает (де-распределяет) память для s1.stos.
Во втором случае (если вы избавитесь от clear()
), вы получаете доступ к 12-му элементу массива, для которого вы выделили только 10 элементов.