views:

24

answers:

1

what I am trying to do is represented in my other question with code.

Basically I need to keep in memory a table of elements (structs), there's no fixed number of elements that can exist, but it is small, but I still can't use an array.

And I don't want to use a linked list of elements because I don't want to keep adding and deleting elements everytime I need to change anything.

Instead what I want to do is allocate a chunk of memory with a single malloc, that chunk of memory will be large enough for say, 100 elements, and if in the rare case that I need more, I can allocate another chunk of 100 elements and link it to the original....

Is this a good idea? is there a name for this kind of structure? it's kinda of like dynamic expanding array? Do people actually use this? or I am just on crack? if this is bad idea, what do you recommend using instead?

Thanks

typedef struct Tb{
   POINT points;
   POINT *next;
} TABLE;

typedef struct Pt{
   int x;
   int y;
}POINT;


POINT *mypoints;
int a = 10;
int b = 1000;

mypoints = (POINT*) malloc (100 * sizeof(POINT));

for (int i =0; i < 100; i++) {
    mypoints->x = a++;
    mypoints->y = b++;
    ++mypoints;
}
A: 

Such allocation schemes have been used everywhere from the early Unix file system to Python's internal list allocation.

Code on!

msw
ok thanks, what is it called? any term I can use to find some other real world examples on google, perhaps some sample code?would it be a bad/good idea to add a POINT* next element to my POINT struct?
emge
@emge It is called a heap if you track it with a list or tree. A Vector if you have you have a dynamic array
Romain Hippeau