<?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>Code Library &#187; statck</title>
	<atom:link href="http://www.ucosoft.com/tag/statck/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ucosoft.com</link>
	<description>Free Source Code and Program Tips</description>
	<lastBuildDate>Thu, 22 Jul 2010 05:17:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Stack vs. Heap</title>
		<link>http://www.ucosoft.com/stack-vs-heap.html</link>
		<comments>http://www.ucosoft.com/stack-vs-heap.html#comments</comments>
		<pubDate>Fri, 11 Jan 2008 16:52:11 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[Program]]></category>
		<category><![CDATA[statck]]></category>
		<category><![CDATA[STL]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2008/01/12/stack-vs-heap.html</guid>
		<description><![CDATA[Here is a simple rule when to define objects on the heap and when to do it on the stack. The stack is much more efficient than the heap. Simple rule: Use the stack when you can. You cannot use the stack if you need the object in other functions. (Stack allocations are deleted when [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Here is a simple rule when to define objects on the heap and when to do it on the stack.</strong></em>
<p>The stack is much more efficient than the heap.
<p>Simple rule: <strong>Use the stack when you can</strong>.
<p>You cannot use the stack if you need the object in other functions. (Stack allocations are deleted when the function that allocated them returns.) </p>
<p>You cannot use the stack if you need a variable size. Stack allocations use a constant size that must be known at compile time. </p>
<p>When you define an object (class instance) on the stack, it is automatically deleted (its destructor is called) when the class instance goes out of scope.
<p>Instead, when you allocate on the heap, you must pay attention to properly cleanup the object (else you will have memory or resources leaks).
<p>For example: </p>
<p><span id="more-104"></span></p>
<p>
<p>&lt;code&gt;
<p>&nbsp; // ** Stack Allocation ** <br />&nbsp; { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Allocate MyClass instance on the stack <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyClass c;
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230; use c
<p>&nbsp; } // c destructor called by C++ compiler
<p>&nbsp; // ** Heap Allocation ** <br />&nbsp; { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Allocate on the heap <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyClass * ptrC = new MyClass();
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230;use heap-allocated instance <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptrC-&gt;DoSomething(&#8230;);
<p>&nbsp; } // Destructor *not* called here! Memory leak! <br />&nbsp;&nbsp; // Must do an explicit delete ptrC;
<p>&lt;/code&gt;
<p>However, <strong>if you use smart pointers, like boost:: or tr1:: shared_ptr, you can safely allocate on the heap, and let the smart pointer pay attention to free the object it is referencing.</strong> (So, with shared_ptr, you can safely allocate on the heap, and kind of &#8220;forget&#8221; about freeing the object. It&#8217;s kind of C# or Java garbage collection&#8230;)
<p>Recently, Visual C++ Team released a kind of &#8220;Service Pack 0&#8243; for VS2008, which includes the useful shared_ptr class, and also other great things. I recall that David C. posted the link on this newsgroup recently. You can read something also here:
<p><a href="http://blogs.msdn.com/vcblog/archive/2007/12/26/just-what-is-this-tr1-thing.aspx">http://blogs.msdn.com/vcblog/archive/2007/12/26/just-what-is-this-tr1&#8230;</a>
<p>In general, if you have a small objects, you can allocate on the stack. Instead, if you want to allocate big memory, you should allocate on the heap (the stack is a limited resource, more limited than the heap). </p>
<p>Note also that there are classes like C++ STL container classes (e.g. std::vector) that you allocate on the stack. But the class implementation internally can allocate the required memory on the heap, e.g.: </p>
<p>&nbsp; std::vector&lt; double &gt; someData(1000);
<p>The vector class instance (&#8216;someData&#8217;) is allocated on the stack (so you don&#8217;t have to call its destructor explicitly), but the vector data (the 1000 double values) is allocated on the heap internally by std::vector implementation.
<p><strong>Referrence:</strong>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Heap_%28data_structure%29">http://en.wikipedia.org/wiki/Heap_%28data_structure%29</a>
<li><a href="http://en.wikipedia.org/wiki/Stack_(data_structure)">http://en.wikipedia.org/wiki/Stack_(data_structure)</a></li>
</ul>

	Tags: <strong><a href="http://www.ucosoft.com/tag/heap" title="heap" rel="tag">heap</a>, <a href="http://www.ucosoft.com/tag/program" title="Program" rel="tag">Program</a>, <a href="http://www.ucosoft.com/tag/statck" title="statck" rel="tag">statck</a>, <a href="http://www.ucosoft.com/tag/stl" title="STL" rel="tag">STL</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/stack-vs-heap.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

