473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

volatile struct in dot h vs dot c

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
Nov 14 '05 #1
3 1925
On 11 May 2004 07:33:32 -0700, cl********@aol.com (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
Nov 14 '05 #2
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

Nov 14 '05 #3
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"?
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Rasmussen | last post by:
What is the difference in meaning and in consequences between struct S { volatile A* a; }; and struct S
4
by: dwaach | last post by:
Hi, I have something like. struct X {}; X ox; X* pox=&ox; X*& volatile r =pox;
5
by: Joe Pizzi | last post by:
Is there any difference between the following two definitions? If so, can you explain them to me? typedef volatile struct { int x; } VS_Def; typedef struct {
11
by: srinivas reddy | last post by:
Hi, Is there any chance that a program doesn' work properly even after a variable is declared as volatile? I remember somebody mentioning a scenario involving L1, L2 caches. Could anybody throw...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
3
by: Mark A. Odell | last post by:
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 ...
9
by: mlimber | last post by:
I am using placement new to locate a shared data structure at a particular location in shared memory. The problem is that when I access the data on one of the two processors sharing it, I don't get...
6
by: red floyd | last post by:
I have a struct that maps onto a set of memory mapped registers. I access this via a pointer. Is it better to declare it as pointer to a volatile struct, or to declare the individual members as...
6
by: titan nyquist | last post by:
Can you make volatile structures in C#? I have a static class, to have "global" variables. This allows the whole program to see them. I make them "volatile" to avoid multi- threading accessing...
16
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.