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!

using array of struct in shared memory

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,
i have a question regarding using an array of structs in shared memory

my problem is that on some reason there isn't enough memory reserved
this is wat i do...

NUMBER 100
define a struct test
define MEMSEG sizeof(struct)*NUMBER

in main()
struct test record * record[NUMBER]
shmget(bla);
* record = ( struct test *)shmat(id, 0, 0);

when i loop through the number of records i only get
4 i expect a 100 could someone tell me what
i can do to correct this problem

thanks it's puzzeling me for some time

Robert



 
Robert,

record is declared as an array of pointers to struct test (i.e. [] binds before *). What you want is a pointer to an array of type struct test. Change the following:
Code:
struct test *record ;
record = (struct test*)shmat(....) ;
Then access the elements of the array as follows:
Code:
for (i=0 ; i<NUMBER ; i++) {
  record[ i ] = ....... ;
}

brudnakm.
 
Also, to get right size you have to use ALIGMENT.
Use next macros:

/* This macro is from the book &quot;C Pointers and Dynamic Memory Management&quot;,
page 190, by M.C.Daconta, John Wiley & Sons, Inc., 1993 */
#ifndef DEFAULT_ALIGNMENT
struct fooalign { char c; double d; };
#define DEFAULT_ALIGNMENT ((char*)&((struct fooalign*)0)->d - (char*)0)
#endif

#ifndef ALIGNED
#define ALIGNED(x) ( ((x) > 0 && (x) <= DEFAULT_ALIGNMENT) ? (DEFAULT_ALIGNMENT) : (((x)/DEFAULT_ALIGNMENT)*DEFAULT_ALIGNMENT) + (((x)%DEFAULT_ALIGNMENT)*DEFAULT_ALIGNMENT) )
#endif

And to get right structure size use:
nSize=ALIGNED(NUMBER * sizeof(struct));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top