0

It's hard to tell my problem in the title. However,

I'm programming a program that use a structure database. In this structure I have information about the database and the element of the database itself. Each element is another structure. For now I declared the elements whit a simply array of elements whit a defined size. But each element is heavy (6 long double, a char[64], some int and soon much more) and I want to make the program able to work on a lot of (possibly infinte) element simultaneously, so it's necessary to use a lot of RAM. But only sometimes the program fills the array, so the program occupies a lot of RAM and works only whit few element. So I thinked to use a binary tree, so when i want to put another element I initialize a new element structure, and when i delete it the program dealloc the structure and I can have "infintie" element. But two problems hit me:

1) If I call a function that allocs a new structure and return the pointer to it, the structure isn't automatic deallocated because the function is finished 2) The program alloc and dealloc element of the structure whitout order, so the array is perfect because is enought a boolean flag "active" or "unactive" for each structure, but in a tree, the things being more complicated, I have to delete the node, then relink his children to another node that hasn't children...

What do you think? Every suggestion is taken with thanks.

1 Answer 1

0

If I understand your question correctly, you want to have a lot of struct element, where sizeof(struct element) is quite big, and you want to add and remove them dynamically during your program.

That means you want to allocate and deallocate memory for them dynamically, so you want to use malloc() and free() and you will be working with struct elem *s. Now the data structure to keep your pointers in depends on the operations you will perform on the data.

If you don't need to search in the elements (or if you never have a lot of them at one point), if it's enough just to iterate through them, a Linked list is appropriate. It allows you to dynamically add and remove elements and it is memory-efficient.

If you need to search in the elements, but using just one variable, you can put your pointers into a binary tree (or a hash table).

Otherwise (if you need to search in the data using multiple variables), you should probably keep them in linked list and construct binary-tree or hash-table index to speed-up searches over often searched variables.

But at that point, you might consider using some existing DBMS.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the great explaination. But if i call a function that alloc a variable, when the function return, can I access it?
Yes, if you do it right. See my other answer that happens to cover just this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.