<?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: Shintaro Kaneko</title>
    <description>The latest articles on DEV Community by Shintaro Kaneko (@kaneshin).</description>
    <link>https://dev.to/kaneshin</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%2F53168%2Fef32dd58-0f5b-4fc6-8f82-3ce70370c976.png</url>
      <title>DEV Community: Shintaro Kaneko</title>
      <link>https://dev.to/kaneshin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaneshin"/>
    <language>en</language>
    <item>
      <title>Dangling Pointer between C and Go</title>
      <dc:creator>Shintaro Kaneko</dc:creator>
      <pubDate>Mon, 22 May 2023 04:10:00 +0000</pubDate>
      <link>https://dev.to/kaneshin/dangling-pointer-between-c-and-go-2fd8</link>
      <guid>https://dev.to/kaneshin/dangling-pointer-between-c-and-go-2fd8</guid>
      <description>&lt;p&gt;In this post, I'm going to tell you about how to handle Dangling Pointer and understand the difference of behavior between C and Go.&lt;/p&gt;

&lt;h1&gt;
  
  
  Dangling Pointer
&lt;/h1&gt;

&lt;p&gt;Dangling point is a pointer that doesn’t point to a valid object of the appropriate type. It doesn’t happen in modern programming language such as Go, Rust, etc but it usually happens in three scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;De-allocation: If a pointer is pointing to a memory location of an object, but the object either has been deleted or de-allocated.&lt;/li&gt;
&lt;li&gt;Function Call: If a pointer is pointing to a local variable of a function and the function call gets over. &lt;/li&gt;
&lt;li&gt;Variable goes out of scope: If a pointer is pointing to a variable that has gone out of scope.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The pointer of these scenarios is known as a dangling pointer.&lt;/p&gt;

&lt;p&gt;Dangling pointers can lead to undefined behavior in a program, including program crashes, data corruption, or memory leaks. To avoid this, we should always set pointers to null after they are freed, and avoid using pointers that point to local variables outside the scope of their function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Garbage Collector
&lt;/h2&gt;

&lt;p&gt;Go has the Garbage Collector, and all variables that refer to an object are deleted, the object will be automatically deleted by system. However, C doesn’t have the Garbage Collector, which means programmers must handle allocated objects by themselves. If they use &lt;code&gt;malloc&lt;/code&gt; to allocate memory dynamically, they have to take care of the memory’s scope and living period until the memory is valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  C code
&lt;/h2&gt;

&lt;p&gt;In terms of the following code, the variable &lt;code&gt;r&lt;/code&gt; of the &lt;code&gt;new_rect&lt;/code&gt; function is expected to be used outside the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="c1"&gt;// rect represents the coordinate of rectangle.&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// new_rect returns a reference of a variable of rect.&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;new_rect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt; &lt;span class="n"&gt;r&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&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;r&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="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_rect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&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="mi"&gt;3&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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"address = %p&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" (x, y) = (%f, %f)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&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;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="c1"&gt;// address = 0x16d1c3140&lt;/span&gt;
&lt;span class="c1"&gt;//  (x, y) = (3021218960012547179075862528.00, 0.00)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;r&lt;/code&gt; that exists in the &lt;code&gt;new_rect&lt;/code&gt; function is deleted when the function call ends. It turns out, the function retuns an address that has already been invalid. The pointer &lt;code&gt;rp&lt;/code&gt; points to the invalid local address and the dangling pointer error occurs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go code
&lt;/h2&gt;

&lt;p&gt;However, in the following Go code, the variable &lt;code&gt;rp&lt;/code&gt; in the main function points to a memory address of the variable &lt;code&gt;r&lt;/code&gt; that is defined in the &lt;code&gt;NewRect&lt;/code&gt; function and has NOT been deleted. So, dangling pointer error doesn’t occur.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="c"&gt;// Rect represents the coordinate of rectangle.&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Rect&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="kt"&gt;float32&lt;/span&gt;
    &lt;span class="n"&gt;Y&lt;/span&gt; &lt;span class="kt"&gt;float32&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// NewRect returns a reference of a variable of Rect.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kt"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Rect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Rect&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&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;r&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&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;rp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;NewRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"address = %p&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" (x, y) = (%f, %f)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c"&gt;// Output:&lt;/span&gt;
&lt;span class="c"&gt;// address = 0x1400011c008&lt;/span&gt;
&lt;span class="c"&gt;//  (x, y) = (1.20, 3.40)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s all there is.&lt;/p&gt;

</description>
      <category>c</category>
      <category>go</category>
      <category>pointer</category>
      <category>danglingpointer</category>
    </item>
    <item>
      <title>SSH: Remove identified keys by hostname</title>
      <dc:creator>Shintaro Kaneko</dc:creator>
      <pubDate>Mon, 09 Mar 2020 13:24:48 +0000</pubDate>
      <link>https://dev.to/kaneshin/ssh-remove-identified-keys-by-hostname-44ef</link>
      <guid>https://dev.to/kaneshin/ssh-remove-identified-keys-by-hostname-44ef</guid>
      <description>&lt;h2&gt;
  
  
  Failed to connect to the host via ssh.
&lt;/h2&gt;

&lt;p&gt;Have you ever seen such a error which is "Failed to connect to the host via ssh." on your terminal when connecting a host via ssh. It should be happened if known_hosts file is taken a wrong identified keys belonging to the host.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh foobar@foobar.com
Failed to connect to the host via ssh.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  -R hostname
&lt;/h2&gt;

&lt;p&gt;To clear the problem you may take away the identified keys with ssh-keygen command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ man ssh-keygen

...
-R hostname
        Removes all keys belonging to hostname from a known_hosts file.  This option is useful to
        delete hashed hosts (see the -H option above).
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use f option to specify a known_hosts files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh-keygen -f ~/.ssh/known_hosts -R foobar.com
# No more
# rm -f ~/.ssh/known_hosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it! :)&lt;/p&gt;



</description>
      <category>ssh</category>
    </item>
    <item>
      <title>Troubleshooting `ld: library not found for -lssl'</title>
      <dc:creator>Shintaro Kaneko</dc:creator>
      <pubDate>Mon, 24 Feb 2020 04:52:15 +0000</pubDate>
      <link>https://dev.to/kaneshin/troubleshooting-ld-library-not-found-for-lssl-198l</link>
      <guid>https://dev.to/kaneshin/troubleshooting-ld-library-not-found-for-lssl-198l</guid>
      <description>&lt;h2&gt;
  
  
  Trouble
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;linking shared-object mysql2/mysql2.bundle                                                              
ld: library not found for -lssl                                                                         
clang: error: linker command failed with exit code 1 (use -v to see invocation)                                             
make: *** [mysql2.bundle] Error 1                                                                       

make failed, exit code 2                            
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I got the error during executing &lt;code&gt;gem install mysql2&lt;/code&gt;. It seems that the linkable library path is not configured correctly on Mojave.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shooting
&lt;/h2&gt;

&lt;p&gt;Invoking the command with &lt;code&gt;LIBRARY_PATH&lt;/code&gt; to be able to find out the openssl library explicitly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib gem install mysql2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's all. 😎&lt;/p&gt;

</description>
      <category>note</category>
      <category>troubleshooting</category>
    </item>
  </channel>
</rss>
