On Thu, 3 Jul 2008 09:29:37 -0700 (PDT), "hu**********@gmail.com"
<hu**********@gmail.comwrote:
struct {
int num;
int num2;
struct b arrayOfB[SIZE];
} a;
struct {
int num3;
int num4
} b;
<snip shared-memory>
Aside: you used sizeof(struct b) where you needed (struct a).
The shared-memory part is offtopic as it is not standard in C; but
issues of laying-out and accessing data in arbitrary memory, whether
it happened to come from shm* or say malloc (a_zillion), is ontopic.
At least if the memory is suitably aligned in the first place, and
both <offtopicshmat and <ontopicmalloc are (for all types).
[now] I will obtain the SIZE in runtime, before ... shmget ...
struct {
int num;
int num2;
struct b *arrayOfB;
} a;
<snip>
my question is: how can tell arrayOfB to point over the shared memory?
As already answered, you don't need this change; you can use the
'struct hack' in C89, or the equivalent but standard 'flexible array
member' in C99, to get the same 'in-place' layout as above.
But if you WANT to change to a pointer:
struct a *ptr,p2*;
Syntax error; should be *ptr, *p2;
p2 = ptr; //ptr is the shmat return
p2++;
ptr->arrayOfB
You're close. With the corrected declaration above, this gives you p2
pointing just after the struct-a. IF that location is suitably aligned
for a struct-b, you can just do ptr->arrayOfB = (struct b*) p2;
In fact you don't need a separate variable, just
ptr->arrayOfB = (struct b*) (ptr+1);
/* or equivalently */ = (struct b*) &ptr[1];
Unfortunately there is no standard way to determine what alignment is
needed for a struct-b, or provided by a struct-a (the size of any type
in C must be a multiple of its required alignment, to make arrays
work), so this can be unsafe. For the particular structs you showed,
it is extremely likely that any requirement for struct-a will (also)
satisfy struct-b, but it isn't absolutely 100% portably guaranteed.
If you want to use a pointer and handle the possible alignment issue,
simplest to use a union which is guaranteed to align for either:
struct a * aptr = shmat (...);
union x { struct a a; struct b b; } * xptr = (union x *) aptr;
aptr->arrayOfB = (struct b*) (xptr + 1);
/* this is guaranteed located after the struct-a,
AND aligned for a struct-b. It may _possibly_ be
offset more than actually needed and waste some space,
but on most modern systems this isn't worth worrying about,
unless your (individual) structures are truly huge,
in which case post a more specific example
and we can get into the more arcane methods. */
- formerly david.thompson1 || achar(64) || worldnet.att.net