<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>STL vectors vs. arrays, new[], and malloc().</title>
		<link>http://www.allegro.cc/forums/view/295855</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 25 Aug 2003 17:20:33 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My question is simple.<br />With all of the usefullness of the STL and its containters, is there really any need for good old fashion arrays and data allocated with new? I mean, I use vectors for pretty much everything, just because im so used to them and they are so easy to use. Is there anything wrong with this? Reguardless of what the C purists might say, I think that there isn&#39;t.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Amos)</author>
		<pubDate>Sat, 23 Aug 2003 08:01:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Well, there&#39;s a bit of a speed boost with straight arrays, depending on what you&#39;re doing. And if you&#39;re not using any of the fancy functionality, making lots of vectors when you could easily get by using lots of arrays can bloat up your program, I imagine. My tilemap class in my editor uses vectors internally because they&#39;re easy to edit, but I use arrays in the game engine for speed, and because I don&#39;t need the STL functionality anymore.</p><p>Different tools for different situations.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sat, 23 Aug 2003 08:13:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>With all of the usefullness of the STL and its containters, is there really any need for good old fashion arrays and data allocated with new?</p></div></div><p>

Yes.</p><p>Regular C-arrays have several advantages.</p><p>#1: They&#39;re directly supported by the language (std::vector is not part of C++, but of the C++ standard-library).<br />#2: They&#39;re very simple to use. Simpler than std::vector (no iterators).<br />#3: The performance of C-arrays is as fast as you can possibly get.<br />#4: You can&#39;t save std::vectors as memory images in a file and load them directly on multiple platforms without having to do intermediate allocations.<br />#5: Vector&#39;s are typed; you can&#39;t play certain games with them that you can with arrays.<br />#6: The conventions for passing arrays around is well known. The behavior of std::vector when it is an argument is implementation-dependent.</p><p>I use vectors either for important arrays that are part of classes or for arrays that are going to resize themselves. I find little need for them other than for these cases.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Korval)</author>
		<pubDate>Sat, 23 Aug 2003 10:37:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Korval said:</div><div class="quote"><p> #2: They&#39;re very simple to use. Simpler than std::vector (no iterators). </p></div></div><p>

The interface of an array is also supported by std::vector and iterators act like pointers (even more, they are implemented just as simple pointers sometimes for std::vector), so using an iterator is like using a pointer and using std::vector is like using a C-array.</p><p>std::vector is the choice when you need to use a self-resizeable container with random access (or even better, direct access) to any element of it in constant time.</p><p>If you already know the size (or limit) of the vector/array and you know it won&#39;t change, you can use a simple C-array and you&#39;ll still be able to use it in STL algorithms, in this case pointers to elements of the C-array are just random access iterators.</p><div class="quote_container"><div class="title">Korval said:</div><div class="quote"><p> #5: Vector&#39;s are typed; you can&#39;t play certain games with them that you can with arrays. </p></div></div><p>

I don&#39;t get your point. What kind of game can be made using a C-array but not with std::vector?</p><div class="quote_container"><div class="title">Korval said:</div><div class="quote"><p> #6: The conventions for passing arrays around is well known. The behavior of std::vector when it is an argument is implementation-dependent. </p></div></div><p>

Ppl should be used to receive objects as references (or pointer to them), in that case I don&#39;t see the &quot;implementation dependent&quot; behavior in there, they&#39;ll act the same way as if you were receiving a C-array:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> feed_me<span class="k2">(</span>std::vector<span class="k3">&lt;</span>int&gt; <span class="k3">&amp;</span> v<span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">return</span> <span class="k3">*</span>v.begin<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> <span class="k3">*</span><span class="k2">(</span>v.end<span class="k2">(</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>

<span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <span class="c">//...</span>
  std::vector<span class="k3">&lt;</span>int&gt; g<span class="k2">(</span><span class="n">10</span>, <span class="n">9</span><span class="k2">)</span><span class="k2">;</span>
  <span class="c">//...</span>
  std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> feed_me<span class="k2">(</span>g<span class="k2">)</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> std::endl<span class="k2">;</span>
  <span class="c">//...</span>
