c++ - Access violation writing location 0xCCCCCCC? -
when try push_back element, gives me error "unhandled exception @ 0.x00d644ab in project.exe: 0xc000005: access violation writing location 0xccccccc." i've tried looking solution, can't seem find 1 relates me. tons.
sdal(){ t* list = new t(50); size1=50; numberofelements=0; } void push_back( const t& element ) { numberofelements++; if(numberofelements>=size1-1){ expandarray(); } list[numberofelements-1]=element; }
the error points "list[numberofelements-1]=element;" when debug it.
the full code here:
template <typename t> class sdal { private: int size1; int numberofelements; t* list; public: sdal(){ t* list = new t[50]; size1=50; numberofelements=0; } sdal(int x){ t* list = new t[x]; size1=x; numberofelements=0; } void expandarray(){ int tmpsize = size1; size1=(int)((double)size1*1.5); t* tmp = new t[size1]; for(int x=0; x<tmpsize; x++){ tmp[x]=list[x]; } list=tmp; } void push_back( const t& element ) { numberofelements++; if(numberofelements>=size1-1){ expandarray(); } list[numberofelements-1]=element; }
visual c++ fills uninitialized memory 0xcccccccc in debug-mode, because that's more caught. trapping on access 0xcccccccc should quite useful hint.
your error simple:
you allocating memory in ctor store containers elements, but:
- you allocate enough 1 element.
- you assign temporary instead of member.
using still uninitialized pointer naturally trap on first access.
Comments
Post a Comment