volatile struct in dot h vs dot c

Discussion in 'C Programming' started by Christopher M. Lusardi, May 11, 2004.

  1. Hello,

    Can you tell me is it my imagination, or should I not stick a "volatile"
    definition into a dot h file?

    My problem is if I put my declaration in main.c before main () my code
    works. But, if I move it to a dot h and include the dot h into main.c, I
    get an error when I run the program.

    I am using the volatile qualifier because I am using mmap.

    Any suggestions,
    Christopher Lusardi
    Christopher M. Lusardi, May 11, 2004
    #1
    1. Advertising

  2. Christopher M. Lusardi

    Leor Zolman Guest

    On 11 May 2004 07:33:32 -0700, (Christopher M. Lusardi)
    wrote:

    >Hello,
    >
    > Can you tell me is it my imagination, or should I not stick a "volatile"
    >definition into a dot h file?
    >
    > My problem is if I put my declaration in main.c before main () my code
    >works. But, if I move it to a dot h and include the dot h into main.c, I
    >get an error when I run the program.


    Your problem, whatever it is, is most likely due to the contents of the .h
    file being included in more than one translation unit. When the declaration
    is only processed once, in main.c, the name is only in scope there; if you
    include the .h file in other modules, you're introducing the name into
    those scopes as well, and something, somewhere, is changing in meaning.

    Or, you've got an unrelated bug and just re-arranging your code is enough
    to change the program's (mis-)behavior. Post the shortest complete program
    you can create that exhibits the problem, and we'll have something more to
    go on.

    >
    > I am using the volatile qualifier because I am using mmap.
    >
    > Any suggestions,
    > Christopher Lusardi


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, May 11, 2004
    #2
    1. Advertising

  3. Christopher M. Lusardi

    Case Guest

    Christopher M. Lusardi wrote:
    > Can you tell me is it my imagination, or should I not stick a "volatile"
    > definition into a dot h file?
    >
    > My problem is if I put my declaration in main.c before main () my code
    > works. But, if I move it to a dot h and include the dot h into main.c, I
    > get an error when I run the program.


    Moving code to an include file does not affect the
    meaning of this code in any way. All that happens
    is that the contents of the include file is literally
    pasted at the spot where you write #include ... So,
    simply a textual thing.

    There is no other way than that you have changed your
    code in some way in the process of creating the header
    file, or that you include the moved code at a different
    spot wrt the original code.

    If your code is not too long you could investigate the
    preprocessor (with #includes and #defines expanded) output
    of both versions: before and after moving to .h file.
    Many compilers have an -E switch to get this output.

    Good luck,

    Case
    Case, May 11, 2004
    #3
  4. Groovy hepcat Christopher M. Lusardi was jivin' on 11 May 2004
    07:33:32 -0700 in comp.lang.c.
    volatile struct in dot h vs dot c's a cool scene! Dig it!

    > Can you tell me is it my imagination, or should I not stick a "volatile"
    >definition into a dot h file?


    No. You should never put variable definitions in headers. Headers
    are for variable and function declarations, macro definitions and type
    definitions, not variable or function definitions.

    > My problem is if I put my declaration in main.c before main () my code
    >works. But, if I move it to a dot h and include the dot h into main.c, I
    >get an error when I run the program.
    >
    > I am using the volatile qualifier because I am using mmap.
    >
    > Any suggestions,


    Yes. Simply put the declaration in a header, but the definition in
    one and only one .c file.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, May 15, 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. Joe Pizzi

    placement of volatile in typedef of struct

    Joe Pizzi, Feb 7, 2004, in forum: C Programming
    Replies:
    5
    Views:
    3,962
    Leor Zolman
    Feb 7, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  3. Mark A. Odell

    Q: volatile struct elements or entire struct?

    Mark A. Odell, Sep 22, 2004, in forum: C Programming
    Replies:
    3
    Views:
    692
    Mark A. Odell
    Sep 23, 2004
  4. ben
    Replies:
    5
    Views:
    593
    Ulrich Eckhardt
    Jan 11, 2005
  5. Replies:
    6
    Views:
    245
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page