<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TheStandardGuy</title>
    <description>The latest articles on DEV Community by TheStandardGuy (@thestandardguy_220cf14772).</description>
    <link>https://dev.to/thestandardguy_220cf14772</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2805791%2F70d5dad0-7f2b-4301-8a9d-172f93dc687f.jpg</url>
      <title>DEV Community: TheStandardGuy</title>
      <link>https://dev.to/thestandardguy_220cf14772</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thestandardguy_220cf14772"/>
    <language>en</language>
    <item>
      <title>Volatile in action</title>
      <dc:creator>TheStandardGuy</dc:creator>
      <pubDate>Sat, 15 Feb 2025 10:01:41 +0000</pubDate>
      <link>https://dev.to/thestandardguy_220cf14772/volatile-in-action-58bm</link>
      <guid>https://dev.to/thestandardguy_220cf14772/volatile-in-action-58bm</guid>
      <description>&lt;p&gt;There is a qualifier called &lt;code&gt;volatile&lt;/code&gt; very popular in embedded code bases, but sometimes it is used improperly since some misconceptions about it. Let's see how it really works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;p&gt;Qualifying a variable as &lt;code&gt;volatile&lt;/code&gt; tells something very straightforward to the compiler, &lt;em&gt;it must not apply any kind of optimization on the variable&lt;/em&gt;. &lt;br&gt;
In this sentence a key role is played by the compiler, so, how can we see this working? I made an example that show this behaviour.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;    &lt;span class="c1"&gt;// p points to v&lt;/span&gt;

     &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// now p points to w&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// changing the value pointed by p, now w!&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// print 42&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// print 21 !!!&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown in the previous example, that is available &lt;a href="https://godbolt.org/z/f1P7zodPT" rel="noopener noreferrer"&gt;here&lt;/a&gt;, using gcc 14.2 on a x86-64 architecture without any compiler options. The compiler optimize stuffs and decide to did not apply the change of the value pointed by &lt;code&gt;p&lt;/code&gt; after its decrementation. It catches a probably Undefined Behavior and avoid it. It is a good thing uh? Absolutely yes, but, for teaching purpose I want it! So, how can I fix this compiler optimization?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;    &lt;span class="c1"&gt;// p points to v&lt;/span&gt;

    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// now p points to w&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// changing the value pointed by p, now w!&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// I'm using the v-address&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// I'm using the w-address&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// print 42&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// print 77&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that I'm using the address of the variables &lt;code&gt;v&lt;/code&gt; and &lt;code&gt;w&lt;/code&gt; the compiler is forced to use them and it can't avoid to change the value pointed by &lt;code&gt;p&lt;/code&gt;. Example available &lt;a href="https://godbolt.org/z/8TT176KoY" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use of volatile
&lt;/h2&gt;

&lt;p&gt;But it is annoying change the program to obtain what I wanted from the beginning because of decisions made by the compiler, even if are good ones. I'm the programmer and I want the full control of what it is generated by my code. Therefore, we can use &lt;code&gt;volatile&lt;/code&gt; for our purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;    &lt;span class="c1"&gt;// p points to v&lt;/span&gt;

    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// now p points to w&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// changing the value pointed by p, now w!&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// print 42&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// now, finally, prints 77!!!&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declaring the variable &lt;code&gt;w&lt;/code&gt; as voltile we forced the compiler to not apply any kind of optimization exactly what we want (also if we want a bad thing). Why apply that qualifier only on &lt;code&gt;w&lt;/code&gt; variable? Because it is its address the subject of the optimization. Example available &lt;a href="https://godbolt.org/z/MY4Pnd6Pz" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Volatile and concurrency
&lt;/h3&gt;

&lt;p&gt;Historically volatile word is believed a synchronization mechanism in concurrency programming. This is wrong, &lt;code&gt;volatile&lt;/code&gt; doesn't guarantee the atomicity of the reading / writing operations over the variable where it is applied, use instead &lt;code&gt;std::atomic&lt;/code&gt;, mutexes or &lt;code&gt;condition_variables&lt;/code&gt; to do so.&lt;/p&gt;

&lt;h3&gt;
  
  
  Volatile and hardware
&lt;/h3&gt;