<span class="k2">}</span>
</pre></div></div><p>

If you didn&#39;t receive it as reference or pointer, you&#39;d get a copy of it, something you couldn&#39;t do with C-arrays without creating a few helper functions first.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sat, 23 Aug 2003 11:50:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> What kind of game can be made using a C-array but not with std::vector?</p></div></div><p>
You can cast C arrays to arrays of other types - the bmp-&gt;line[] pointer is such an array. std::vector makes no garentee on the linearity of the actual memory organization, just that it gives the appearence of linearity from the API (and speed/memory) point of view.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Sat, 23 Aug 2003 12:01:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The point is:<br />std:vector is like using an array.</p><p>gcc will even inline the [] operator calls, so you won&#39;t see a difference in the generated code (just tried it).</p><p>What you should do is to check what data structure you need. And then use it. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />If you need dynamic arrays use vector.<br />If you need static arrays use normal arrays.<br />If you need a stack, use the stack...</p><p>It&#39;s pretty straight forward.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sat, 23 Aug 2003 12:30:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Bob: afaik std::vector has the property of linearity and that&#39;s one of the reasons it could store less elements than, for example, a std::deque.</p><p>Maybe it&#39;s just common implementation.</p><p>Seems it&#39;s time for checking the C++ standard documnet <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>...</p><p>Just checked it and seems they don&#39;t clarify it (or maybe I just didn&#39;t see it).</p><p>Now you&#39;re making me doubt <img src="http://www.allegro.cc/forums/smileys/cry.gif" alt=":&#39;(" /></p><p>Bad Bob.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sat, 23 Aug 2003 12:35:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>The interface of an array is also supported by std::vector and iterators act like pointers (even more, they are implemented just as simple pointers sometimes for std::vector), so using an iterator is like using a pointer and using std::vector is like using a C-array.</p></div></div><p>

Iteraters are not pointers. If you treat them as pointers, you&#39;re begging for problems. Iteraters aren&#39;t guarenteed to be only a CPU word in size.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Ppl should be used to receive objects as references (or pointer to them), in that case I don&#39;t see the &quot;implementation dependent&quot; behavior in there, they&#39;ll act the same way as if you were receiving a C-array:</p></div></div><p>

What about the case of returning an array?</p><p>For a C-array, you just do:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> iArray<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">;</span>
<span class="k1">int</span> <span class="k3">*</span>FuncName<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">return</span> iArray<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

It is immediately obvious how to treat the return value. If you don&#39;t want someone to be able to change the return value, use a &#39;const int *FuncName()&#39;. You are guarenteed that no copy operation will result, and that any changes you make will stick.</p><p>However, what about:</p><div class="source-code snippet"><div class="inner"><pre>vector<span class="k3">&lt;</span>int&gt; iVec<span class="k2">;</span>
vector<span class="k3">&lt;</span>int&gt; <span class="k3">&amp;</span>FuncName<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">return</span> iVec<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Will this code result in a copy? Maybe; it depends on what they do on their end. It&#39;s far too easy for someone to do the wrong thing. Sure, you can do:</p><div class="source-code snippet"><div class="inner"><pre>vector<span class="k3">&lt;</span>int&gt; iVec<span class="k2">;</span>
vector<span class="k3">&lt;</span>int&gt; <span class="k3">*</span>FuncName<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">return</span> <span class="k3">*</span>iVec<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

to virtually guarentee that a copy operation doesn&#39;t occur. However, if you&#39;ve ever worked with a std::vector*, you&#39;ll know it&#39;s really annoying and ugly (-&gt;[] is not a normal thing, and adding in a lot of (*) is also annoying).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>If you didn&#39;t receive it as reference or pointer, you&#39;d get a copy of it</p></div></div><p>

Do you? Do std::vector&#39;s have copy symantics? Are you sure? Are you sure that some implementations aren&#39;t allowed to implicitly reference vectors?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Korval)</author>
		<pubDate>Sat, 23 Aug 2003 14:26:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Korval said:</div><div class="quote"><p> Iteraters are not pointers. If you treat them as pointers, you&#39;re begging for problems. Iteraters aren&#39;t guarenteed to be only a CPU word in size. </p></div></div><p>

