891 Subj : Re: Pointers + Stuff To : borland.public.cpp.borlandcpp From : Alistair Date : Sun Jul 04 2004 08:30 pm Bob, Works great, thanks! You were right, it does return garbage as the object is no longer! Found this out the hard way and its nice to see someone explain it! Cheers Alistair "Bob Gonder" wrote in message news:pm9ge052di2qctgs9fpbgd6acdjm4b1tn9@4ax.com... > 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