Link to home
Start Free TrialLog in
Avatar of rulerr
rulerr

asked on

structures containing thousands of entries

hi,

i am creating a web program which will need to hold user information in a struct. I've thought about creating an array of structs, but this has some limitations that I don't want.  I am looking for a way to do something similar to this:
typedef struct {
 int var1;
 int var2;
 char *arg;
} _users;
_users *users;

int main() {
   users = calloc(1024, sizeof(_users));
}

again, im looking for alternatives. linked lists wont work because then i'd have to search through the linked list everytime i look for a user, and the userlist will be HUGE, and there will be too much time going through the whole list. i'm aware of hte several ways i could improve linked lists, but not really feeling up to it. looking for something more like users[id] where id = 1-1024, etc.

feel free to post your suggestions.
thanks.
Avatar of gj62
gj62

Not sure I understand - you are going to have to search through structs too (you can have a linked list of structs).

The nice think about linked lists is as you build them, you can keep it sorted on the fly, then bsearch it pretty easily.

I would recommend you consider a linked list of structs in sorted order...  If that's what you want, we can certainly provide the code to do it, it really isn't that much...
By the way, thousands really isn't that many, especially for a struct of that size - now, if we were talking about millions, then we'd have to give it some thought...
ASKER CERTIFIED SOLUTION
Avatar of Kocil
Kocil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rulerr

ASKER

Thanks.  I haven't thought of something like this, and it's exactly what I will implement. Sorry for not accepting previous answers, but this one suits me best.

again, thanks a lot.
Just out of curiousity:
Can you run your code with a C++ compiler? In that case, you can use the STL (Standard Template Library). The STL is pure heaven for these kind of issues. Totally flexible. You can either store the struct or a pointer to the struct in a number of different datastructures. For instance, when you need random access, traversing the complete linked list every time to find your item is slooow. In STL you would simply use a Map, or a Hash_map if you have many items and performing string-based look-ups. There is a whole bunch more.

I know the question is closed but I wouldn't want anybody to have to live without the STL :) STLPort.org has complete libs that work with about any compiler.