<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: C++ Memory Allocations</title>
	<atom:link href="http://www.techartifact.com/blogs/2009/04/c-memory-allocations.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.techartifact.com/blogs/2009/04/c-memory-allocations.html</link>
	<description>Latest tip and information on Java and Oracle ADF</description>
	<lastBuildDate>Thu, 09 Feb 2012 07:11:23 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: nirmal</title>
		<link>http://www.techartifact.com/blogs/2009/04/c-memory-allocations.html/comment-page-1#comment-14</link>
		<dc:creator>nirmal</dc:creator>
		<pubDate>Mon, 06 Apr 2009 10:46:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.techartifact.com/blogs/?p=304#comment-14</guid>
		<description>Hi Ankit,
    First of all sorry for replying u late.

Before answering your question, I hope that you have good idea of practical usage of references in c++.


If not then read this:
References are like alias of an entity.
For example:
&lt;code&gt; int p = 10;
 int &amp;a = p;&lt;/code&gt;
Here &lt;strong&gt;a&lt;/strong&gt;&#039;s address and content is similar to that of &lt;strong&gt;p&lt;/strong&gt;.

Next is a simple example which will make you clear of your confusion regarding references.

&lt;code&gt;int a=10;
int *b=NULL;
int &amp;c=a;
cout&lt;&lt;&amp;a&lt;&lt;endl;
cout&lt;&lt;&amp;b&lt;&lt;endl;
cout&lt;&lt;&amp;c&lt;&lt;endl;&lt;/code&gt;



Output:


0012FF6C

0012FF68

0012FF6C





In this example we have three variables (a,b and c)

a - is a int variable;

b - is a integer pointer (pointing to NULL, not initialized)

c - is a reference to the address of a



Look at the output, you will make it out that a &amp; b are having different addresses

but &lt;strong&gt;c&lt;/strong&gt;&#039;s address = &lt;strong&gt;a&lt;/strong&gt;&#039;s address (obviously, &lt;strong&gt;c&lt;/strong&gt; is a reference to &lt;strong&gt;a&lt;/strong&gt;).



So from output it is also clear that &lt;strong&gt;a&lt;/strong&gt; &amp; &lt;strong&gt;b&lt;/strong&gt; both are occupying some memory in stack.

Now you might be having some doubt about &lt;strong&gt;b&lt;/strong&gt;. 

&lt;strong&gt;b&lt;/strong&gt; Itself is a  variable of type unsigned integer holding a value (NULL or 0).(all pointers are of type unsigned int)

But if we intialize &lt;strong&gt;b&lt;/strong&gt; (b = new int) then &lt;strong&gt;b&lt;/strong&gt; will hold a value or an address of 

an allocated memory in heap, which means it will now pointing to some memory allocated in heap.



What about &lt;strong&gt;c&lt;/strong&gt;? It is showing same address and content as of &lt;strong&gt;a&lt;/strong&gt;. 

Then where does it occupies the memory.



To get some overview about we will go through an another example:



Arrangle all three variables in a structure.



&lt;code&gt;struct some_struct

{

	int a;

	int *b;

	int &amp;c;

};



int main()

{

	some_struct sp = {10,NULL,sp.a};

	cout&lt;&lt;sizeof(sp);

	return 0;

}
&lt;/code&gt;


Output:



12



Looking at the output, we can make it out that &lt;strong&gt;c&lt;/strong&gt; is also occupying some memory in stack,

(4 bytes for a + 4 bytes for b and 4 bytes for c.)

but we can&#039;t reflect it using address operator(&amp;).





That&#039;s it , I hope this explanation is helpful to you.





Nirmal</description>
		<content:encoded><![CDATA[<p>Hi Ankit,<br />
    First of all sorry for replying u late.</p>
<p>Before answering your question, I hope that you have good idea of practical usage of references in c++.</p>
<p>If not then read this:<br />
References are like alias of an entity.<br />
For example:<br />
<code> int p = 10;<br />
 int &amp;a = p;</code><br />
Here <strong>a</strong>&#8216;s address and content is similar to that of <strong>p</strong>.</p>
<p>Next is a simple example which will make you clear of your confusion regarding references.</p>
<p><code>int a=10;<br />
int *b=NULL;<br />
int &amp;c=a;<br />
cout&lt;&lt;&amp;a&lt;&lt;endl;<br />
cout&lt;&lt;&amp;b&lt;&lt;endl;<br />
cout&lt;&lt;&amp;c&lt;&lt;endl;</code></p>
<p>Output:</p>
<p>0012FF6C</p>
<p>0012FF68</p>
<p>0012FF6C</p>
<p>In this example we have three variables (a,b and c)</p>
<p>a &#8211; is a int variable;</p>
<p>b &#8211; is a integer pointer (pointing to NULL, not initialized)</p>
<p>c &#8211; is a reference to the address of a</p>
<p>Look at the output, you will make it out that a &amp; b are having different addresses</p>
<p>but <strong>c</strong>&#8216;s address = <strong>a</strong>&#8216;s address (obviously, <strong>c</strong> is a reference to <strong>a</strong>).</p>
<p>So from output it is also clear that <strong>a</strong> &amp; <strong>b</strong> both are occupying some memory in stack.</p>
<p>Now you might be having some doubt about <strong>b</strong>. </p>
<p><strong>b</strong> Itself is a  variable of type unsigned integer holding a value (NULL or 0).(all pointers are of type unsigned int)</p>
<p>But if we intialize <strong>b</strong> (b = new int) then <strong>b</strong> will hold a value or an address of </p>
<p>an allocated memory in heap, which means it will now pointing to some memory allocated in heap.</p>
<p>What about <strong>c</strong>? It is showing same address and content as of <strong>a</strong>. </p>
<p>Then where does it occupies the memory.</p>
<p>To get some overview about we will go through an another example:</p>
<p>Arrangle all three variables in a structure.</p>
<p><code>struct some_struct</p>
<p>{</p>
<p>	int a;</p>
<p>	int *b;</p>
<p>	int &#038;c;</p>
<p>};</p>
<p>int main()</p>
<p>{</p>
<p>	some_struct sp = {10,NULL,sp.a};</p>
<p>	cout&lt;&lt;sizeof(sp);</p>
<p>	return 0;</p>
<p>}<br />
</code></p>
<p>Output:</p>
<p>12</p>
<p>Looking at the output, we can make it out that <strong>c</strong> is also occupying some memory in stack,</p>
<p>(4 bytes for a + 4 bytes for b and 4 bytes for c.)</p>
<p>but we can&#8217;t reflect it using address operator(&amp;).</p>
<p>That&#8217;s it , I hope this explanation is helpful to you.</p>
<p>Nirmal</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ankit</title>
		<link>http://www.techartifact.com/blogs/2009/04/c-memory-allocations.html/comment-page-1#comment-13</link>
		<dc:creator>Ankit</dc:creator>
		<pubDate>Sun, 05 Apr 2009 15:14:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.techartifact.com/blogs/?p=304#comment-13</guid>
		<description>I must say its a very infomative article and is a must read for those who want to clear their concepts of memory allocation.

Can you plss mention where are reference types and value types stored on he memory in C++? I mean on the heap or stack.

As i know if we write a program using C#.net reference types are stored on stack while value types are stored on heap.</description>
		<content:encoded><![CDATA[<p>I must say its a very infomative article and is a must read for those who want to clear their concepts of memory allocation.</p>
<p>Can you plss mention where are reference types and value types stored on he memory in C++? I mean on the heap or stack.</p>
<p>As i know if we write a program using C#.net reference types are stored on stack while value types are stored on heap.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

