Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > volatile struct in dot h vs dot c

Reply
Thread Tools

volatile struct in dot h vs dot c

 
 
Christopher M. Lusardi
Guest
Posts: n/a
 
      05-11-2004
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
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-11-2004
On 11 May 2004 07:33:32 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Case
Guest
Posts: n/a
 
      05-11-2004
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

 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      05-15-2004
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"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Dynamically resize visible text and append dot dot dot (like gmail subject lines.. ) how to question? jawolter@gmail.com Javascript 6 12-12-2005 04:09 PM
Use of the Volatile keyword for a pointer to a volatile memory block ben C Programming 5 01-11-2005 05:38 PM
Q: volatile struct elements or entire struct? Mark A. Odell C Programming 3 09-23-2004 12:50 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments