Mail Archives: djgpp/1997/08/31/01:36:38
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 <ludvig AT club-internet DOT fr>
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;element<NumberOfElements();++element)
{
/* initialize the `next' pointer for the previous element */
if(strAprev)
{
StrAprev->next = (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);
}
}
- Raw text -