UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Dyanamic malloc in shared memory.

#1 2008-12-11 09:55 AM

Jaysingh
Guest

Re: Dyanamic malloc in shared memory.

#2 2008-12-11 02:59 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Dyanamic malloc in shared memory.

Well, I'm not entirely sure of your situation, but it sounds to me like you want to just
allocate one large chunk of data, and then subdivide it yourself into separate areas
for your structs, rather than calling malloc() for each struct...  Eg:

char *big_buf = calloc (10000, sizeof (gInfoDB));
for (i = 0; i < 10000; i++)
    InfoDB[i].ctDB = (gInfoDB*)(big_buf + (i * sizeof (gInfoDB)));

Of course, since your "gInfoDB" struct ALSO contains yet another pointer, you'll also
need to do the same trick for that, for the "hash_t"...  Is there some reason you NEED
those to be pointers?  If not, it'd probably be a lot easier to just make that an actual
"hash_t" in there instead of a pointer to it...  (Unless it's some kind of opaque type
that you don't know the true size of, hence the need for a pointer to it...)  Alternatively,
if that's all the "gInfoDB" struct really contains, then why bother having it exist as a
struct anyway?  In other words, why not have "InfoDB" contain the "hash_t" pointer
directly, without the need for "gInfoDB" at all?

Offline

#3 2008-12-11 03:03 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Dyanamic malloc in shared memory.

Oh, and I spaced on the shared memory bit...  So, just replace calloc() with whatever
you use to get a chunk of shared memory (mmap(), shmat(), whatever)...

Offline

  • Index
  • » C
  • » Dyanamic malloc in shared memory.

Board footer

Powered by FluxBB