{"id":5709,"date":"2023-01-14T00:01:15","date_gmt":"2023-01-14T08:01:15","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5709"},"modified":"2023-01-07T11:11:57","modified_gmt":"2023-01-07T19:11:57","slug":"the-volatile-keyword","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5709","title":{"rendered":"The <em>volatile<\/em> Keyword"},"content":{"rendered":"<p>Perhaps the most hilarious keyword in the C language is <em>volatile<\/em>. It&#8217;s a data type qualifier, which I discussed in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5694\">last week&#8217;s Lesson<\/a>. But this qualifier doesn&#8217;t mean that the data is unstable or risky to use, unlike other things described as &#8220;volatile.&#8221;<br \/>\n<!--more--><br \/>\nLike the other C language type qualifiers &mdash; <em>const<\/em>, <em>restrict<\/em>, and <em>_Atomic<\/em> &mdash; <em>volatile<\/em> has more to do with the compiler optimizing code than with anything that happens to data once a program is running.<\/p>\n<p>The direction that <em>volatile<\/em> gives to the compiler is that the data shouldn&#8217;t be optimized at all, ever. I mention this specific use <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5248\">in a post<\/a> from a year ago where I recommended using the <em>volatile<\/em> keyword to prevent the compiler from optimizing a routine to zero-out memory.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_01_14-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_01_14-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid memzero(void *b, int len)\r\n{\r\n    volatile int x;\r\n    char *a;\r\n\r\n    a = (char *)b;\r\n    for( x=0; x&lt;len; x++)\r\n        *(a+x) = 0;\r\n}\r\n\r\nint main()\r\n{\r\n    struct human {\r\n        char name[32];\r\n        int age;\r\n        float weight;\r\n    } my;\r\n\r\n    memzero(&amp;my,sizeof(struct human));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>memzero()<\/em> function is a re-write of the <em>memset()<\/em> function in C, which is considered non-secure: If you want to zero a buffer to erase sensitive information, <em>memset()<\/em> might not do the complete job. The compiler can optimize the function&#8217;s guts, in which case memory remains untouched.<\/p>\n<p>In the code above, the <em>memzero()<\/em> function uses the <em>volatile<\/em> qualifier to ensure that the compiler doesn&#8217;t optimize <em>int<\/em> variable <code>x<\/code>. Its value isn&#8217;t altered as it loops from zero to <code>len<\/code>, filling buffer <code>b<\/code> with zero values.<\/p>\n<p>Of course, you can replace the <em>memset()<\/em> function with <em>memset_s()<\/em> to avoid optimization; you need not write your own function, but the <em>memzero()<\/em> function does demonstrate a valid use for the <em>volatile<\/em> qualifier. (The upcoming C23 standard includes the <em>memset_explicit()<\/em> function that uses the original <em>memset()<\/em> function&#8217;s arguments, but also guarantees no optimization.)<\/p>\n<p>The final data qualifier in the list is <em>_Atomic<\/em>, a keyword introduced with the C11 standard (like a few of the other underscore prefix keywords). This keyword creates an atomic data type that can be accessed by multiple threads in a single program.<\/p>\n<p>I cover threaded programming <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5365\">in another post<\/a>, but not at a level deep enough to discuss how atomic data is used or beneficial. Someday I may further explore this qualifier, but for know just nod knowingly when someone discusses it and say something pithy like, &#8220;Certainly atomic qualifiers ease data manipulation for multithreaded activities.&#8221; Then quickly change the topic to sports.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No, it doesn&#8217;t mean that the data will explode at any given moment. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5709\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5709","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5709"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5709\/revisions"}],"predecessor-version":[{"id":5718,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5709\/revisions\/5718"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}