Q: volatile struct elements or entire struct?

Discussion in 'C Programming' started by Mark A. Odell, Sep 22, 2004.

  1. If I have a structure that may point to a volatile "region (e.g. device)"
    or a context in memory what would be the best way to use the volatile
    keyword?

    E.g.

    a) volatile on struct objects

    struct Foo
    {
    unsigned char command;
    unsigned char status;
    unsigned long baseAddr;
    unsigned long addrRange;
    }

    volatile struct Foo *pDevFoo = (some dev. address);
    struct Foo hostFoo;


    - or -


    b) volatile on struct members

    struct Foo
    {
    volatile unsigned char command;
    volatile unsigned char status;
    volatile unsigned long baseAddr;
    volatile unsigned long addrRange;
    }

    struct Foo *pDevFoo = (some dev. address);
    struct Foo hostFoo; /* live with extra accesses */


    Does my use of volatile in a) even do what I intend? That is, make
    accesses to the device volatile?

    Thanks.

    --
    - Mark ->
    --
     
    Mark A. Odell, Sep 22, 2004
    #1
    1. Advertising

  2. Mark A. Odell

    Chris Torek Guest

    In article <news:Xns956C9FD7F9153CopyrightMarkOdell@130.133.1.4>
    Mark A. Odell <> wrote:

    [which of these should one use:]

    >a) volatile on struct objects
    >
    >struct Foo {
    > unsigned char command;
    > unsigned char status;
    > unsigned long baseAddr;
    > unsigned long addrRange;
    >}
    >
    >volatile struct Foo *pDevFoo = (some dev. address);
    > struct Foo hostFoo;


    This is the version I prefer, for several reasons, the most valid :)
    being the one shown right here in your example: you can make a
    non-volatile version of it.

    >b) volatile on struct members
    >
    >struct Foo
    >{
    > volatile unsigned char command;
    > volatile unsigned char status;
    > volatile unsigned long baseAddr;
    > volatile unsigned long addrRange;
    >}
    >
    >struct Foo *pDevFoo = (some dev. address);
    >struct Foo hostFoo; /* live with extra accesses */
    >
    >Does my use of volatile in a) even do what I intend? That is, make
    >accesses to the device volatile?


    Yes. Both methods work (provided, of course, that your implementation's
    definition of "volatile" does what you need in the first place --
    if not, neither method works anyway!).
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Sep 23, 2004
    #2
    1. Advertising

  3. Chris Torek <> wrote in
    news::

    > In article <news:Xns956C9FD7F9153CopyrightMarkOdell@130.133.1.4>
    > Mark A. Odell <> wrote:
    >
    > [which of these should one use:]
    >
    >>a) volatile on struct objects
    >>
    >>struct Foo {
    >> unsigned char command;
    >> unsigned char status;
    >> unsigned long baseAddr;
    >> unsigned long addrRange;
    >>}
    >>
    >>volatile struct Foo *pDevFoo = (some dev. address);
    >> struct Foo hostFoo;

    >
    > This is the version I prefer, for several reasons, the most valid :)
    > being the one shown right here in your example: you can make a
    > non-volatile version of it.


    Excellent.
    >
    >>b) volatile on struct members
    >>
    >>struct Foo
    >>{
    >> volatile unsigned char command;
    >> volatile unsigned char status;
    >> volatile unsigned long baseAddr;
    >> volatile unsigned long addrRange;
    >>}
    >>
    >>struct Foo *pDevFoo = (some dev. address);
    >>struct Foo hostFoo; /* live with extra accesses */
    >>
    >>Does my use of volatile in a) even do what I intend? That is, make
    >>accesses to the device volatile?

    >
    > Yes. Both methods work (provided, of course, that your implementation's
    > definition of "volatile" does what you need in the first place --
    > if not, neither method works anyway!).


    Oh it does, this has been heavily verified. Cross-compilers tend to this
    as one my hope for.

    Thanks Chirs.

    --
    - Mark ->
    --
     
    Mark A. Odell, Sep 23, 2004
    #3
  4. "Mark A. Odell" <> wrote in
    news:Xns956D59DBB7A4DCopyrightMarkOdell@130.133.1.4:


    > Oh it does, this has been heavily verified. Cross-compilers tend to this
    > as one my hope for.

    ^^
    might

    --
    - Mark ->
    --
     
    Mark A. Odell, Sep 23, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Lipp
    Replies:
    1
    Views:
    413
    Patrick TJ McPhee
    Jan 30, 2004
  2. Joe Pizzi

    placement of volatile in typedef of struct

    Joe Pizzi, Feb 7, 2004, in forum: C Programming
    Replies:
    5
    Views:
    4,097
    Leor Zolman
    Feb 7, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,422
    Chris Fogelklou
    Apr 20, 2004
  4. Christopher M. Lusardi

    volatile struct in dot h vs dot c

    Christopher M. Lusardi, May 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    495
    Peter Shaggy Haywood
    May 15, 2004
  5. ben
    Replies:
    5
    Views:
    611
    Ulrich Eckhardt
    Jan 11, 2005
Loading...

Share This Page