<?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: Ahmed Abouzied</title>
    <description>The latest articles on DEV Community by Ahmed Abouzied (@ahmedaabouzied).</description>
    <link>https://dev.to/ahmedaabouzied</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%2F130048%2F455317d3-e614-404d-ae66-62b9bd31b057.jpg</url>
      <title>DEV Community: Ahmed Abouzied</title>
      <link>https://dev.to/ahmedaabouzied</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedaabouzied"/>
    <language>en</language>
    <item>
      <title>On the internet conquest of happiness</title>
      <dc:creator>Ahmed Abouzied</dc:creator>
      <pubDate>Mon, 03 Jan 2022 01:46:13 +0000</pubDate>
      <link>https://dev.to/ahmedaabouzied/on-the-internet-conquest-of-happiness-23d4</link>
      <guid>https://dev.to/ahmedaabouzied/on-the-internet-conquest-of-happiness-23d4</guid>
      <description>&lt;h2&gt;
  
  
  You outgrow your town
&lt;/h2&gt;

&lt;p&gt;Sympathetic surroundings are essential to happiness. I think it’s common enough to find oneself in a situation of recalling the song “Creep” by Radiohead and to be specific the part “What the hell am I doing here?". I dare say that you -the reader- share the experience with me of finding no one to share the interest in a specific subject you deeply care about. You find the people and the environment surrounding you to be not just ignorant about that subject, but also discouraging. A young person who wishes to read a serious book is discouraged by their parents. Teachers tell them off in the fear that these books may disorient their thoughts. And let’s not ignore the mere reason that this fear for the young person from such book induced by the parent, teacher or even friends is due to the mere fact that they have never read such books and they have neither the interest nor the will to read such books. They find that the young person’s interest in such books or topics peculiar and not useful. The young person then, of course, will themself lonesome and unhappy. For they seek to meet others with similar interests.&lt;/p&gt;

&lt;p&gt;This is often the case for individuals with intellectual and artistic interests alike. This is actually the reason brilliant students who were born in small towns often apply for universities that are far away, for the subjects they wish to study are not offered by the nearby small universities. These students often move to larger cities where they can find better more specialized education offered by people who share the same interest in these subjects and arts.&lt;/p&gt;

&lt;p&gt;They often find in universities other individuals who seek to learn the same subjects. This sparks enjoyable and fruitful conversations. These conversations embark a great deal of happiness. They share internal jokes. They agree and also disagree on opinions. They share and compare sources of knowledge. These experiences would have been impossible in their original environment of the small town.&lt;/p&gt;

&lt;p&gt;If one is lucky then after university, one can find a job in the same field. In such a job one should meet other individuals who share the same interests and the same enjoyable experiences should continue. Such jobs, of course, will hardly be found in the small town they were born in. The more specialized the fields of interest are, the more these jobs lean to be located only in big cities with immense communities and specialized fields of trades.&lt;/p&gt;

&lt;p&gt;These individuals clearly outgrow their hometowns They clearly grow at a rate that is faster than the rate of the small towns they were born in. Other individuals however find no reason to be unhappy in their small towns. Why would they, if all they seek is to be found near enough. The professions they seek do exist in that small town. All the knowledge they wish to acquire can be well acquired in the nearby schools and libraries. They hardly feel lonely due to the lack of people with similar interests because they can find a reasonable number of people who share the same interests as they do. Those people are said to live within the boundaries of their hometowns&lt;/p&gt;

&lt;p&gt;Some individuals on the other hand, as explained by Bertrand Russel in his book “The conquest of happiness” Chapter IX, don’t have the means to move to big cities. For various reasons, they can’t move to attend a university far away from home. They can’t seek a job in the big city where they can meet individuals with similar artistic or intellectual interests. They know from books and papers that there are opportunities out there in the faraway big cities that suit their intellectual abilities, yet they find themselves incapable of seeking such opportunities due to various factors. Thus they remain unhappy and lonesome. Russel mentions that something needs to be done to help such individuals. Of course, Russel having died in 1970, hasn’t witnessed the effects of the internet and the opportunities it offers as a resolution for this dilemma.&lt;/p&gt;

&lt;h2&gt;
  
  
  The wonders of the internet
&lt;/h2&gt;

&lt;p&gt;We now of course have the privilege of the internet. A place where a young person with an interest in a subject that is not being taught in the nearby school is able to search for information in that subject online. An artist is able to follow other artists on social media platforms and scroll through the pages and pages of artwork that would never be found in the nearby art galleries.&lt;/p&gt;

&lt;p&gt;The internet has made it even possible to work entirely remotely from the comfort of your home for employers that reside in those big cities which you can not move to.&lt;/p&gt;

&lt;p&gt;On the internet, one can find communities specialized in any subject that comes to mind. One can satisfy curiosity regarding any subject and have those meaningful conversations one misses in the small towns. It’s even international. One can seek specialized fields of knowledge that only reside across the borders in different countries with ease.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ugly side
&lt;/h2&gt;

