<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by M. Sami GÜRPINAR on Medium]]></title>
        <description><![CDATA[Stories by M. Sami GÜRPINAR on Medium]]></description>
        <link>https://medium.com/@0msg?source=rss-ab080ce6821d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j_TDykTxJcDk2Q9dA1vTKQ.jpeg</url>
            <title>Stories by M. Sami GÜRPINAR on Medium</title>
            <link>https://medium.com/@0msg?source=rss-ab080ce6821d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:14:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@0msg/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[String view in a Nutshell]]></title>
            <link>https://medium.com/@0msg/string-view-in-a-nutshell-a90d25aea7d5?source=rss-ab080ce6821d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a90d25aea7d5</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[modern-cpp]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[cpp17]]></category>
            <dc:creator><![CDATA[M. Sami GÜRPINAR]]></dc:creator>
            <pubDate>Thu, 30 Jun 2022 17:17:43 GMT</pubDate>
            <atom:updated>2022-07-09T17:06:40.326Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cypieTT_lFqAmzl3V69sew.png" /></figure><p>In C++, <strong>std::string</strong> is a common way to work with strings as it provides a lot of convenient functionalities including searching for substrings, comparing strings, concatenating strings, and slicing strings.</p><p>Nevertheless, <strong>std::string</strong> requires space to be dynamically allocated every time an operation is performed and initializing and copying could be expensive process.</p><p>To address this issue, C++17 introduces <strong>std::string_view </strong>that<strong> </strong>is just a view of a string, that is a read-only reference to a char sequence. Possible implementation as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*CK6evjlx6nfWk9jN14luXw.png" /></figure><p>We can construct<strong> std::string_view</strong> with any type of strings such as std::string_view, std::string, C-style string literals and perform an operation on without extra memory allocation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*JqUTRA1JLBhdPO0NlUqd2A.png" /></figure><p><strong>Output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*jAwI3glxjctMyQakuvmHeA.png" /></figure><p>The program below produces the same output as prior one, but no copies of the string “Hell” is made.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*gcyLmLG8kS6RmcyRIQsMdg.png" /></figure><p>We can also use <strong>std::string_view</strong> with C-style string literals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*aMVIZf2jy9aa_82LGWFwQw.png" /></figure><p>Moreover, <strong>std::string_view</strong> has fully support of constexpr unlike <strong>std::string. </strong>In<strong> </strong>the below program, sv will be replaced with “Hello, World!” at compile-time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*MLPj5ysdvqkLo3e0mj_qOA.png" /></figure><p>Apparently, all replacements have been done at compile-time:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*txM5qP8n9N53YbkDjHX02A.png" /><figcaption>The assembly output</figcaption></figure><p>To summarize, the best practice is to prefer <strong>std::string_view</strong> over <strong>std::string</strong> when a read-only string is needed to work with such as function parameters, commandline paramaters.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a90d25aea7d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Const Keyword with Pointers]]></title>
            <link>https://medium.com/@0msg/const-keyword-with-pointers-649c59ad5492?source=rss-ab080ce6821d------2</link>
            <guid isPermaLink="false">https://medium.com/p/649c59ad5492</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[const]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[M. Sami GÜRPINAR]]></dc:creator>
            <pubDate>Wed, 29 Jun 2022 18:51:32 GMT</pubDate>
            <atom:updated>2022-06-30T14:30:57.534Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*U7yJHvnlp7WRVY15AQUaNQ.png" /></figure><p>Whenever we need an immutable variable, we can use the <strong>const</strong> keyword.By doing so, the variable resides in the memory that is readonly and cannot be modified as long as it exists. Similarly the <strong>const </strong>keyword can be used with pointers as well. But the declaration is interpreted differently depending on where the const is.</p><h4><strong>Pointer to Constant</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*N1bCeMi0UpnQwVvSrsNg2Q.png" /><figcaption>The syntax of the pointer to const variable</figcaption></figure><p>In this case,the variable that pointer points is constant. We cannot modify the variable but we can change the address kept by the pointer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*vMtX0-8QFl_TX0ql1Y4x1Q.png" /></figure><p>When we attemp to modify the variable itself, the error arises.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*WYAxYnkgGjMVgDCrmUbqHQ.png" /></figure><p><strong>Output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*-Ly6MFl0UQ9SiMfe2MMA_g.png" /></figure><h4>Constant Pointer to Non-const Variable</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*qWXnM5Ld8x8tfLQwUK4yow.png" /><figcaption>The syntax of const pointer to variable</figcaption></figure><p>Unlike the pointer to a constant variable, the constant pointer refers to a pointer which its address it points is unmodifiable, except the variable it points.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*bu676Wdg9UUa_MHLYyyqEg.png" /></figure><p>The following code produces an error because the variable of constant pointer cannot be changed as shown in the code below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*3Lhqkz2KuwgthxhKEdvKew.png" /></figure><p><strong>Output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zsv41UrKKEuZ44uBJYzIVQ.png" /></figure><h4><strong>Constant Pointer to Constant</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*zIdB_IZnvJyQ3EQvspE0Mg.png" /><figcaption>The syntax of const pointer to constant</figcaption></figure><p>This type of pointer is used when we need a pointer to a constant variable as well as the pointer storing the address is constant. In other words, neither the value the pointer points nor the address it keeps can be changed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*EQaQvsZsMM5ESnvBLx6Euw.png" /></figure><p>The following code throws an error when we try to change the value of the pointer or the value of the constant variable using the pointer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*mV5XRn7mbFI65M23HM5KlQ.png" /></figure><p><strong>Output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F_3-Wy2Ar4ZQl5SkGNLGrw.png" /></figure><p>To summarize, if the const comes before *, the variable is not changeable.Otherwise, the address value kept by the pointer is not.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=649c59ad5492" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>