Path: chuka.playstation.co.uk!news From: sosman@terratron.com (Steven Osman) Newsgroups: scee.yaroze.beginners Subject: Re: A Problem with Malloc and a question about InitHeap Date: Mon, 13 Dec 1999 05:27:38 GMT Organization: PlayStation Net Yaroze (SCEE) Lines: 78 Message-ID: <385482c4.380848671@news.playstation.co.uk> References: <3851a5b1.5208772@news.playstation.co.uk> <3852CCEA.714FF3B2@identicalsoftware.com> NNTP-Posting-Host: 209.27.57.69 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Newsreader: Forte Agent 1.5/32.452 Incidentally, at the cost of 4 bytes of memory you can make it faster... void AddCamera(Camera ** listStart, Camera **listEnd) { Camera *newCamera=(Camera *) malloc(sizeof(Camera)); if (*listEnd) { *listEnd->next=newCamera; } else { *listStart=newCamera; } *listEnd=newCamera; ReadCamera(newCamera); } Just keep the start and end pointers of the list (make sure they are both initialized to NULL). Steven On Sat, 11 Dec 1999 22:15:06 +0000, Dennis Payne wrote: >> void AddCamera(Camera* list) >> { >> while( list!= NULL ) list=list->next; >> list = (Camera*) malloc( sizeof(Camera) ); >> ReadCamera(list); >> } > >The code is wrong (I think). To do the equivelent of the macro: >void AddCamera(Camera** list) >{ > while( (*list)!= NULL ) (*list)=(*list)->next; > (*list) = (Camera*) malloc( sizeof(Camera) ); > ReadCamera(*list); >} > >I don't think this is what you want though. This iterates through >the list but does not add the item to the list. It also destroys >the pointers to the list in the process. Here's what I think >you want: > >void AddCamera(Camera** list) >{ > Camera *prev; > if ((*list) == NULL) > { > (*list) = (Camera*) malloc( sizeof(Camera) ); > ReadCamera(*list); > } > else > { > prev = *list; > while( prev->next != NULL ) prev=prev->next; > prev->next = (Camera*) malloc( sizeof(Camera) ); > ReadCamera(prev->next); > } >} > >(In C++ you would pass in a "Camera *&list".) > >If you need an explaination of what this is doing let me know. > >> BTW - InitHeap seems to have no effect. Oh and one last thing how do >> you reclaim the memory after you have loaded your files into video >> memory or into their structures(I parse a raw binary file also). > >After you're done with the files simply use the memory. For >example if you loaded all the graphic images then you could use >InitHeap with the memory addresses of where the images were stored. >(I have no experience with InitHeap so I don't know why it would be >a problem.) > >Dennis Payne >dulsi@identicalsoftware.com