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
  • » Dynamic Array In Shared Memory

#1 2004-05-25 09:36 AM

guepard53
Member
Registered: 2004-05-12
Posts: 6

Re: Dynamic Array In Shared Memory

Hello !

In C, I want to put a dynamic array (*tab for example) in shared memory (shmget, shmat). It works but when the size of the array begins to grow, the writing in the array doesn't work... Can I make a 'malloc' of a dynamic array stored in shared memory ? Or something like that... (the size of the shared memory is big enough to work...)

Tanks (English's not my motherlanguage... :roll: )


Guépard

Offline

#2 2004-05-25 04:10 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: Dynamic Array In Shared Memory

Offline

#3 2004-05-26 08:30 AM

guepard53
Member
Registered: 2004-05-12
Posts: 6

Re: Dynamic Array In Shared Memory

struct myStruct { char text[100000]; unsigned long int size; };

Guépard

Offline

#4 2004-05-26 11:14 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Dynamic Array In Shared Memory

Offline

#5 2004-05-26 11:17 AM

guepard53
Member
Registered: 2004-05-12
Posts: 6

Re: Dynamic Array In Shared Memory

Yes, it's a typo fault, in my code it's correct (If not, I should have a error in compilation)  :roll:


Guépard

Offline

#6 2004-05-26 12:38 PM

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

Re: Dynamic Array In Shared Memory

Does your machine have enough free memory to handle as much
as you're allocating there?  It's quite a lot; roughly 100MB, it looks
like...  And, even if you have the free RAM, some systems will only
set aside a part of it for use as shared memory, too...  So, maybe
you're simply exceeding the limits of available memory...  (And, as
for why the initial allocation didn't fail, perhaps for the same reason
that on many systems a malloc() of an insanely huge amount won't
fail, but later trying to actually access all of it will seg-fault in the
same way...  Namely, some systems overcommit memory, in the
hope that you won't really need to use as much as you asked for,
so only try to actually give it all to you later, when you try to really
use it; at which point, they THEN can fail, if they're unable to give
it all to you...)

Or, it could be something completely different...  I don't know...

Offline

#7 2004-05-26 01:05 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: Dynamic Array In Shared Memory

The real problem is most likely that you don't get the shared memory in
the first place and just skip the error check. My system wouldn't give me
1000 of your "mystruct" either. So you tried to write at the address -1 and
that will cause a segfault...

Always check for errors if you create a resource to prevent running into
such things.

You try to get 100004Bytes*1000=100004000 Bytes or ~100MByte. Most
likely that your shared mem won't give you that. If you really need such a
big shared memory area, use a file together with mmap, that should work
and in the end it'll be almost as fast. Several programs using mmap on the
same file share the mmaped memory area unless you specify otherwise,
and you can use spin locks to organise the access...

Offline

  • Index
  • » C
  • » Dynamic Array In Shared Memory

Board footer

Powered by FluxBB