Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays of structs in shared mem

Status
Not open for further replies.

bobbyjones01

Programmer
Aug 15, 2001
2
CA
Hi,

I'm trying to store an array of structures in shared memory... and I can't figure out how to do it! I've done enough programming with shared memory in Unix that I *thought* I knew how it all worked (guess not) . So if anyone can give me an example storing an array of structures in an area of shared memory (using the shxxx functions, not mapped memory) I would be very appreciative!

Thanks,
Bob.
 
Think about how you would do it with malloc()...


typedef _myStruct {
float number ;
int anotherNumber ;
char aString[ 80 ] ;
FILE *openFile ;
etc... ;
} myStruct ;

...

int N = 44 ; /* ?????? */
myStruct *self ; /* pointer to your array of any size */

self = (myStruct*)malloc (sizeof(myStruct) * N) ;

... well shmat works the same way..

int id ;

id = shmget( key, sizeof(myStruct) * N, mode ) ;
self = (myStruct*)shmat( id ) ;

... in either case you reference the elements of the array as follows ...

for (i=0 ; i<N ; i++) {
self[ i ].number = 3.14159 ;
self[ i ].anotherNumber = 3 ;
etc...
}


Hope this helps,

Brudnakm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top