SPUG: C question ;)

Benjamin Franks benjamin at dzhan.com
Mon Mar 11 11:21:14 CST 2002


Yes, that's what I was essentially asking.  I wanted to make sure that it
was normal array/pointer stuff and nothing special had to be done because
it was shared memory.

Thanks for your help!
--Ben


On Sun, 10 Mar 2002, Scott Blachowicz wrote:

> On Sun, Mar 10, 2002 at 05:35:38PM -0800, Benjamin Franks wrote:
> > ...
> > int shmid=shmget((ftok("blah/blah",1), 1000*sizeof(my_struct), IPC_CREAT | 0666);
> >
> > Then when I attach the shmid to a pointer, do I want to attach a pointer
> > of the my_struct type, as in:
> >
> > my_struct *ptr;
> > ptr=shmat(shmid,NULL,0);
> >
> > I guess that's where my confusion lies.  Let's say I want to work on the
> > 500th element of my shared memory array of my_struct's.  How can I do it?
>
> Well...in C, an array name is kind of like a constant ptr and array indexing
> is just a form of pointer arithmetic, so once you have a pointer, you're in
> business.
>
>     /* Set all of the elements to the current time. */
>     for (i = 0; i < 500; i++) ptr[i].a = ptr[i].b = time(NULL);
>
>     /* All of these should be equivalent (unless I'm remembering wrong). */
>     (ptr+499)->a = 0;
>     (*(ptr+499)).a = 0;
>     ptr[499].a = 0;
>     499[ptr].a = 0;   /* !! */
>
> Is that what you wanted?
> --
> Scott Blachowicz
>


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org





More information about the spug-list mailing list