{"id":6698,"date":"2017-07-27T04:00:48","date_gmt":"2017-07-27T04:00:48","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=6698"},"modified":"2023-01-05T11:57:43","modified_gmt":"2023-01-05T11:57:43","slug":"volatile-example-the-value-of-the-keyword-volatile-in-c","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/volatile-example-the-value-of-the-keyword-volatile-in-c\/","title":{"rendered":"Volatile Example: The Value of the Keyword \u2018Volatile\u2019 in C"},"content":{"rendered":"<p>The keyword \u201cvolatile\u201d informs the compiler that the value of a variable can be changed from the outside. This can occur under the control of the operating system, hardware, or another stream. Because the value is changeable, the compiler loads it from memory each time.<\/p>\n<p>A volatile integer variable can be declared as:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nint volatile \u0445;<br \/>\nvolatile int \u0445;<br \/>\n[\/code]<\/p>\n<p><!--more--><\/p>\n<p>To declare a pointer to this variable, do the following:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nvolatile int *\u0445;<br \/>\nint volatile *\u0445;<br \/>\n[\/code]<\/p>\n<p>A volatile pointer on a nonvolatile data is used rarely, but:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nint *volatile \u0445;<br \/>\n[\/code]<\/p>\n<p>If you want to declare a volatile pointer to a volatile memory area, you should do the following:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nint volatile *volatile \u0445;<br \/>\n[\/code]<\/p>\n<p>The volatile variables are not optimized and it can be useful. Imagine the following function:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nint opt = 1;<br \/>\nvoid Fn(void) {<br \/>\n    start:<br \/>\n        if (opt == 1)<br \/>\n            goto start;<br \/>\n        else<br \/>\n            break;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>At first glance, the program will loop. The compiler can optimize it as follows:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nvoid Fn(void) {<br \/>\n    start:<br \/>\n    int opt = 1;<br \/>\n    if (true)<br \/>\n        goto start;<br \/>\n)<br \/>\n[\/code]<\/p>\n<p>Now the cycle will definitely be endless. However, the external operation allows writing 0 to the variable opt and break the cycle.<\/p>\n<p>To prevent such optimization, you can use the keyword \u201cvolatile\u201d, for example, to announce that some external element of the system changes the variable:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nvolatile int opt = 1;<br \/>\nvoid Fn(void) {<br \/>\n    start:<br \/>\n    if (opt == 1)<br \/>\n        goto start;<br \/>\n    else<br \/>\n        break;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Volatile variables are used as global variables in multithreaded programs. Any thread can change common variables. We do not want to optimize these variables.<\/p>\n<p>Thanks for your attention!<\/p>\n<p style=\"text-align: center;\">References<\/p>\n<p><em>McDowell, Gayle Laakmann. Cracking the Coding Interview: 150 Programming Questions and Solutions.<\/em><\/p>\n<h2>Help with Coding Assignments<\/h2>\n<blockquote><p><em>Our volatile <a href=\"https:\/\/assignmentshark.com\/blog\/c-vector-example\/\" target=\"_blank\" rel=\"noopener noreferrer\">example for C++<\/a> can help you to deal with your own assignment. However, you can&#8217;t hand in this sample as your own work. If you don&#8217;t want to handle your homework or don&#8217;t have time for it, you can get <a href=\"https:\/\/assignmentshark.com\/do-my-programming-homework.html\" target=\"_blank\" rel=\"noopener\">do my coding homework<\/a> help from AssignmentShark.com. We do everything possible to satisfy our customers&#8217; needs. Our experts work fast, so you can get a completed assignment even before the deadline.<\/em><\/p>\n<p><em>If you want to get our help, all you need to do is to place an order with your requirements and set the deadline. We can solve any problem in your homework. Consider our service if you want to get help from a professional expert who has a degree in your sphere of study and experience in handling different types of assignments.<\/em><\/p>\n<p><em>Using our service is a great benefit for all students. On our site, you have the opportunity to contact an expert directly via chat. We have reasonable prices, so any student can afford using our service. With our help, you can get the highest grades. We are the best option for all students with homework problems. We are a reliable service that will handle your assignments much quicker than you or anyone else.<\/em><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/do-my-computer-science-homework.html\" target=\"_blank\" rel=\"noopener\">Do my computer science assignment<\/a> &#8211; We can do it with pleasure!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>The keyword \u201cvolatile\u201d informs the compiler that the value of a variable can be changed from the outside. This can occur under the control of the operating system, hardware, or another stream. Because the value is changeable, the compiler loads it from memory each time. A volatile integer variable can be declared as: [code language=&#8221;cpp&#8221;] [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-6698","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/6698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=6698"}],"version-history":[{"count":10,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/6698\/revisions"}],"predecessor-version":[{"id":13299,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/6698\/revisions\/13299"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=6698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=6698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=6698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}