&lt;p&gt;Another way to think about volatile is that it tell to compiler that the variable can be changed by something external from the code. Is this the case of low level programming as, for example, embedded environment. A variable can be used by a peripheral and it couldn't be evident from the code. In this cases &lt;code&gt;volatile&lt;/code&gt; word avoid the compiler made decision distruptive for our program. &lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeawayas
&lt;/h2&gt;

&lt;p&gt;After seen &lt;code&gt;volatile&lt;/code&gt; in action we can summarize some takeaway points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;volatile&lt;/code&gt; to deactivate possible compiler optimizations.&lt;/li&gt;
&lt;li&gt;Use volatile in hardware related code.&lt;/li&gt;
&lt;li&gt;Don't use it as a synchronization method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use &lt;code&gt;volatile&lt;/code&gt; with awareness and see you on next episode!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
      <category>c</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>TheStandardGuy</dc:creator>
      <pubDate>Sat, 08 Feb 2025 20:48:37 +0000</pubDate>
      <link>https://dev.to/thestandardguy_220cf14772/-414m</link>
      <guid>https://dev.to/thestandardguy_220cf14772/-414m</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/thestandardguy_220cf14772" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2805791%2F70d5dad0-7f2b-4301-8a9d-172f93dc687f.jpg" alt="thestandardguy_220cf14772"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/thestandardguy_220cf14772/pointers-a-weird-beast-for-beginners-and-beyond-1ab6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Pointers, a weird beast for beginners and beyond&lt;/h2&gt;
      &lt;h3&gt;TheStandardGuy ・ Feb 8&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#cpp&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#c&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>cpp</category>
      <category>c</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Pointers, a weird beast for beginners and beyond</title>
      <dc:creator>TheStandardGuy</dc:creator>
      <pubDate>Sat, 08 Feb 2025 16:37:36 +0000</pubDate>
      <link>https://dev.to/thestandardguy_220cf14772/pointers-a-weird-beast-for-beginners-and-beyond-1ab6</link>
      <guid>https://dev.to/thestandardguy_220cf14772/pointers-a-weird-beast-for-beginners-and-beyond-1ab6</guid>
      <description>&lt;p&gt;There is no construct of the C/C++ programming language more difficult for beginners than pointers. This is a fact, not an opinion. Many students struggle with this concept and many developers flee from the idea of handling with them. I think that to understand pointers, one should start from the beginning, therefore...&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a pointer?
&lt;/h2&gt;

&lt;p&gt;A pointer is a data type of the language like &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt; etc. The syntax to declare a pointer is to put an &lt;code&gt;*&lt;/code&gt; after a type, so, for example, a pointer to an integer will be declared as &lt;code&gt;int *&lt;/code&gt;, a pointer to a custom structure, like say &lt;code&gt;MyType&lt;/code&gt;, will be declared as &lt;code&gt;MyType *&lt;/code&gt;. &lt;br&gt;
Now that we know how to declare a pointer, we need to know what it is! A pointer, like the name itself says, is a type that &lt;em&gt;points&lt;/em&gt; to something, but what? It points to an address of the type declared. Therefore, &lt;code&gt;int *&lt;/code&gt; stores the value of the address of an integer type, &lt;code&gt;MyType *&lt;/code&gt; stores the address of a &lt;code&gt;MyType&lt;/code&gt; variable.&lt;br&gt;
Making a clearer example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// remembering that the address of a variable is &lt;/span&gt;
    &lt;span class="c1"&gt;// accessible with operator &amp;amp;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// 0x50527c (as example)&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// 0x50527c, the address of value!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
Code example 1





&lt;h2&gt;
  
  
  Principal pointers operators
&lt;/h2&gt;

&lt;p&gt;Now that we know what a pointer is, besides assigning the address of another variable, what can we do with them? There are two main operations for pointers, &lt;em&gt;dereferencing&lt;/em&gt; and what it is called &lt;em&gt;pointer arithmetic&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dereferencing
&lt;/h3&gt;

