• C

structures containing thousands of entries


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.
Who is Participating?
KocilConnect With a Mentor Commented:
Use array of pointer to struct.

Looks like this in the memory

Array of *_user  
[0] -> [user 0]
[1] -> [user 1]
[2] -> [user 2]

You may have a big array of *user without wasting too much memory. You only malloc memory for _user data if needed.
You can sort and bsearch on the the array efficiently as usual;
typedef struct {
  int var1;
  int var2;
  char *arg;
} _user;

typedef struct {
  int number;
  int max;
  _user **users;   /* array of pointers to _user */
} _userlist;

int UserInit(_userlist* ul, int max)
  ul->number = 0;
  ul->users = malloc(sizeof(_user*));
  ul->max = max;

int UserAdd(_userlist* ul, _user* u)
  if (ul->number >= ul->max) realloc(ul->users, ul->max+10);
  ul->users[ul->number] = malloc(sizeof(_user));

  /* you may add feature to sort insert user here */
  *ul->users[ul->number] = *u;

/* compare user var1 */
int compvar1(_user* u1, _user* u2)
   int v1 = u1->var1;
   int v2 = u1->var2;
   return v1 == v2 ? 0 : (v1 < v2 ? -1 : 1);

_user* UserSearch(_userlist* ul, _user* key)
  return bsearch(key, ul->users, ul->number,
    sizeof(_user*), comparevar1);  
int main() {
  _userlist ulist;
  _user user = {....};

  UserInit(&ulist, 1024);
  UserAdd(&ulist, &user);

  found = UserSearch(&ulist, &user) != NULL;
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...
rulerrAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.