I'm developing the firmware for a slave in a comunication channel. Now
there is certain information (namely the addresses of the slave and the
master) that must be changeable while the device is up and operating.
The obvious choice is to store the addresses in some variable. But I
would like to prevent unintentional changes in those variables so I
thought of doing the following stuff:
File addresses.c :
char masterAddress;
void changeMasterAdd ress (char newValue) {
masterAddress = newValue;
}
Another file:
extern const volatile char masterAddress;
extern void changeMasterAdd ress (char newValue);
I've compiled this with gcc and it works as expected. The compiler
issues warnings if I try to assign a value to masterAddress outside
addresses.c, and I can still change its value with changeMasterAdd ress
anywhere in the code.
Finally my questions:
1.- Is this behaviour standard?
2.- Is the volatile keyword needed in the declaration? I thought of
placing it to prevent the compiler from optimizing multiple accesses to
a read-only variable (as seen in the rest of the files).
Thanks in advance. 7 1442
"Antonio" <an*****@gmail. com> writes: I'm developing the firmware for a slave in a comunication channel. Now there is certain information (namely the addresses of the slave and the master) that must be changeable while the device is up and operating. The obvious choice is to store the addresses in some variable. But I would like to prevent unintentional changes in those variables so I thought of doing the following stuff:
File addresses.c :
char masterAddress;
void changeMasterAdd ress (char newValue) { masterAddress = newValue; }
Another file:
extern const volatile char masterAddress; extern void changeMasterAdd ress (char newValue);
I've compiled this with gcc and it works as expected. The compiler issues warnings if I try to assign a value to masterAddress outside addresses.c, and I can still change its value with changeMasterAdd ress anywhere in the code.
Finally my questions: 1.- Is this behaviour standard? 2.- Is the volatile keyword needed in the declaration? I thought of placing it to prevent the compiler from optimizing multiple accesses to a read-only variable (as seen in the rest of the files).
This approach isn't the way to do what you want.
To prevent accesses to masterAddress from outside the
addresses.c compilation unit, make it 'static':
static char masterAddress;
This declaration prevents all accesses outside the single
compilation unit (addresses.c). If you want to allow read
access but disallow write access, C doesn't provide an easy
way to do that; so, if that's what you want to do, you can
add this declaration to addresses.c:
const char * const masterAddress_p = &masterAddre ss;
and for other compilations units that you want to use the
variable:
extern const char * const masterAddress_p ;
The value of the variable masterAddress may now be accessed
(for reading, not for writing) with '*masterAddress _p'.
Inside addresses.c, the variable 'masterAddress' may be
assigned to directly, which will result in changing the
value of '*masterAddress _p'.
The idea of declaring the variable 'const volatile' just in
the extern declarations is no good; some implementations
may do the right thing with this approach, but strictly
speaking it results in undefined behavior.
Tim Rentsch wrote: "Antonio" <an*****@gmail. com> writes:
I'm developing the firmware for a slave in a comunication channel. Now there is certain information (namely the addresses of the slave and the master) that must be changeable while the device is up and operating. The obvious choice is to store the addresses in some variable. But I would like to prevent unintentional changes in those variables so I thought of doing the following stuff:
File addresses.c :
char masterAddress;
void changeMasterAdd ress (char newValue) { masterAddress = newValue; }
Another file:
extern const volatile char masterAddress; extern void changeMasterAdd ress (char newValue);
I've compiled this with gcc and it works as expected. The compiler issues warnings if I try to assign a value to masterAddress outside addresses.c, and I can still change its value with changeMasterAdd ress anywhere in the code.
Finally my questions: 1.- Is this behaviour standard? 2.- Is the volatile keyword needed in the declaration? I thought of placing it to prevent the compiler from optimizing multiple accesses to a read-only variable (as seen in the rest of the files). This approach isn't the way to do what you want.
To prevent accesses to masterAddress from outside the addresses.c compilation unit, make it 'static':
static char masterAddress;
This declaration prevents all accesses outside the single compilation unit (addresses.c). If you want to allow read access but disallow write access, C doesn't provide an easy way to do that; so, if that's what you want to do, you can add this declaration to addresses.c:
const char * const masterAddress_p = &masterAddre ss;
and for other compilations units that you want to use the variable:
extern const char * const masterAddress_p ;
The value of the variable masterAddress may now be accessed (for reading, not for writing) with '*masterAddress _p'. Inside addresses.c, the variable 'masterAddress' may be assigned to directly, which will result in changing the value of '*masterAddress _p'.
Thanks a lot. I like your approach much more.
The idea of declaring the variable 'const volatile' just in the extern declarations is no good; some implementations may do the right thing with this approach, but strictly speaking it results in undefined behavior.
Why is it so? (Besides the fact that the standard says so.)
Antonio wrote: Tim Rentsch wrote: "Antonio" <an*****@gmail. com> writes:
[...] extern const volatile char masterAddress;
[...] The idea of declaring the variable 'const volatile' just in the extern declarations is no good; some implementations may do the right thing with this approach, but strictly speaking it results in undefined behavior.
Why is it so? (Besides the fact that the standard says so.)
What, exactly, is "const volatile" supposed to mean? "Const" means
that it doesn't change, and "volatile" means that it may change
without you doing anything.
I am guessing that you want it to mean "this is a volatile variable,
but _I_ am not allowed to change it"?
What does the standard say about this, anyway?
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Kenneth Brody wrote: Antonio wrote: Tim Rentsch wrote: "Antonio" <an*****@gmail. com> writes: [...] > extern const volatile char masterAddress; [...] The idea of declaring the variable 'const volatile' just in the extern declarations is no good; some implementations may do the right thing with this approach, but strictly speaking it results in undefined behavior.
Why is it so? (Besides the fact that the standard says so.)
What, exactly, is "const volatile" supposed to mean? "Const" means that it doesn't change, and "volatile" means that it may change without you doing anything.
I am guessing that you want it to mean "this is a volatile variable, but _I_ am not allowed to change it"?
What does the standard say about this, anyway?
`const' and `volatile' are both type qualifiers, and it is
perfectly legal to have them both.
There is an example in the (C99) draft of the same, and this is what
it says:
"An object declared
extern const volatile int real_time_clock ;
may be modifiable by hardware, but cannot be assigned to, incremented,
or decremented."
Does that help?
"Antonio" <an*****@gmail. com> writes: Tim Rentsch wrote: [snip]
Thanks a lot. I like your approach much more.
You're welcome. Glad it worked for you. The idea of declaring the variable 'const volatile' just in the extern declarations is no good; some implementations may do the right thing with this approach, but strictly speaking it results in undefined behavior.
Why is it so? (Besides the fact that the standard says so.)
Basically, all declarations of a variable need to be the
same. (The technical phrase is that "their types need to
be compatible", which isn't quite the same as "the same",
but the details of how they are different isn't important
here.)
The reason all declarations need to be the same is that
if they were different, different compilation units might
make different assumptions about how to make use of the
variables. For example, 'const' variables might be put
in special read-only memory; if a variable were 'const'
in one compilation unit and not in another, that would
probably cause problems under such a scheme.
Of course, it would have been possible to come up with a
rule to deal with these situations. But the rule would
likely be extremely complicated, and impose constraints
on compilers and so forth that wouldn't otherwise be
there; moreover, the benefits would likely be extremely
small. Rather than add all that complication for little
benefit, instead a decision was made to require each
variable to have compatible declarations everywhere it
was used.
>What, exactly, is "const volatile" supposed to mean? "Const" means that it doesn't change, and "volatile" means that it may change without you doing anything.
I am guessing that you want it to mean "this is a volatile variable, but _I_ am not allowed to change it"?
What does the standard say about this, anyway?
I believe one of the standard examples is:
const volatile time_t *hardware_real_ time_clock = <some magic address here>;
which represents a pointer to a hardware register YOU can't change,
but which changes all by itself. The same typically applies to
memory-mapped IO "status registers" which are read-only. Or, for
that matter, they might apply to memory-mapped IO "data registers"
which, as soon as you read it, load up the next character available
if there is one.
const doesn't mean constant. A const variable is not a
valid constant expression. The declaration:
ant const int bar = 42;
switch (c) {
case bar: ...;
default: ...;
...
}
was removed from the C standard retroactively by undefined behavior
caused by void main on a DS9K with the time-travel add-on.
Gordon L. Burditt
On Wed, 27 Jul 2005 08:18:02 -0400, Kenneth Brody wrote: Antonio wrote: Tim Rentsch wrote: > "Antonio" <an*****@gmail. com> writes: [...] > > extern const volatile char masterAddress; [...] > The idea of declaring the variable 'const volatile' just in > the extern declarations is no good; some implementations > may do the right thing with this approach, but strictly > speaking it results in undefined behavior. Why is it so? (Besides the fact that the standard says so.)
What, exactly, is "const volatile" supposed to mean? "Const" means that it doesn't change, and "volatile" means that it may change without you doing anything.
const means read-only or "I can't change it". volatile means "something
else can change it without my knowledge". const volatile means I
can't change it but something else can.
I am guessing that you want it to mean "this is a volatile variable, but _I_ am not allowed to change it"?
That's pretty much what it is.
Lawrence This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jay Moore |
last post by:
Greetings, all!
I have a project for work, and I'm not sure how to efficiently do what I
need to do. I'm hoping someone out there can help.
Project is this: I'm creating a web-based interface where people at my
company (operators) can enter data for service calls. All data entered
is run thru one or more PHP scripts for error checking...
|
by: nzanella |
last post by:
Hello,
I am running SQL Server 2000. I would like to know whether
Microsoft Transact-SQL has a method for limiting the result
set from a query in a way analogous to MySQL's LIMIT keyword,
so that, for instance, if the result set contains 10,000 rows,
then only the first 10 rows from the record set are output.
Thank you,
|
by: Daniel |
last post by:
limiting access to files with asp.net
is there any way i can make a file only accessible to certain users of my
website? my files are to large to copy to a temp directory and they are of
many types, .exe .xsl .xml etc. i want an href to each file but only allow
users who have the right login-info in their session variable to get to the...
|
by: Jo Davis |
last post by:
access fe and access be. later the be might be sql server
I don't want people to pass this application around. And I want control over
usage. I want it to 'expire' after a while.
I have fairly good control over the install.
I have (probably?) prevented unauthorised distribution thus:
I have a module which checks the HD partition...
|
by: randyelliott |
last post by:
Good Day,
I have a MS Access (Access 2000 now upgraded to 2003) database that
tracks customer information. One function of this database is to
create an encrypted license file for our software, which is then
emailed out to the customer. The encryption string is based on
information in the database and can only be generated by executing a...
| |
by: Gerry Abbott |
last post by:
Hi all,
Im using an ubound form and recordsets for data update. Id like to trap
invalid entries on this form. How can i limit the number of characters the
user enters for a particular field. ?
Thanks in advance.
Gerry Abbott
|
by: N J |
last post by:
Hi,
I ahve developed a program using access and am distributing it using MDE's,
I ahve had many requests for a demo. I was thinking of limiting the number
of records to say 100?
If anyone has any idea's that'd be great, thanks...Nick J
--
|
by: Peter Chant |
last post by:
I'm considering setting a website up for a club. I do not plan the contents
to be for public consumption, but on the other hand I'm not going to have
anything on there that is confidential, that would cause a problem if it
went further.
The basic reason is for publicity of club events. I want to make it easy to
use. I suspect a login...
|
by: prakashwadhwani |
last post by:
I've used Dbase/Clipper for years & Access for a little while now but
Access doesn't seem to have any elegant solution to limiting the size
of a text box ... i.e. the max number of characters that can be
entered into a text box.
In Dbase/Clipper eg. if memory serves me correctly, it's a 1-line
elegant solution ..
@ row, col get variable...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |