473,698 Members | 2,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1943
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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #4

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

Similar topics

8
397
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
5816
by: dwaach | last post by:
Hi, I have something like. struct X {}; X ox; X* pox=&ox; X*& volatile r =pox;
5
6644
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
2135
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 some light on this? Thanks, Srinivas
9
2760
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
8320
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 struct Foo {
9
3084
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 the same values seen by the other processor (which uses a cache-less access method, unlike what follows). Here's a reduced example: struct SharedData { enum Item { A=0, B=1 };
6
2605
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 volatile? That is to say, which is likely to be better/less error prone: struct registers { unsigned long reg1;
6
3533
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 issues. That works. THE PROBLEM: In that static class, I want to combine some variables inside a structure, and then make a variable of that structure type,
16
1827
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 accesses to the members are byte-wide, or is the compiler free to read or read-modify-write in any data width it chooses? Is slapping a volatile on each member of the struct definition any different? better? worse?
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.