Iterators for std::vector can be made with simple pointers, the implementation is free to do that and some must be doing that, the only thing is that you shouldn&#39;t trust they&#39;re always pointers: as said, it&#39;s implementation dependent, but once again, they could.</p><p>I didn&#39;t mean &quot;iterators are pointers&quot;, I meant &quot;pointers are iterators&quot;, random access iterators for being exact. &quot;Everything that acts like an iterator is an iterator&quot;.</p><div class="quote_container"><div class="title">the same guy said:</div><div class="quote"><p> What about the case of returning an array? </p></div></div><p>

In #6 you were talking about it as an argument, there it depends of the form you receive it. When it&#39;s returned, things depend of the way you manipulate the received data and also of the exact return type.</p><p>In that point, right, when it&#39;s returned it&#39;s not like using a normal C-array.</p><div class="quote_container"><div class="title">still the same guy said:</div><div class="quote"><p> Do you? Do std::vector&#39;s have copy symantics? Are you sure? Are you sure that some implementations aren&#39;t allowed to implicitly reference vectors? </p></div></div><p>

If the implementation did such a thing, in the next code:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;vector&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;iostream&gt;</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="k1">void</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_813.html" target="_blank">tell</a><span class="k2">(</span>vector<span class="k3">&lt;</span>int&gt; i<span class="k2">)</span></td></tr><tr><td class="number">7</td><td><span class="k2">{</span></td></tr><tr><td class="number">8</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k3">&amp;</span>i<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">' '</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td><span class="k2">}</span></td></tr><tr><td class="number">10</td><td>&#160;</td></tr><tr><td class="number">11</td><td><span class="k1">void</span> tell2<span class="k2">(</span>vector<span class="k3">&lt;</span>int&gt; <span class="k3">&amp;</span> i<span class="k2">)</span></td></tr><tr><td class="number">12</td><td><span class="k2">{</span></td></tr><tr><td class="number">13</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k3">&amp;</span>i<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span></td></tr><tr><td class="number">14</td><td><span class="k2">}</span></td></tr><tr><td class="number">15</td><td>&#160;</td></tr><tr><td class="number">16</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">17</td><td><span class="k2">{</span></td></tr><tr><td class="number">18</td><td>  vector<span class="k3">&lt;</span>int&gt; a<span class="k2">(</span><span class="n">10</span><span class="k2">)</span>, b<span class="k2">(</span><span class="n">20</span><span class="k2">)</span>, c<span class="k2">(</span><span class="n">30</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"a: "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k3">&amp;</span>a<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">' '</span><span class="k2">;</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_813.html" target="_blank">tell</a><span class="k2">(</span>a<span class="k2">)</span><span class="k2">;</span> tell2<span class="k2">(</span>a<span class="k2">)</span><span class="k2">;</span> <span class="c">//1</span></td></tr><tr><td class="number">20</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"b: "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k3">&amp;</span>b<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">' '</span><span class="k2">;</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_813.html" target="_blank">tell</a><span class="k2">(</span>b<span class="k2">)</span><span class="k2">;</span> tell2<span class="k2">(</span>b<span class="k2">)</span><span class="k2">;</span> <span class="c">//2</span></td></tr><tr><td class="number">21</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"c: "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k3">&amp;</span>c<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">' '</span><span class="k2">;</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_813.html" target="_blank">tell</a><span class="k2">(</span>c<span class="k2">)</span><span class="k2">;</span> tell2<span class="k2">(</span>c<span class="k2">)</span><span class="k2">;</span> <span class="c">//3</span></td></tr><tr><td class="number">22</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

