const volatile, register volatile, static volatile in C++












25















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?










share|improve this question























  • Very nice! +1. Could even include mutable.

    – Captain Obvlious
    Apr 28 '13 at 5:35
















25















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?










share|improve this question























  • Very nice! +1. Could even include mutable.

    – Captain Obvlious
    Apr 28 '13 at 5:35














25












25








25


15






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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 28 '13 at 5:31









Avraam MavridisAvraam Mavridis

4,4381149103




4,4381149103













  • 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

















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












1 Answer
1






active

oldest

votes


















30














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.






share|improve this answer





















  • 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 shows const 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 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











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
});


}
});














draft saved

draft discarded


















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









30














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.






share|improve this answer





















  • 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 shows const 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 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
















30














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.






share|improve this answer





















  • 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 shows const 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 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














30












30








30







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 shows const 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 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














  • 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 shows const 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 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








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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?