 
    
<?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>crash Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/tag/crash/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/tag/crash/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Tue, 07 Jun 2011 19:23:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Programming Error &#8211; Incorrect Array Declaration</title>
		<link>https://complete-concrete-concise.com/programming/c-programming/programming-error-incorrect-array-declaration/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Tue, 07 Jun 2011 19:23:19 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[corrupt]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[random]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/programming/c-programming/programming-error-incorrect-array-declaration</guid>

					<description><![CDATA[<p>In C++, we can declare an array by using the new operator: char *p = new char[50]; // dynamically allocate an array of 50 char This is useful in functions when we don&#8217;t know in advance how large the array should be or if we want to allocate the array in the heap instead of [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c-programming/programming-error-incorrect-array-declaration/">Programming Error &#8211; Incorrect Array Declaration</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In C++, we can declare an array by using the <code><strong>new</strong></code> operator:</p>
<div class="c2"><code>char *p = new char[50]; // dynamically allocate an array of 50 char</code></div>
<p>This is useful in functions when we don&#8217;t know in advance how large the array should be  or if we want to allocate the array in the heap instead of the stack (the equivalent in C would be to use malloc() or calloc() to allocate the array)</p>
<p>A common programming error (because it looks very similar and is perfectly valid) is to replace the [] with ().</p>
<div class="c2"><code>char *p = new char(50);</code></div>
<p>In this case, we have allocated a single char and initialized its value to 50 &#8211; which is not what was intended.</p>
<p>This leads to those hard to track down bugs of corrupted data and / or random crashes.</p>
<p><strong>NOTE:</strong> when freeing an array created with <code><strong>new</strong></code>, remember to call <strong><code>delete[]</code></strong> instead of <code><strong>delete</strong></code> to avoid memory leaks (and definitely never call <code><strong>free()</strong></code>)</p>

<p>The post <a href="https://complete-concrete-concise.com/programming/c-programming/programming-error-incorrect-array-declaration/">Programming Error &#8211; Incorrect Array Declaration</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
