From: tim Newsgroups: comp.os.msdos.djgpp Subject: Re: Linked lists with DJGPP Date: 31 Aug 1997 01:34:34 GMT Organization: H2O Lines: 77 Message-ID: <5uahna$sbv@nr1.calgary.istar.net> References: <3406E97E DOT 35F6 AT club-internet DOT fr> Reply-To: No AT delorie DOT com, More AT delorie DOT com, Spam AT delorie DOT com NNTP-Posting-Host: pm306.spots.ab.ca Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit CC: ludvig AT club-internet DOT fr To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk ludvig wrote: > > Hi I'v left my Borland4.52 for DJGPP and I'm not used to protectedmode > programming so I have just a little question... > > What I have understood is that one can't allocate as many adress spaces > as you would wish even if they can be at almost any size, and as I would > have large (preferably generic) linked list, my question is: > Is the new/malloc functions being able to alloc for an example 1500 > different structures(they arn't all different, but some differs in size > and configuration, and the compiler wouldn't know anyway as it i dynamic > allocation I want) or do I have to allocate a huge memory block and > store all information by myselve? > > Thank you for reading this Ludvig Larsson > > ps. please send a copy to You could try something like this: /**************************************************/ struct StrA; typedef struct StrA { /* * data members */ void *data; /* additional data for this element */ struct StrA *next; /* pointer to the next element */ } StrA; void LoadStructures(void) { StrA *StrAhead; /* head of the linked list */ StrA *StrAcurrent; /* current element pointer */ StrA *StrAprev; /* previous element pointer */ long nBytesTotal; /* number of bytes to allocate */ long element; /* data element index */ char *d; /* general purpose pointer */ /* calculate the total number of bytes required */ nBytesTotal = CalculateTotalStorageRequirements(); /* allocate the memory */ StrAhead = (StrA *)malloc(nBytesTotal); if(!StrAhead) return; /* simple error check */ /* initialize the memory */ d = (char*)StrA; StrAprev = NULL; for(element=0;elementnext = (StrA *)d; } /* Initialize the current element */ StrAcurrent = (StrA*)d; StrA->next = NULL; /* place the data after the fixed portion of the structure */ StrA->data = (void*)((int)d + sizeof(StrA); /* load the data into the element */ StoreElementData(element,StrAcurrent->data); /* save the current pointer for the previous element */ StrAprev = StrAcurrent; /* advance the data pointer to the next element location */ d+=sizeof(StrA) + ElementDataSize(element); } }