const volatile, register volatile, static volatile in C++
I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think:
register volatile int T=10;
Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)
const volatile int T=10;
The program itself can not modify T, but T can be modified frow somewhere outside the code.
static volatile int T=10;
If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.
Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?
c++ static const volatile
add a comment |
I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think:
register volatile int T=10;
Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)
const volatile int T=10;
The program itself can not modify T, but T can be modified frow somewhere outside the code.
static volatile int T=10;
If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.
Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?
c++ static const volatile
Very nice! +1. Could even includemutable
.
– Captain Obvlious
Apr 28 '13 at 5:35
add a comment |
I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think:
register volatile int T=10;
Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)
const volatile int T=10;
The program itself can not modify T, but T can be modified frow somewhere outside the code.
static volatile int T=10;
If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.
Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?
c++ static const volatile
I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think:
register volatile int T=10;
Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)
const volatile int T=10;
The program itself can not modify T, but T can be modified frow somewhere outside the code.
static volatile int T=10;
If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.
Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?
c++ static const volatile
c++ static const volatile
asked Apr 28 '13 at 5:31
Avraam MavridisAvraam Mavridis
4,4381149103
4,4381149103
Very nice! +1. Could even includemutable
.
– Captain Obvlious
Apr 28 '13 at 5:35
add a comment |
Very nice! +1. Could even includemutable
.
– Captain Obvlious
Apr 28 '13 at 5:35
Very nice! +1. Could even include
mutable
.– Captain Obvlious
Apr 28 '13 at 5:35
Very nice! +1. Could even include
mutable
.– Captain Obvlious
Apr 28 '13 at 5:35
add a comment |
1 Answer
1
active
oldest
votes
register volatile int T=10;
volatile
qualifier means that the compiler cannot apply optimizations or reorder access to T
, While register
is a hint to the compiler that T
will be heavily used. If address of T
is taken, the hint is simply ignored by the compiler. Note that register
is deprecated but still used.
Practical Usage:
I have never used it never felt the need for it and can't really think of any right now.
const volatile int T=10;
const
qualifier means that the T
cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile
still means the same as in case 1. The compiler cannot optimize or reorder access to T
.
Practical Usage:
- Accessing shared memory in read-only mode.
- Accessing hardware registers in read-only mode.
static volatile int T=10;
static
storage qualifier gives T
static storage duration (C++11 §3.7) and internal linkage, while volatile
still governs the optimization and reordering.
Practical Usage:
- Same as
volatile
except that you need the object to have static storage duration and to be inaccessible from other translation units.
2
For the second one, are you sure you're talking about const-volatile objects, or const-volatile references (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware...
– Mehrdad
Apr 28 '13 at 6:03
1
@Mehrdad: I was referring to, pointer to const-volatile objects something like:unsigned char const volatile *hd_addr;
– Alok Save
Apr 28 '13 at 6:07
1
Yeah, your example showsconst volatile int T=10;
which is different. :)
– Mehrdad
Apr 28 '13 at 6:09
2
@Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :)
– Alok Save
Apr 28 '13 at 6:11
@Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as theconst volatile int
is not alsostatic
, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff.
– cooperised
Aug 3 '17 at 15:42
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16259939%2fconst-volatile-register-volatile-static-volatile-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
register volatile int T=10;
volatile
qualifier means that the compiler cannot apply optimizations or reorder access to T
, While register
is a hint to the compiler that T
will be heavily used. If address of T
is taken, the hint is simply ignored by the compiler. Note that register
is deprecated but still used.
Practical Usage:
I have never used it never felt the need for it and can't really think of any right now.
const volatile int T=10;
const
qualifier means that the T
cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile
still means the same as in case 1. The compiler cannot optimize or reorder access to T
.
Practical Usage:
- Accessing shared memory in read-only mode.
- Accessing hardware registers in read-only mode.
static volatile int T=10;
static
storage qualifier gives T
static storage duration (C++11 §3.7) and internal linkage, while volatile
still governs the optimization and reordering.
Practical Usage:
- Same as
volatile
except that you need the object to have static storage duration and to be inaccessible from other translation units.
2
For the second one, are you sure you're talking about const-volatile objects, or const-volatile references (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware...
– Mehrdad
Apr 28 '13 at 6:03
1
@Mehrdad: I was referring to, pointer to const-volatile objects something like:unsigned char const volatile *hd_addr;
– Alok Save
Apr 28 '13 at 6:07
1
Yeah, your example showsconst volatile int T=10;
which is different. :)
– Mehrdad
Apr 28 '13 at 6:09
2
@Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :)
– Alok Save
Apr 28 '13 at 6:11
@Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as theconst volatile int
is not alsostatic
, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff.
– cooperised
Aug 3 '17 at 15:42
|
show 1 more comment
register volatile int T=10;
volatile
qualifier means that the compiler cannot apply optimizations or reorder access to T
, While register
is a hint to the compiler that T
will be heavily used. If address of T
is taken, the hint is simply ignored by the compiler. Note that register
is deprecated but still used.
Practical Usage:
I have never used it never felt the need for it and can't really think of any right now.
const volatile int T=10;
const
qualifier means that the T
cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile
still means the same as in case 1. The compiler cannot optimize or reorder access to T
.
Practical Usage:
- Accessing shared memory in read-only mode.
- Accessing hardware registers in read-only mode.
static volatile int T=10;
static
storage qualifier gives T
static storage duration (C++11 §3.7) and internal linkage, while volatile
still governs the optimization and reordering.
Practical Usage:
- Same as
volatile
except that you need the object to have static storage duration and to be inaccessible from other translation units.
2
For the second one, are you sure you're talking about const-volatile objects, or const-volatile references (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware...
– Mehrdad
Apr 28 '13 at 6:03
1
@Mehrdad: I was referring to, pointer to const-volatile objects something like:unsigned char const volatile *hd_addr;
– Alok Save
Apr 28 '13 at 6:07
1
Yeah, your example showsconst volatile int T=10;
which is different. :)
– Mehrdad
Apr 28 '13 at 6:09
2
@Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :)
– Alok Save
Apr 28 '13 at 6:11
@Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as theconst volatile int
is not alsostatic
, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff.
– cooperised
Aug 3 '17 at 15:42
|
show 1 more comment
register volatile int T=10;
volatile
qualifier means that the compiler cannot apply optimizations or reorder access to T
, While register
is a hint to the compiler that T
will be heavily used. If address of T
is taken, the hint is simply ignored by the compiler. Note that register
is deprecated but still used.
Practical Usage:
I have never used it never felt the need for it and can't really think of any right now.
const volatile int T=10;
const
qualifier means that the T
cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile
still means the same as in case 1. The compiler cannot optimize or reorder access to T
.
Practical Usage:
- Accessing shared memory in read-only mode.
- Accessing hardware registers in read-only mode.
static volatile int T=10;
static
storage qualifier gives T
static storage duration (C++11 §3.7) and internal linkage, while volatile
still governs the optimization and reordering.
Practical Usage:
- Same as
volatile
except that you need the object to have static storage duration and to be inaccessible from other translation units.
register volatile int T=10;
volatile
qualifier means that the compiler cannot apply optimizations or reorder access to T
, While register
is a hint to the compiler that T
will be heavily used. If address of T
is taken, the hint is simply ignored by the compiler. Note that register
is deprecated but still used.
Practical Usage:
I have never used it never felt the need for it and can't really think of any right now.
const volatile int T=10;
const
qualifier means that the T
cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile
still means the same as in case 1. The compiler cannot optimize or reorder access to T
.
Practical Usage:
- Accessing shared memory in read-only mode.
- Accessing hardware registers in read-only mode.
static volatile int T=10;
static
storage qualifier gives T
static storage duration (C++11 §3.7) and internal linkage, while volatile
still governs the optimization and reordering.
Practical Usage:
- Same as
volatile
except that you need the object to have static storage duration and to be inaccessible from other translation units.
edited May 23 '17 at 12:26
Community♦
11
11
answered Apr 28 '13 at 5:47
Alok SaveAlok Save
164k35341482
164k35341482
2
For the second one, are you sure you're talking about const-volatile objects, or const-volatile references (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware...
– Mehrdad
Apr 28 '13 at 6:03
1
@Mehrdad: I was referring to, pointer to const-volatile objects something like:unsigned char const volatile *hd_addr;
– Alok Save
Apr 28 '13 at 6:07
1
Yeah, your example showsconst volatile int T=10;
which is different. :)
– Mehrdad
Apr 28 '13 at 6:09
2
@Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :)
– Alok Save
Apr 28 '13 at 6:11
@Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as theconst volatile int
is not alsostatic
, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff.
– cooperised
Aug 3 '17 at 15:42
|
show 1 more comment
2
For the second one, are you sure you're talking about const-volatile objects, or const-volatile references (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware...
– Mehrdad
Apr 28 '13 at 6:03
1
@Mehrdad: I was referring to, pointer to const-volatile objects something like:unsigned char const volatile *hd_addr;
– Alok Save
Apr 28 '13 at 6:07
1
Yeah, your example showsconst volatile int T=10;
which is different. :)
– Mehrdad
Apr 28 '13 at 6:09
2
@Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :)
– Alok Save
Apr 28 '13 at 6:11
@Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as theconst volatile int
is not alsostatic
, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff.
– cooperised
Aug 3 '17 at 15:42
2
2
For the second one, are you sure you're talking about const-volatile objects, or const-volatile references (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware...
– Mehrdad
Apr 28 '13 at 6:03
For the second one, are you sure you're talking about const-volatile objects, or const-volatile references (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware...
– Mehrdad
Apr 28 '13 at 6:03
1
1
@Mehrdad: I was referring to, pointer to const-volatile objects something like:
unsigned char const volatile *hd_addr;
– Alok Save
Apr 28 '13 at 6:07
@Mehrdad: I was referring to, pointer to const-volatile objects something like:
unsigned char const volatile *hd_addr;
– Alok Save
Apr 28 '13 at 6:07
1
1
Yeah, your example shows
const volatile int T=10;
which is different. :)– Mehrdad
Apr 28 '13 at 6:09
Yeah, your example shows
const volatile int T=10;
which is different. :)– Mehrdad
Apr 28 '13 at 6:09
2
2
@Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :)
– Alok Save
Apr 28 '13 at 6:11
@Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :)
– Alok Save
Apr 28 '13 at 6:11
@Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as the
const volatile int
is not also static
, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff.– cooperised
Aug 3 '17 at 15:42
@Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as the
const volatile int
is not also static
, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff.– cooperised
Aug 3 '17 at 15:42
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16259939%2fconst-volatile-register-volatile-static-volatile-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Very nice! +1. Could even include
mutable
.– Captain Obvlious
Apr 28 '13 at 5:35