70b Subj : Re: Pointers + Stuff To : borland.public.cpp.borlandcpp From : Bob Gonder Date : Sun Jul 04 2004 10:05 am Alistair wrote: >struct s_Something >{ > void *data; > int length; > int datatype; //0 - image, 1 - wav, 2 - string, 3 - etc... > bool temp; >}; > >s_Something mySomething; >mySomething.data = malloc(80); >mySomething.length = 80; > >dosummet(&mySomething); >//in dosummet -> mySomething->data etc... > >s_Something *somethings[256]; > >so at the end of my main application loop I can loop the list of pointers, >getting the objects using memory and then see if they are temp (temporary) >and if so, i can free the memory. > >Can someone give me some idea how this can be done without a lot of >technical speak, I am new to C++ and I would like to get an understanding >without long words / phrases i dont yet understand! int MaxSomethig=0; s_Something *somethings[256]; s_Something mySomething; mySomething.data = malloc(80); mySomething.length = 80; somethings[MaxSomething]=&mySomething; MaxSomething++; while( MaxSomething>0 ) { MaxSomething--; free( somethings[MaxSomething].data); /* don't recall if it's '.data' or '->data' compiler will tell you. */ }; Well, that's what you asked for, but there are problems to this approach. If you create the s_Something in a subroutine or function, it is deleted when you return, and the pointer in somethings points to garbage, and the pointer in .data is lost as well. Might be better to fix it up a bit. s_Something *mySomething = malloc(sizeof(s_Something));; mySomething->data = malloc(80); mySomething->length = 80; somethings[MaxSomething]=mySomething; free( somethings[MaxSomething].data); free( somethings[MaxSomething]); . 0