&lt;p&gt;The operation of dereferentiation is done with the operator &lt;code&gt;*&lt;/code&gt; (again) before a variable of pointer-type. This operation allows us to access the &lt;em&gt;value pointed&lt;/em&gt; to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;     
    &lt;span class="c1"&gt;// remembering that the address of a variable is &lt;/span&gt;
    &lt;span class="c1"&gt;// accessible with operator &amp;amp;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// p points to the memory address of var&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// now var = 21&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
Code example 2





&lt;p&gt;But what happens if I dereference an unassigned pointer? In cplusplus, regardless of type, an unassigned variable has a random value, so accessing it as a memory address can bring us many headaches. If we are lucky, the program crashes; otherwise, we can have undefined behaviours (UB) or memory corruptions (MC).&lt;br&gt;
Why did I say a crash is a a good thing? Because we noticed it and we *&lt;em&gt;must *&lt;/em&gt; intervene to resolve it, while with UB and MC there is the chance we won't notice it for a long time, and it can be very difficult to debug and resolve it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Pointer arithmetic
&lt;/h3&gt;

&lt;p&gt;Pointer arithmetic allows us to change the value of a pointer, hence, the address stored inside the variable. Like any other integral number, a pointer can be pre- and post- incremented &lt;code&gt;++&lt;/code&gt; and decremented &lt;code&gt;--&lt;/code&gt;. Furthermore, a pointer can be added/substracted to/from an integer. One might think that incrementing a pointer by 1 points to the next address in memory, and this is not completely wrong, but it points to the next address distant from the previous one by the size of the type pointed to. &lt;br&gt;
Then, in our example, &lt;code&gt;p+=1&lt;/code&gt; means that &lt;code&gt;p&lt;/code&gt; is now pointing the address distant &lt;code&gt;1*sizeof(int)&lt;/code&gt; from the previous one. &lt;br&gt;
This pointers feature should be used very consciously because it allows us to &lt;em&gt;navigate&lt;/em&gt; the memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// the memory is sequentially mapped so w is sizeof(int) bytes after v&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;    &lt;span class="c1"&gt;// p points to w&lt;/span&gt;

    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// now p points to next address ... but there is v there!&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// adding 1 to the value in the address pointed by p&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// print 22 &lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// print 42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
Code example 3





&lt;p&gt;As we can see, we changed the value of a variable that was never pointed to explicitly by an assignement of a pointer variable, but with pointer arithmetic!&lt;/p&gt;

&lt;h2&gt;
  
  
  The zero of pointers
&lt;/h2&gt;

&lt;p&gt;Like the number 0 represents a very special case for numbers, there is a similar value for pointers. In C++ it is &lt;code&gt;nullptr&lt;/code&gt; and, usually, a pointer with value &lt;code&gt;nullptr&lt;/code&gt; is called &lt;em&gt;invalid&lt;/em&gt;. It is very important to use this value in a smart way, so we should assign a pointer to nullptr when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have finished using it.&lt;/li&gt;
&lt;li&gt;To signal something has goes wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These practices can be used to detect if a pointer is safe to dereference to avoid crashes, UB, or MC. Therefore, we should &lt;strong&gt;always&lt;/strong&gt; check the validity of a pointer before dereferencing it.&lt;br&gt;
But what happens if I forget to check the validity and deference a nullptr? With good chance we have a crash due to segmentation fault or access violation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// static variables live accross the function scope&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;answ&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; 
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answ&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;answ&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="c1"&gt;// std::cout &amp;lt;&amp;lt; *p &amp;lt;&amp;lt; std::endl; // crash!&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"p is invalid"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
Code example 4





&lt;h2&gt;
  
  
  Key takeawayas
&lt;/h2&gt;

&lt;p&gt;Summarizing we've learned that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pointers are variable that store a memory location.&lt;/li&gt;
&lt;li&gt;Always initialize pointers, possibly with &lt;code&gt;nullptr&lt;/code&gt; if no valid address is available.&lt;/li&gt;
&lt;li&gt;Always check for &lt;code&gt;nullptr&lt;/code&gt; before dereferencing to avoid crashes.&lt;/li&gt;
&lt;li&gt;Be careful with pointer arithmetic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what I would say about pointers for today but, for sure, this is not the last time I talk about them! &lt;/p&gt;

&lt;p&gt;See you in the next episode!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>c</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