&lt;p&gt;The ugly side of the equation is that even though those specialized communities exist, one still feels lonesome. Despite being able to read through those Reddit threads discussing watches which is a recent obsession of mine, I still have the need to talk to someone about watches. I don’t understand why. Isn’t it enough that I’ve found those individuals and all this information that I would have never found in the local library of the small town I live in?&lt;/p&gt;

&lt;p&gt;Don’t get me wrong. I’m still grateful for the opportunity to have an internet access. Despite the hardships of remote work, I’m grateful for the opportunity to work with an international company from the comfort of my own home. I’m happy about the fact that I can meet those interesting people from all over the world through the internet. Despite the ugliness and the nonmagical experience of browsing through the kindle store for a book to read, I’m still grateful for the mere fact that I can browse and read those books which I could have never been able to read in the local libraries or bookshops. Nonetheless, something is missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  On being active
&lt;/h2&gt;

&lt;p&gt;I’m starting to think that the reason that I find the web to become recently tedious and boring is the fact that I’m hardly active. I don’t post on social media. I don’t share stories or articles. I hardly connect with people on Reddit, Twitter, or Goodreads. I only resort to scrolling and reading through the various things I find only without reacting, commenting, or posting something in return. I passively watch videos on youtube for hours without any action taken on my part. I came to realize that this is not a proper way of communication. I’m being passive. I’m using the internet as a method of communication to the proper extent. I’m only being on the receiving end of information. To phrase it as Russel has done, I know through the internet that people with similar interests do exist in some other places, but I can not converse or indulge in conversations with them. As it was the case of that individual who lived in the small town he grew up in and found an interest through the available books that is not shared with any near person.&lt;/p&gt;

&lt;p&gt;As a matter of fact, the internet has far more resources on any topic than a library in a small town could ever have. So with the wider access to information that the internet makes me capable of, and with me being passive and only consuming resources found online without getting involved in online communities. I’m far worse. As I’ve far outgrown my town in the subjects I care about than I could possibly have with no internet access.&lt;/p&gt;

</description>
      <category>thoughts</category>
      <category>internet</category>
      <category>happiness</category>
      <category>philosophy</category>
    </item>
    <item>
      <title>On C++ Memory Management</title>
      <dc:creator>Ahmed Abouzied</dc:creator>
      <pubDate>Mon, 30 Mar 2020 02:15:29 +0000</pubDate>
      <link>https://dev.to/ahmedaabouzied/on-c-memory-management-8bb</link>
      <guid>https://dev.to/ahmedaabouzied/on-c-memory-management-8bb</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Due to it's being type unsafe, it's very difficult to develop a garbage collector for c++. It's rather something that the C++ community often boosts about. It's true that it makes the compiled programs very lean and smaller in size. However, it adds work to the development process. You'd have to handle the memory yourself as a c++ developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of memory
&lt;/h2&gt;

&lt;p&gt;There are mainly three types of memory that can be used by the developer :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global Memory.&lt;/li&gt;
&lt;li&gt;Stack Memory.&lt;/li&gt;
&lt;li&gt;Heap Memory.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Global memory&lt;/strong&gt; is allocated by the operating system loader. It exists in two different locations: &lt;code&gt;.bss&lt;/code&gt; (uninitialized memory) and &lt;code&gt;.data&lt;/code&gt; (value initialized memory).&lt;/p&gt;

&lt;p&gt;These are the global variables declared outside of function scopes in c++. The following example is from the great book "Hands-On System Programming With C++"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bss_mem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;data_mem&lt;/span&gt; &lt;span class="o"&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="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&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="n"&gt;bss_mem&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// ==&amp;gt; 0&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;data_mem&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ==&amp;gt; 42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Global memory specially value-initialized memory increases the size of the program on disk, therefore longer loading time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack memory&lt;/strong&gt; stores values in the order they are declared and these values are removed in the opposite order. Applying "last in first out" such as regular stack data structures. Thus all data stored on the stack must have known, fixed size.&lt;/p&gt;

&lt;p&gt;When a function is called, it's pushed to the memory stack. All variables declared in that function are pushed to the stack right after the function one after the other. When that function returns, items are popped out of the stack one after the other until the function is popped. This is basically how the function scope is implemented.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Heap memory&lt;/strong&gt; stores data with a size that might change. The heap is less organized. When data is put on the heap, the program requests a certain amount of space from the operating system. The operating system responds with an empty spot on the stack that is big enough, marks it as being in use and returns a pointer. Accessing data in the heap is usually slower than accessing the stack because you'd have to follow a pointer which means that you're jumping around in memory.&lt;/p&gt;

&lt;p&gt;The process is described with very decent analogy on the rust-lang documentation website as follows :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of being seated at a restaurant. When you enter, you state the number of people in your group, and the staff finds an empty table that fits everyone and leads you there. If someone in your group comes late, they can ask where you’ve been seated to find you.&lt;/p&gt;

&lt;p&gt;consider a server at a restaurant taking orders from many tables. It’s most efficient to get all the orders at one table before moving on to the next table. Taking an order from table A, then an order from table B, then one from A again, and then one from B again would be a much slower process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Heap Allocation &amp;amp; Deallocation
&lt;/h2&gt;