if <b>tell</b> modified i (for changing values, removing, inserting...), it&#39;d be also changing the internal stuff of a or b or c (whichever of those be sent) that wouldn&#39;t make any kind of sense there for the way it was received. That&#39;d also mean the 3 columns (the one with cout, the one calling tell and the one calling tell2) for //1, //2 or //3 would show the same value, but in this case only the first and third column of each one would be the same, the second one is of a temporary object.</p><p>afaik, the only thing that <b>could</b> (it isn&#39;t forced by the standard) have <b>Reference Counting</b> is <i>string</i>, but it also has the property of &quot;copy on write&quot;, so the first modification would make it create its own &quot;independent&quot; object.</p><p>If it also applied to <i>vector</i>, it&#39;d need to comply with the &quot;copy on write&quot; model too.</p><p>Whenever it really happens or not, it&#39;d need to be well-behaved.</p><p>[edit] It seems like everything is going off-topic now, I stop here [/edit]
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sat, 23 Aug 2003 16:17:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What I do in my game is use temporary vectors, then copy all the information from them into a pointer that has been created with new [ vector.size() ]. Because vectors are bigger and when I want to network a game, I can&#39;t send a vector because (I believe, I could be wrong) that they have pointers, and sending the address of something is no good.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sat, 23 Aug 2003 21:57:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Synapse Jumps said:</div><div class="quote"><p> What I do in my game is use temporary vectors, then copy all the information from them into a pointer that has been created with new [ vector.size() ]. </p></div></div><p>

If std::vector really has the linearity &quot;property&quot;, it wouldn&#39;t be necessary to copy all the elements from std::vector to a temporary C-array before sending them thru the network, you could just take the address of the first element and use it as a pointer to element (or &quot;simulated&quot; dynamic array if you prefer to call it that way):</p><div class="source-code snippet"><div class="inner"><pre>std::vector<span class="k3">&lt;</span>int&gt; iv<span class="k2">;</span>
<span class="c">//...</span>
<span class="k1">int</span> <span class="k3">*</span> piv <span class="k3">=</span> <span class="k3">&amp;</span>iv<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">;</span>
</pre></div></div><p>

Even though, after Bob&#39;s post, it&#39;s now in doubt if it&#39;ll work for all the implementations of std::vector.</p><div class="quote_container"><div class="title">Synapse Jumps said:</div><div class="quote"><p> I can&#39;t send a vector because (I believe, I could be wrong) that they have pointers, and sending the address of something is no good. </p></div></div><p>

Yep, trying to send an std::vector directly wouldn&#39;t work.</p><p>......</p><p>Believe Bob, believe book, believe none.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sun, 24 Aug 2003 01:34:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Someone was telling me that the standard does say the elements are linear or something to that effect.</p><p>Oh wait it says that accessing any element is O(1) time.  That means there must be a direct mapping.  Hmm I don&#39;t have my copy of the standard with me right now.</p><p>Anyway, if we are talking about performance you have to talk about an implementation of STL.  And all the implementions I&#39;ve seen store elements linearly in memory.</p><p>And I will bet you that using an iterator and accessing an vector using [] will generate the identical assembly code that an array would, at least for MSVC.NET.  Adding elements will take more code than a C array if you don&#39;t allow resizing.  I would prove it were I not on vacation <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Sun, 24 Aug 2003 12:36:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Adding elements will take more code than a C array if you don&#39;t allow resizing.</p></div></div><p>

Don&#39;t you mean, &quot;no more code&quot;?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Korval)</author>
		<pubDate>Sun, 24 Aug 2003 13:28:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No.  More code is generated for adding elements to the std::vector because it has to check to see if its size is too much.</p><p>If you take an example of you want to make an array of 50 elements.  In the C method you allocate a new 50-element array and add 50 things to it.  In the C++ method you do the same in a std::vector.</p><p>If you are using actual C, the C way is faster and generates less code.  If you are using dynamic arrays in C++ with objects, then the array method may be SLOWER than the std::vector method because std::vector takes advantage of std::allocator and placement new to prevent redundant data copy.  In the std::vector case with primitive objects, it will be slower than an array because of size checks, but the std::vector was is easier and safer imho and and speed decrease is worth it, and all of the speed advantages just make it better.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Mon, 25 Aug 2003 01:44:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>In the std::vector case with primitive objects, it will be slower than an array because of size checks</p></div></div><p>

