E
Evangelista Sami
Hi all
i have implemented a list type as an array of pointer like this
typedef struct {
int nb_elements;
void **elements;
} list;
to avoid having a pointer for each element as it is done with a recursive
definition.
the major drawback is that when i want to add an element at the end of the list,
i have to allocate an array of nb_elements+1 elements then copy all the elements
of the previous array to the new array like this
void append
(list *l,
void *new_element)
{
int i;
void **elements = (void **) malloc(sizeof(void *) * (l->nb_elements+1));
for(i=0; i<l->nb_elements; i++)
elements = l->elements;
elements = new_element;
l->nb_elements++;
free(l->elements);
l->elements = elements;
}
which is very time expensive.
can anyone tell me of a hint to avoid doing that or to optimize it.
note that my first aim is to save as much memory as possible and i didn't find
any solution which is cheaper in term of memory.
Thanks for any help
Sami Evangelista
i have implemented a list type as an array of pointer like this
typedef struct {
int nb_elements;
void **elements;
} list;
to avoid having a pointer for each element as it is done with a recursive
definition.
the major drawback is that when i want to add an element at the end of the list,
i have to allocate an array of nb_elements+1 elements then copy all the elements
of the previous array to the new array like this
void append
(list *l,
void *new_element)
{
int i;
void **elements = (void **) malloc(sizeof(void *) * (l->nb_elements+1));
for(i=0; i<l->nb_elements; i++)
elements = l->elements;
elements = new_element;
l->nb_elements++;
free(l->elements);
l->elements = elements;
}
which is very time expensive.
can anyone tell me of a hint to avoid doing that or to optimize it.
note that my first aim is to save as much memory as possible and i didn't find
any solution which is cheaper in term of memory.
Thanks for any help
Sami Evangelista