&lt;p&gt;Managing heap memory in c++ is through the &lt;code&gt;new()&lt;/code&gt; and &lt;code&gt;delete()&lt;/code&gt; operator functions as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;iostream&amp;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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&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;// allocating the variable on the heap and returning a pointer.&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;ptr&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ==&amp;gt; 42&lt;/span&gt;
    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// releasing the memory.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This technique results in possible errors such as forgetting to call &lt;code&gt;delete()&lt;/code&gt; to release the memory or calling &lt;code&gt;delete()&lt;/code&gt; instead of &lt;code&gt;delete []&lt;/code&gt; when de-allocating an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ownership
&lt;/h2&gt;

&lt;p&gt;In C++11 the concept of pointer ownership was introduced to make it easier and less error-prone to manage the heap memory. The following two classes were introduced to achieve this :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;std::unique_ptr{}&lt;/code&gt; : A pointer uniquely owned by a single entity.&lt;br&gt;
This pointer is automatically deallocated by the compiler when it gets out of scope. Copying this pointer is not allowed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;std::shared_ptr{}&lt;/code&gt;: A pointer that may be owned by more than one entity. This pointer is deallocated automatically when all owners have released ownership of it. Copying this pointer is allowed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's discouraged by the C++ guidelines to perform any dynamic memory allocations by other than those two classes. Thus &lt;code&gt;std::unique_ptr{}&lt;/code&gt; should be used instead of &lt;code&gt;new()&lt;/code&gt; and &lt;code&gt;delete()&lt;/code&gt;. To prevent memory leaks.&lt;/p&gt;

&lt;p&gt;The previous example should be changed to be the following :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;memory&amp;gt;
#include &amp;lt;iostream&amp;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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&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;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="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;ptr&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ==&amp;gt; 42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The following functions can be used to create unique and shared pointers :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::make_unique&amp;lt;type&amp;gt;(args)&lt;/code&gt; to create &lt;code&gt;std::unique_ptr{}&lt;/code&gt; that allocates and deallocates a single object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::make_unique&amp;lt;type[]&amp;gt;(size)&lt;/code&gt; to create &lt;code&gt;std::unique_ptr{}&lt;/code&gt; that performs array allocation and deallocations.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::make_shared()&lt;/code&gt; to create &lt;code&gt;std::shared_ptr{}&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Unique Pointer Class
&lt;/h3&gt;

&lt;p&gt;It should be noted that &lt;code&gt;std::unique_ptr{}&lt;/code&gt; accepts a pointer previously allocated by &lt;code&gt;new()&lt;/code&gt; and on destruction, releases the memory it was given via &lt;code&gt;delete&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The unique pointer can release it's allocation back to the heap on demand by calling the &lt;code&gt;reset()&lt;/code&gt; function upon it. It explicitly tells the pointer to release its memory on demand without having to lose scope. It's demonstrated in the following example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;memory&amp;gt;
#include &amp;lt;iostream&amp;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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&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;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ==&amp;gt; 0x55bcfa2b1e70&lt;/span&gt;
    &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reset&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="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ==&amp;gt; 0 (null pointer)&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;ptr&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="c1"&gt;// the boolean operator is used to check if the pointer is null&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;ptr&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&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;



&lt;h4&gt;
  
  
  The Shared Pointer Class
&lt;/h4&gt;

&lt;p&gt;Consider that you have two threads T1, T2. T1 creates a pointer and thus owns it. T2 has to use that pointer from T1 and thus owns it as much as T1.&lt;/p&gt;

&lt;p&gt;Handling this with &lt;code&gt;new()&lt;/code&gt; and &lt;code&gt;delete()&lt;/code&gt; can be quite messy since it's difficult to predict if the other thread is no longer using the pointer and therefore difficult to determine when to release the memory and from which thread this should be done.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;std::shared_pt{}&lt;/code&gt; solves this problem with ease as in the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;thread&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;memory&amp;gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;myclass&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;m_data&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;myclass&lt;/span&gt;&lt;span class="p"&gt;()&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;"myclass deleted&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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;void&lt;/span&gt; &lt;span class="n"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;m_data&lt;/span&gt;&lt;span class="o"&gt;++&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="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="n"&gt;t1&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="kr"&gt;thread&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;thread2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;shared_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;myclass&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;();&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;"thread2: complete&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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;void&lt;/span&gt; &lt;span class="nf"&gt;thread1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&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;make_shared&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;myclass&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thread2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;();&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;"thread1 : complete&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thread1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// &amp;gt; g++ -std=c++17 -lpthread main.cpp; ./a.out&lt;/span&gt;
&lt;span class="c1"&gt;// thread1: complete&lt;/span&gt;
&lt;span class="c1"&gt;// thread2: complete&lt;/span&gt;
&lt;span class="c1"&gt;// myclass deleted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>cpp</category>
      <category>memorymanagement</category>
      <category>lowlevel</category>
    </item>
  </channel>
</rss>