Why wouldn&#39;t you be doing size checks with a C-array? Obviously, the array itself doesn&#39;t check, but why wouldn&#39;t the object doing the adding make sure that the user doesn&#39;t add too many? At least, in a debug build.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Korval)</author>
		<pubDate>Mon, 25 Aug 2003 05:32:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I kindof awnsered my own question today. I tried to make a multidimentional array out of vectors and MSVC got mad at me, so I decided to just write a 2D array template class using new and delete and it works great and looks much cleaner then the iterator BS I surely would have had to write with the vector approach.</p><p>Anyone want to see the class? Mabey you can tell me how to better it a little? OK!! You convinced me. here it is you nags! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="c">/*** Creates and destroys a dynamic 2D array ***/</span></td></tr><tr><td class="number">2</td><td><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> QType&gt; <span class="k1">class</span> array2D</td></tr><tr><td class="number">3</td><td><span class="k2">{</span></td></tr><tr><td class="number">4</td><td>public:</td></tr><tr><td class="number">5</td><td>  QType <span class="k3">*</span><span class="k3">*</span>ptr<span class="k2">;</span></td></tr><tr><td class="number">6</td><td>  </td></tr><tr><td class="number">7</td><td>  <span class="k1">int</span> height<span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  <span class="k1">int</span> width<span class="k2">;</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td>  array2D<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span></td></tr><tr><td class="number">11</td><td>  ~array2D<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  array2D<span class="k2">(</span> <span class="k1">int</span> w, <span class="k1">int</span> h <span class="k2">)</span> <span class="k2">{</span> resize<span class="k2">(</span>w,h<span class="k2">)</span><span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">13</td><td>  <span class="k1">void</span> resize<span class="k2">(</span> <span class="k1">int</span> w, <span class="k1">int</span> h <span class="k2">)</span> <span class="k2">;</span></td></tr><tr><td class="number">14</td><td>  <span class="k1">inline</span> QType at<span class="k2">(</span> <span class="k1">int</span> x, <span class="k1">int</span> y <span class="k2">)</span> <span class="k2">;</span></td></tr><tr><td class="number">15</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td><span class="c">/*****************************************************/</span></td></tr><tr><td class="number">18</td><td><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> QType&gt; <span class="k1">void</span> array2D<span class="k3">&lt;</span>QType&gt;::resize<span class="k2">(</span> <span class="k1">int</span> w, <span class="k1">int</span> h <span class="k2">)</span> </td></tr><tr><td class="number">19</td><td><span class="k2">{</span></td></tr><tr><td class="number">20</td><td>  ptr<span class="k3">=</span> <span class="k1">new</span> QType <span class="k3">*</span><span class="k2">[</span>h<span class="k2">]</span><span class="k2">;</span>  </td></tr><tr><td class="number">21</td><td>  <span class="k1">for</span><span class="k2">(</span> <span class="k1">int</span> y<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> y<span class="k3">&lt;</span>h<span class="k2">;</span> y<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">22</td><td>    ptr<span class="k2">[</span>y<span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> QType <span class="k2">[</span>w<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">24</td><td>  height<span class="k3">=</span>h<span class="k2">;</span></td></tr><tr><td class="number">25</td><td>  width<span class="k3">=</span>w<span class="k2">;</span></td></tr><tr><td class="number">26</td><td><span class="k2">}</span></td></tr><tr><td class="number">27</td><td>&#160;</td></tr><tr><td class="number">28</td><td><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> QType&gt; array2D<span class="k3">&lt;</span>QType&gt;::~array2D<span class="k2">(</span><span class="k2">)</span> </td></tr><tr><td class="number">29</td><td><span class="k2">{</span></td></tr><tr><td class="number">30</td><td>  <span class="k1">for</span> <span class="k2">(</span> <span class="k1">int</span> y <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> y <span class="k3">&lt;</span> height<span class="k2">;</span> y<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>  <span class="k2">{</span></td></tr><tr><td class="number">31</td><td>    <span class="k1">delete</span> <span class="k2">[</span><span class="k2">]</span> ptr<span class="k2">[</span>y<span class="k2">]</span><span class="k2">;</span>  </td></tr><tr><td class="number">32</td><td>  <span class="k2">}</span>  </td></tr><tr><td class="number">33</td><td>  <span class="k1">delete</span> <span class="k2">[</span><span class="k2">]</span> ptr<span class="k2">;</span></td></tr><tr><td class="number">34</td><td><span class="k2">}</span></td></tr><tr><td class="number">35</td><td>&#160;</td></tr><tr><td class="number">36</td><td><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> QType&gt; <span class="k1">inline</span> QType array2D<span class="k3">&lt;</span>QType&gt;::at<span class="k2">(</span> <span class="k1">int</span> x, <span class="k1">int</span> y <span class="k2">)</span> </td></tr><tr><td class="number">37</td><td><span class="k2">{</span></td></tr><tr><td class="number">38</td><td>  <span class="k1">if</span> <span class="k2">(</span> <span class="k2">(</span>x<span class="k3">&lt;</span><span class="n">0</span><span class="k2">)</span> <span class="k3">|</span><span class="k3">|</span> <span class="k2">(</span>y<span class="k3">&lt;</span><span class="n">0</span><span class="k2">)</span> <span class="k3">|</span><span class="k3">|</span> <span class="k2">(</span>x&gt;<span class="k2">(</span>width-1<span class="k2">)</span><span class="k2">)</span> <span class="k3">|</span><span class="k3">|</span> <span class="k2">(</span>y&gt;<span class="k2">(</span>height-1<span class="k2">)</span><span class="k2">)</span> <span class="k2">)</span></td></tr><tr><td class="number">39</td><td>    <span class="c">// ERROR</span></td></tr><tr><td class="number">40</td><td>  <span class="k1">return</span> ptr<span class="k2">[</span>x<span class="k2">]</span><span class="k2">[</span>y<span class="k2">]</span></td></tr><tr><td class="number">41</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Amos)</author>
		<pubDate>Mon, 25 Aug 2003 07:16:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You shouldn&#39;t really get that a slow down for inserting, say those 50, elements in an <b>std::vector</b> if you&#39;re going to do it in a ~C way:</p><div class="source-code snippet"><div class="inner"><pre>std::vector<span class="k3">&lt;</span>int&gt; vi<span class="k2">(</span><span class="n">50</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> <span class="n">50</span><span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span>
<span class="k2">{</span>
  vi<span class="k3">&lt;</span>i&gt; <span class="k3">=</span> i <span class="k3">*</span> <span class="n">2</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

<b>operator []</b> won&#39;t check if the index is valid or not, so if you use an invalid index, it won&#39;t be <b>std::vector</b>&#39;s fault if the program crashes.</p><p>If you want range checking you need to use <b>at()</b>, that will return an exception of type <b>std::</b><b>out_of_range</b> in case you use an invalid index, method that would be slow compared to using <b>operator []</b>, but safer.</p><p>The other method that would cause a slow down, but at the same time would give you more safety, is using <b>push_back()</b>.</p><p>-----------------</p><p>I was checking one of my little precious books and found the &quot;answer&quot; to Bob&#39;s comment:</p><div class="quote_container"><div class="title">my little precious book said:</div><div class="quote"><p>
<b>Contiguity of Vectors</b><br />Built-in arrays in C++ reside in contiguous chunks of memory. The Standard, however, does not require that vector elements occupy contiguous memory. When STL was added to the Standard, it seemed intuitive that vectors should store their elements contiguously, so contiguity never became an explicit requirement. Indeed, all current STL implementations follow this convention. The current specification, however, permits implementations that do not use contiguous memory. This loophole will probably be fixed by the Standardization committee in the future, and vector contiguity will become a Standard requirement.
</p></div></div><p>

So you can make the assumption but you can&#39;t be 100% sure <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Mon, 25 Aug 2003 15:44:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Regarding the 2d array class:</p><p>You should use a virtual constructor, declare a copy ctor and an opertator=</p><p>Other than that, it looks ok at the first glance <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Mon, 25 Aug 2003 17:20:33 +0000</pubDate>
	</item>
</rss>
