0
나는 간단한 프로그램이 : 나는 초심자 CPP에있어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;
}
, 그리고 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