delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/11/10:15:43

From: Charles Terry <cterry AT plinet DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Dynamic memory allocation
Date: Sat, 11 Apr 1998 06:30:50 -0700
Organization: All USENET -- http://www.Supernews.com
Lines: 72
Message-ID: <352F708A.352E@plinet.com>
References: <352EDA1E DOT 7B0C55AF AT teccart DOT qc DOT ca>
NNTP-Posting-Host: 25012 AT 207 DOT 174 DOT 4 DOT 197
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

J-Réginald Louis wrote:
> 
> My question is not about DJGPP itself, but a general C++ question.
> I'm writting a 3D engine. I have a class called 'Scene' holding a list
> of object to render.
> The list is consisting of an array of pointers to type 'Object'. Like
> this:
> 
> #define MAX_LIST    10            // example
> 
> stuct Scene
> {
>  Object *ObjList[MAX_LIST];
>  int AddObject(Object *obj);                // Add an object to the
> object list
>  int RemoveObject(Object *Obj);        // Remove the object from the
> object list
>  // Other data definition and method definition
> };
> 
> My problem is that the user/programmer can't add more than 10 object's.
> I think it's not
> "correct" to determine a specific array length.
> So, what I'm asking is how can I append and remove objects in the list
> dynamicaly?

Have you been to comp.lang.c++ ?

// #define MAX_LIST    10            // example
struct List{
Object*	curobject ;
list* nextlist ;
};

 stuct Scene
 {
  list *ObjList;
  char empty=TRUE;
   void  AddObject(Object *obj);                // Add an object to the

>  int RemoveObject(Object *Obj);        // Remove the object from the

>  // Other data definition and method definition
> };

void Scene::AddObject(Object*obj)
{
list * temp=ObjList;
list * newobject=new list;
newlist->curobject=obj;
newlist->nextlist=NULL;
if (empty){ObjList=newlist; empty=FALSE;}
else{
while (temp->nextlist|=NULL){
	temp=nextlist;}
temp->nextlist=newlist;	
}}}

The remove function is a little tougher so you get
that one.

Psuedocode:
step thru the list
compare the pointer in the list with the pointer you want to
remove
when you find it make the prev nextlist pointer point
to the current nextlist pointer.
delete the current list
check for empty.

Charles

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019