<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Khelll&#039;s Blog &#187; C++</title>
	<atom:link href="http://khelll.com/blog/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://khelll.com/blog</link>
	<description>Cool Web Development...</description>
	<lastBuildDate>Thu, 13 Oct 2011 03:48:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>C++ passes by reference, Java and Ruby don&#8217;t</title>
		<link>http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/</link>
		<comments>http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 08:17:42 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/' addthis:title='C++ passes by reference, Java and Ruby don&#8217;t '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I got a lot of comments for the previous article, that was explaining how Ruby passes by value, just like Java does. I thought that showing a simple example implemented in C++, Java and Ruby will clarify the idea. In &#8230; <a href="http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/' addthis:title='C++ passes by reference, Java and Ruby don&#8217;t' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/' addthis:title='C++ passes by reference, Java and Ruby don&#8217;t '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>I got a lot of comments for the <a href="http://www.khelll.com/blog/ruby/ruby-pass-by-value-or-by-reference/">previous article</a>, that was explaining how Ruby passes by value, just like Java does.</p>
<p>I thought that showing a simple example implemented  in C++, Java and Ruby will clarify the idea. In the following lines, we will make swap functionality in 3 languages, and demonstrate that neither Java nor Ruby passes by reference.</p>
<p>C++</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Point <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">double</span> x, y<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
&nbsp;
    Point<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> x, <span style="color: #0000ff;">double</span> y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>x <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span>
        this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>y <span style="color: #000080;">=</span> y<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">void</span> print<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;x = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> x <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, y = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> y <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Parameters are passed by reference</span>
&nbsp;
<span style="color: #0000ff;">void</span> swap<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">&amp;</span>p1, Point <span style="color: #000040;">&amp;</span>p2<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    Point temp <span style="color: #000080;">=</span> p1<span style="color: #008080;">;</span>
    p1 <span style="color: #000080;">=</span> p2<span style="color: #008080;">;</span>
    p2 <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//initializing the points</span>
    Point val1<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span>, <span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Point val2<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">20</span>, <span style="color: #0000dd;">40</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//printing their values</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Before Passing By Reference&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> val1.<span style="color: #007788;">print</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> val2.<span style="color: #007788;">print</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//swaping, passing by referecne</span>
    swap<span style="color: #008000;">&#40;</span>val1, val2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//printing the values again</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;After Passing By Reference&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> val1.<span style="color: #007788;">print</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> val2.<span style="color: #007788;">print</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>passcpp
Before Passing By Reference
x = <span style="color: #000000;">5</span>, y = <span style="color: #000000;">10</span>
x = <span style="color: #000000;">20</span>, y = <span style="color: #000000;">40</span>
After Passing By Reference
x = <span style="color: #000000;">20</span>, y = <span style="color: #000000;">40</span>
x = <span style="color: #000000;">5</span>, y = <span style="color: #000000;">10</span></pre></div></div>

<p>Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Point</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> x,  y<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> x, <span style="color: #000066; font-weight: bold;">double</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x = %.2f , y = %.2f&quot;</span>, x, y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> swap<span style="color: #009900;">&#40;</span><span style="color: #003399;">Point</span> p1,<span style="color: #003399;">Point</span> p2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Point</span> temp <span style="color: #339933;">=</span> p1<span style="color: #339933;">;</span>
        p1 <span style="color: #339933;">=</span> p2<span style="color: #339933;">;</span>
        p2 <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//initializing the points</span>
        <span style="color: #003399;">Point</span> var1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Point</span> var2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//printing their values</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Before Passing<span style="color: #000099; font-weight: bold;">\n</span>%s<span style="color: #000099; font-weight: bold;">\n</span>%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, var1, var2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// //swaping, passing by reference value, aka: pass by value</span>
        swap<span style="color: #009900;">&#40;</span>var1,var2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//printing the values again</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;After Passing<span style="color: #000099; font-weight: bold;">\n</span>%s<span style="color: #000099; font-weight: bold;">\n</span>%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, var1, var2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ java Point
Before Passing By Reference
x = <span style="color: #000000;">5.00</span> , y = <span style="color: #000000;">10.00</span>
x = <span style="color: #000000;">20.00</span> , y = <span style="color: #000000;">40.00</span>
After Passing By Reference
x = <span style="color: #000000;">5.00</span> , y = <span style="color: #000000;">10.00</span>
x = <span style="color: #000000;">20.00</span> , y = <span style="color: #000000;">40.00</span></pre></div></div>

<p>Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Point
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>x,y<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@x</span>,@y = x,y
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_s
    <span style="color:#996600;">&quot;x = #{@x} , y = #{@y}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> swap<span style="color:#006600; font-weight:bold;">&#40;</span>p1,p2<span style="color:#006600; font-weight:bold;">&#41;</span>
  p1,p2 = p2,p1
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#initializing the points</span>
var1 = Point.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span>,<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>
var2 = Point.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">20</span>,<span style="color:#006666;">40</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#printing their values</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Before Passing&quot;</span> , var1, var2
<span style="color:#008000; font-style:italic;"># //swaping, passing by reference value, aka: pass by value</span>
swap var1,var2
<span style="color:#008000; font-style:italic;">#printing the values again</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;After Passing&quot;</span> , var1, var2</pre></div></div>

<p>Output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ ruby passruby.rb
Before Passing By Reference
x = <span style="color: #000000;">5</span> , y = <span style="color: #000000;">10</span>
x = <span style="color: #000000;">20</span> , y = <span style="color: #000000;">40</span>
After Passing By Reference
x = <span style="color: #000000;">5</span> , y = <span style="color: #000000;">10</span>
x = <span style="color: #000000;">20</span> , y = <span style="color: #000000;">40</span></pre></div></div>

<p>As you can notice, in c++ example, passing by reference, will do the swap successfully.<br />
Java and Ruby both don&#8217;t pass by reference, instead they pass a copy of the reference, which is a value finally, and so the swap fails, as swapping the copies doesn&#8217;t swap the original passed objects.</p>
<p>Please note also that in Java, primitive types are passed by their values directly and no need for any kind of references copies.The same case applies for Ruby, with immediate types(int, char&#8230;).</p>
<p>According to the previous 2 facts, we conclude that neither Java nor Ruby passes by reference, instead, both pass by value.</p>
<p>I think that fact that Ruby MRI is using C means they can&#8217;t use references, and so they are using pointers, and that explains why swap fails. </p>
<p>I hope that the idea is clear now.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/' addthis:title='C++ passes by reference, Java and Ruby don&#8217;t' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/feed/</wfw:commentRss>
		<slash:comments>141</slash:comments>
		</item>
	</channel>
</rss>

