<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>How to delete allocated memory at void pointer?</title>
		<link>http://www.allegro.cc/forums/view/555755</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 30 Dec 2005 05:31:40 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m writing my linked lists class.  Its nodes point to actual element that was allocated outside the class.  And i&#39;d like to be able to deallocate both nodes and elements from inside the class.</p><p>What i have to do with it?<br /><span class="source-code"><span class="k1">delete</span> node-&gt;data<span class="k2">;</span></span> or <span class="source-code"><span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span> <span class="k2">(</span>BYTE<span class="k3">*</span><span class="k2">)</span> node-&gt;data<span class="k2">;</span></span></p><p><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /> I got Dev-C warning when delete void* directly! by deleting node-&gt;data. So I&#39;m not sure I can do that.<br /><b>[Warning] deleting `void*&#39; is undefined</b></p><p>But casting to (BYTE*) and do delete[] stop compiler to complain anymore.</p><p>Does delete operator know the size of allocated memery point by void*?  Below is a small part of my 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="k1">class</span> Linked_Lists</td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td>    public:</td></tr><tr><td class="number">4</td><td>        Linked_Lists<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>        ~Linked_Lists<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>        <span class="k1">bool</span> add<span class="k2">(</span><span class="k1">void</span> <span class="k3">*</span>data<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>        <span class="k1">bool</span> remove_tail<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>        <span class="k1">bool</span> first<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>        <span class="k1">bool</span> last<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>        <span class="k1">bool</span> next<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>        <span class="k1">bool</span> prev<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>        <span class="k1">void</span><span class="k3">*</span> get_data<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>        <span class="k1">bool</span> is_empty<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>        <span class="k1">int</span> get_count<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>        </td></tr><tr><td class="number">16</td><td>    protected:</td></tr><tr><td class="number">17</td><td>        <span class="k1">struct</span> Node</td></tr><tr><td class="number">18</td><td>        <span class="k2">{</span></td></tr><tr><td class="number">19</td><td>            <span class="k1">void</span> <span class="k3">*</span>data<span class="k2">;</span></td></tr><tr><td class="number">20</td><td>            Node <span class="k3">*</span>prev_node<span class="k2">;</span></td></tr><tr><td class="number">21</td><td>            Node <span class="k3">*</span>next_node<span class="k2">;</span></td></tr><tr><td class="number">22</td><td>        <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>        Node <span class="k3">*</span>head<span class="k2">;</span></td></tr><tr><td class="number">24</td><td>        Node <span class="k3">*</span>tail<span class="k2">;</span></td></tr><tr><td class="number">25</td><td>        Node <span class="k3">*</span>current<span class="k2">;</span></td></tr><tr><td class="number">26</td><td>        <span class="k1">int</span> count<span class="k2">;</span></td></tr><tr><td class="number">27</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>&#160;</td></tr><tr><td class="number">29</td><td>Linked_Lists::~Linked_Lists<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">30</td><td><span class="k2">{</span></td></tr><tr><td class="number">31</td><td>    Node <span class="k3">*</span>node<span class="k2">;</span></td></tr><tr><td class="number">32</td><td>    </td></tr><tr><td class="number">33</td><td>    <span class="k1">if</span> <span class="k2">(</span>head <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span> <span class="k1">return</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td>    </td></tr><tr><td class="number">35</td><td>    first<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>  </td></tr><tr><td class="number">36</td><td>    <span class="k1">do</span></td></tr><tr><td class="number">37</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">38</td><td>        node <span class="k3">=</span> current<span class="k2">;</span></td></tr><tr><td class="number">39</td><td>        next<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">40</td><td>        <span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span> <span class="k2">(</span>BYTE<span class="k3">*</span><span class="k2">)</span> node-&gt;data<span class="k2">;</span></td></tr><tr><td class="number">41</td><td>        <span class="k1">delete</span> node<span class="k2">;</span>    </td></tr><tr><td class="number">42</td><td>    <span class="k2">}</span> <span class="k1">while</span> <span class="k2">(</span>current <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">43</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NonLegato)</author>
		<pubDate>Tue, 27 Dec 2005 14:11:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Does delete operator know the size of allocated memery point by void*?
</p></div></div><p>
I think yes.</p><p>But if you&#39;re writing this for anything other than academic purposes, I suggest you switch to STL.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Tue, 27 Dec 2005 14:26:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why to switch to STL? I know there are namespaces just for this, but if you write class on your own, you know how it works and you can easily modify it to whatever you want. For example I included linked list to object classes in my project. I cannot imagine what junk I&#39;d have in my code when using STL. But anyway I&#39;m not saying it&#39;s wrong. Whether to use it or not depends on circumstances. But one note - STL makes your programs a bit overweight.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OICW)</author>
		<pubDate>Tue, 27 Dec 2005 14:53:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Then why do you use Allegro? Why don&#39;t you write your own code for loading bitmaps, blitting them, playing samples, setting gfx mode, getting mouse and keyboard input, etc? If you did, you would know exactly how it worked.</p><p>Let me tell you why. Because someone already wrote that for you and it works. It has been tried and tested many times and there&#39;s no reason to reinvent the wheel.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Tue, 27 Dec 2005 14:58:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
But if you&#39;re writing this for anything other than academic purposes, I suggest you switch to STL.
</p></div></div><p>

I&#39;m using Dev-C++. I still remember that using STL with MinGW results in a very large exe-file. So I avoid it if I don&#39;t use much of it.</p><p>I&#39;ve ever used VC++ before and it doesn&#39;t have this STL issue. <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NonLegato)</author>
		<pubDate>Tue, 27 Dec 2005 15:02:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok Miran you&#39;re right, but I&#39;m talking about basic data structure. First it&#39;s good to know how it works, second it&#39;s better to write your own which will be suit you, third STL produces very large exe files. But again I&#39;m not saying STL s wrong. But when you need simple linked list, then I don&#39;t see any need to include STL.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OICW)</author>
		<pubDate>Tue, 27 Dec 2005 15:57:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I&#39;m using Dev-C++. I still remember that using STL with MinGW results in a very large exe-file. So I avoid it if I don&#39;t use much of it.
</p></div></div><p>
Proof? In my experience STL code is always highly efficient, except perhaps when you talk about speed of compilation. Maybe the exe gets so large because you use many different vector types, and forgot to strip symbols?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (amarillion)</author>
		<pubDate>Tue, 27 Dec 2005 16:46:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Proof? In my experience STL code is always highly efficient, except perhaps when you talk about speed of compilation. Maybe the exe gets so large because you use many different vector types, and forgot to strip symbols?
</p></div></div><p>

I have done some experiments on basic hello world code. It is an iostream part of STL that causes big exe files.</p><pre>
hello world                             19 KB
+ my owned linked list                  54 KB
+ std::iostream                        572 KB
+ std::list                             56 KB
+ std::vector                           61 KB
</pre><p>

Well, I still need your advice on deleting void pointer.  Dev C++ warning make me unsure that deleting void* will free allocated memory properly.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NonLegato)</author>
		<pubDate>Tue, 27 Dec 2005 18:17:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Going back to the original question:</p><p>Firstly, I don&#39;t think you can call delete on a void *.  Delete needs to know the size of the allocated structure - in fact (at least with MS, anyway), if you step with the debugger inside of new/delete, you&#39;ll see that internally it uses malloc/free.</p><p>I think your code though is highly perilous ground - the point of C++ is to avoid pointer casting &amp; void types and the inherent danger of the lack of type safety found in procedural C.  I think what you&#39;re probably looking for is this: try making your linked list class a template class.  In fact, this is exactly what STL does for its container classes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Wil Renczes)</author>
		<pubDate>Tue, 27 Dec 2005 18:19:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I have done some experiments on basic hello world code. It is an iostream part of STL that causes big exe files.
</p></div></div><p>Could you share the code, please? I did a simple hello world test myself and with simple std::iostream and std::list file size was ~25k, 5k when stripped, 4.5k when UPX&#39;ed. Perhaps my test was too simple though.</p><p>Also, this was done under Linux with GCC 3.4.4. In theory I think there shouldn&#39;t be major difference between OS&#39;es because the compiler is the same.</p><p>Though I noticed that with custom list you saved a whole two kB&#39;s <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />Is that enough to justify creating your own classes that you have trouble with? With STL list you wouldn&#39;t even have to make topics like this one.</p><p>As for deleting void*, I don&#39;t know. I haven&#39;t used them in my C++ projects because I haven&#39;t seen the need <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Tue, 27 Dec 2005 19:53:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Could you share the code, please? I did a simple hello world test myself and with simple std::iostream and std::list file size was ~25k, 5k when stripped, 4.5k when UPX&#39;ed. Perhaps my test was too simple though.
</p></div></div><p>

With iostream included, my hello world grows to 465KB when compiled without debugging info. (Dev C++ v4.9.9.2 and MinGW32 v3.4.2)</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;cstdlib&gt;</span>
<span class="p">#include &lt;iostream&gt;</span>

<span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span>

<span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span> <span class="k3">*</span>argv<span class="k2">[</span><span class="k2">]</span><span class="k2">)</span>
<span class="k2">{</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Hello World\n"</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I noticed that with custom list you saved a whole two kB&#39;s <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />Is that enough to justify creating your own classes that you have trouble with? With STL list you wouldn&#39;t even have to make topics like this one.
</p></div></div><p>

I just try to write my own class for fun. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> And if it turns to be simpler and smaller than STL I will use it in my game instead. Maybe iostream link size scared me out. <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NonLegato)</author>
		<pubDate>Tue, 27 Dec 2005 20:46:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><pre>
Bare main()                        -&gt; 6KB
std::list with all function called -&gt; 27 KB

Difference                         -&gt; 21 KB
</pre><p>
Delete should knows size (since in most cases it uses delete which takes void * anyway) but since type is unknown it doesn&#39;t know which destructor to call. Remember, object&#39;s destructor <u>must</u> be called when object is destroyed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Hrvoje Ban)</author>
		<pubDate>Tue, 27 Dec 2005 20:51:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why do you want a void*? Just use a char*, it works the same, as each char is 1 byte, and you can allocate + free it with no problems.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Tue, 27 Dec 2005 21:24:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>With that code you gave I got this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#g++ linklist.cpp ; ls -l a.out</span>
<span class="k3">-</span>rwxr-xr-x  <span class="n">1</span> hoho users <span class="n">8187</span> Dec <span class="n">27</span> <span class="n">14</span><span class="k2">:</span><span class="n">16</span> a.out

<span class="p">#cp a.out a1.out</span>
<span class="p">#upx -9 a1.out</span>
<span class="p">#ls -l a1.out</span>
<span class="k3">-</span>rwxr-xr-x  <span class="n">1</span> hoho users <span class="n">4830</span> Dec <span class="n">27</span> <span class="n">14</span><span class="k2">:</span><span class="n">17</span> a1.out

<span class="p">#cp a.out a2.out</span>
<span class="p">#strip a2.out</span>
<span class="p">#ls -l a2.out</span>
<span class="k3">-</span>rwxr-xr-x  <span class="n">1</span> hoho users <span class="n">4420</span> Dec <span class="n">27</span> <span class="n">14</span><span class="k2">:</span><span class="n">17</span> a2.out
</pre></div></div><p>
Perhaps mingw really does create bigger executeables under windows.</p><p>With a STL list test I got this result:
</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;list&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><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="k1">int</span> main <span class="k2">(</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td>  list <span class="k3">&lt;</span>int&gt; l<span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  l.size<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>  l.empty<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  l.push_back<span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>  l.push_front<span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  l.front<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>  l.back<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>  l.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  l.end<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  l.insert<span class="k2">(</span><span class="n">0</span>,<span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  l.clear<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  l.push_back<span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  l.push_front<span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td>  l.pop_front<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>  l.pop_back<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>  l.remove<span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>  l.sort<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>  l.reverse<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">25</td><td>  cout<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">"Done"</span><span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span></td></tr><tr><td class="number">26</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#g++ linklist.cpp; ls -l a.out</span>
<span class="k3">-</span>rwxr-xr-x  <span class="n">1</span> hoho users <span class="n">19057</span> Dec <span class="n">27</span> <span class="n">14</span><span class="k2">:</span><span class="n">24</span> a.out

<span class="p">#cp a.out a1.out ; upx -9 a1.out ; ls -l a1.out</span>
<span class="k3">-</span>rwxr-xr-x  <span class="n">1</span> hoho users <span class="n">8575</span> Dec <span class="n">27</span> <span class="n">14</span><span class="k2">:</span><span class="n">25</span> a1.out

<span class="p">#cp a.out a2.out ; strip a2.out ; ls -l a2.out</span>
<span class="k3">-</span>rwxr-xr-x  <span class="n">1</span> hoho users <span class="n">11060</span> Dec <span class="n">27</span> <span class="n">14</span><span class="k2">:</span><span class="n">25</span> a2.out

<span class="p">#upx -9 a2.out</span>
ls <span class="k3">-</span>l a2.out
<span class="k3">-</span>rwxr-xr-x  <span class="n">1</span> hoho users <span class="n">6084</span> Dec <span class="n">27</span> <span class="n">14</span><span class="k2">:</span><span class="n">25</span> a2.out
</pre></div></div><p>
Not too bad. I guess the size difference might come from if programs under Linux are dynamically linked to libstdc++. You might want to try what UPX and strip can do for the executeables under Windows. I wouldn&#39;t be surprised if the 465kB gets reduced to &lt;50kiB</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I just try to write my own class for fun.
</p></div></div><p>There is nothing bad about writing your own classes but you might want to write them similar to STL by using templates so you wouldn&#39;t have such problems with void pointers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Tue, 27 Dec 2005 21:34:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why are you trying to delete a void*? You can&#39;t new a void*.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Wed, 28 Dec 2005 08:34:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Why are you trying to delete a void*? You can&#39;t new a void*.
</p></div></div><p>
This indicates a design error. You should be deleting the object in the same context as you&#39;re creating it. Otherwise memory leaks will be the least of your problems.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Steve++)</author>
		<pubDate>Wed, 28 Dec 2005 09:01:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Firstly, I don&#39;t think you can call delete on a void *. Delete needs to know the size of the allocated structure
</p></div></div><p>
No, it does not need to know the size. The OS does, or the underlying allocation system, but delete does not.<br />The problem here is that delete wants to call a deconstructor for the data, and void does not have a deconstructor.</p><p>What is this data? How do you allocate it? What is it used for?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Billybob)</author>
		<pubDate>Wed, 28 Dec 2005 10:56:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Quote:<br />Firstly, I don&#39;t think you can call delete on a void *. Delete needs to know the size of the allocated structure
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
No, it does not need to know the size. The OS does, or the underlying allocation system, but delete does not.<br />The problem here is that delete wants to call a deconstructor for the data, and void does not have a deconstructor.
</p></div></div><p>

Sorry - poor wording on my part.  I think we&#39;re getting at the same thing - when you delete an object, internally, delete derives the size of the allocation that&#39;s being freed since it knows the struct size of the class that&#39;s being deleted.  But it still calls free() with a given byte size.  And as William points out, void is not a type, so you can&#39;t call its destructor.</p><p>Which brings me back to the problem: one shouldn&#39;t use a void pointer type in the first place.  It&#39;s a skanky way to short-circuit a problem, and fraught with potential gotchas.  Seriously:  look at template classes.  This is exactly what they&#39;re for:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>&#160;</td></tr><tr><td class="number">2</td><td><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> T&gt;</td></tr><tr><td class="number">3</td><td><span class="k1">class</span> MyNode</td></tr><tr><td class="number">4</td><td><span class="k2">{</span></td></tr><tr><td class="number">5</td><td>public:</td></tr><tr><td class="number">6</td><td>    T<span class="k3">*</span> data<span class="k2">;</span></td></tr><tr><td class="number">7</td><td>    MyNode<span class="k3">&lt;</span>T&gt;<span class="k3">*</span> prev_node<span class="k2">;</span></td></tr><tr><td class="number">8</td><td>    MyNode<span class="k3">&lt;</span>T&gt;<span class="k3">*</span> next_node<span class="k2">;</span></td></tr><tr><td class="number">9</td><td><span class="k2">}</span><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">template</span> <span class="k3">&lt;</span><span class="k1">class</span> T&gt;</td></tr><tr><td class="number">12</td><td><span class="k1">class</span> MyLinkedList</td></tr><tr><td class="number">13</td><td><span class="k2">{</span></td></tr><tr><td class="number">14</td><td>public:</td></tr><tr><td class="number">15</td><td>&#160;</td></tr><tr><td class="number">16</td><td><span class="c">//skipping over the bulk of it</span></td></tr><tr><td class="number">17</td><td>&#160;</td></tr><tr><td class="number">18</td><td>~MyLinkedList_Lists<span class="k2">(</span><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>    MyNode<span class="k3">&lt;</span>T&gt; <span class="k3">*</span>node<span class="k2">;</span></td></tr><tr><td class="number">21</td><td>    </td></tr><tr><td class="number">22</td><td>    <span class="k1">if</span> <span class="k2">(</span>head <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span> <span class="k1">return</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>    </td></tr><tr><td class="number">24</td><td>    first<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>  </td></tr><tr><td class="number">25</td><td>    <span class="k1">do</span></td></tr><tr><td class="number">26</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">27</td><td>        node <span class="k3">=</span> current<span class="k2">;</span></td></tr><tr><td class="number">28</td><td>        next<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>        <span class="k1">delete</span> node<span class="k2">;</span>    </td></tr><tr><td class="number">30</td><td>    <span class="k2">}</span> <span class="k1">while</span> <span class="k2">(</span>current <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td>&#160;</td></tr><tr><td class="number">33</td><td>private:</td></tr><tr><td class="number">34</td><td>    MyNode<span class="k3">&lt;</span>T&gt;<span class="k3">*</span> head<span class="k2">;</span></td></tr><tr><td class="number">35</td><td>    MyNode<span class="k3">&lt;</span>T&gt;<span class="k3">*</span> tail<span class="k2">;</span></td></tr><tr><td class="number">36</td><td>    MyNode<span class="k3">&lt;</span>T&gt;<span class="k3">*</span> current<span class="k2">;</span></td></tr><tr><td class="number">37</td><td>    <span class="k1">int</span> count<span class="k2">;</span></td></tr><tr><td class="number">38</td><td>&#160;</td></tr><tr><td class="number">39</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

Apologies in advance for the sparse example - I haven&#39;t run the compiler on this, so my syntax might be off somewhere, but you get the idea.  The point is that you simply call delete on the node object now, since the compiler knows what T is when you instantiate the class - you&#39;re solid.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Wil Renczes)</author>
		<pubDate>Wed, 28 Dec 2005 12:19:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks everybody. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>Now I understand the concept of type safety and template. I think I&#39;m going to read my old C++ book about data structure and algorithm again. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NonLegato)</author>
		<pubDate>Wed, 28 Dec 2005 14:02:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There are some good uses to void*.  In my linked list class, I use void* to whatever data is to be stored in the list. You just have to <i>cast</i> it whenever you want to read/use the data.</p><p>However, deleting a void* is a no-no in my books.  I like to think of the void* as if pointing to something with my finger.  I wouldn&#39;t want to delete my finger (ouch), I want to delete the <i>something</i> that it is pointing at.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Thu, 29 Dec 2005 04:54:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I never used void*  since it didn&#39;t sound like something I would need, which also means I don&#39;t have any experience with it, but my programmer-sense says; if you create something whose size you will need later, then calc&amp;store its size while you are creating it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Murat AYIK)</author>
		<pubDate>Thu, 29 Dec 2005 05:33:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Firstly, I don&#39;t think you can call delete on a void *. Delete needs to know the size of the allocated structure
</p></div></div><p>
Typically, what happens is that C++ stores the exact size of the allocated memory block one word &quot;before&quot; the allocated memory block. For example:</p><div class="source-code snippet"><div class="inner"><pre>complex<span class="k3">*</span> c <span class="k3">=</span> <span class="k1">new</span> complex<span class="k2">(</span><span class="n">1</span>.<span class="n">0</span>, <span class="k3">-</span><span class="n">1</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// say complex is a struct with two 4-byte floats</span>
<span class="c">// address  0xF0    0xF1    0xF2</span>
<span class="c">// value    2       1.0f    -1.0f</span>
<span class="c">// c == 0xF1</span>
<span class="c">// size of allocated memory block (2 words) stored at 0xF0</span>

<span class="k1">char</span><span class="k3">*</span> str <span class="k3">=</span> <span class="k1">new</span> <span class="k1">char</span><span class="k2">[</span><span class="n">16</span><span class="k2">]</span><span class="k2">;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_756.html" target="_blank">strcpy</a><span class="k2">(</span>str, <span class="s">"Hello world!!!"</span><span class="k2">)</span><span class="k2">;</span>
<span class="c">// address  0xF3    0xF4    0xF5    0xF6    0xF7</span>
<span class="c">// value    4       Hell    o wo    rld!    !!\0[?]</span>
<span class="c">// str == 0xF4</span>
<span class="c">// size of allocated memory block (4 words) stored at 0xF3</span>
</pre></div></div><p>

Then when delete is called, it looks one memory space to the &quot;left&quot; of the allocated block to know how much to delete. This is how delete knows how much memory to free when given an array, even though you don&#39;t have to write <span class="source-code"><span class="k1">delete</span><span class="k2">[</span><span class="n">16</span><span class="k2">]</span> str<span class="k2">;</span></span>for example.</p><p>Thus, requiring the type pointed to by a void* pointer is a matter of type safety re: which destructor to call, and has nothing to do with how much memory is actually being deleted.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goodbytes)</author>
		<pubDate>Thu, 29 Dec 2005 06:00:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
There are some good uses to void*. In my linked list class, I use void* to whatever data is to be stored in the list. You just have to cast it whenever you want to read/use the data.
</p></div></div><p>
That&#39;s not a good use for void*. That&#39;s where you should use templates to make the code type safe.</p><p>And by the way, I bet no one can outperform STL in effiency, type safety and elegancy so why to invent the wheel over and over?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Thu, 29 Dec 2005 17:02:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That&#39;s not a good use for void*. That&#39;s where you should use templates to make the code type safe.
</p></div></div><p>

It&#39;s worked for me so far.  Maybe I&#39;m thinking of template in more of a general definition and you have a more technical explanation.  Care to share an example of what you mean?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And by the way, I bet no one can outperform STL in effiency, type safety and elegancy so why to invent the wheel over and over?
</p></div></div><p>

I don&#39;t doubt it.  A lot of people tell me I&#39;m &quot;reinventing the wheel,&quot; but of course I am.  I know there&#39;s a lot of things out there (like map editors, graphics libraries, etc.).  It&#39;s not that I&#39;m trying to make a better one or &quot;reinvent&quot; it, but rather understand it and learn from it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Fri, 30 Dec 2005 00:14:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It&#39;s worked for me so far. Maybe I&#39;m thinking of template in more of a general definition and you have a more technical explanation. Care to share an example of what you mean?
</p></div></div><p>
You&#39;re assuming that the linked list contains only objects of a specific type. But it doesn&#39;t have to (the compiler doesn&#39;t force type safety) and if one of the elements isn&#39;t of a desired type, you&#39;ll likely get a crash (or undefined behavior at best).</p><p>Also what if you want to store ints, for example? You&#39;d need one pointer dereference every time a value is accessed while that&#39;s not the case if you&#39;d use a template.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Fri, 30 Dec 2005 03:46:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You&#39;re assuming that the linked list contains only objects of a specific type.
</p></div></div><p>

What object can void* not point to?  </p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Also what if you want to store ints, for example? You&#39;d need one pointer dereference every time a value is accessed while that&#39;s not the case if you&#39;d use a template.
</p></div></div><p>

And what&#39;s so bad about dereferencing?  Also, void* and the next* is not all I store inside the linked list.  I also store an int (ikey) for sorting purposes (or if you are simply storing ints) as well as a char* (ckey) if you need a char for some reason.  Of course, this makes each node larger in size, but I don&#39;t think it&#39;s enough to make anything suffer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Fri, 30 Dec 2005 04:05:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That&#39;s not a good use for void*
</p></div></div><p>
It is.<br />That is to say, in C. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> In C++, one should (almost?) never need to use void pointers. If there&#39;s a situation where they seem nescessary it&#39;s probably a sign that you need to rethink your design.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Fri, 30 Dec 2005 04:06:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
What object can void* not point to?
</p></div></div><p>
That&#39;s my point indeed. When you need to access the data you need to know the real type of the objects. But you can&#39;t know it for sure because the type information is lost.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And what&#39;s so bad about dereferencing?
</p></div></div><p>
It&#39;s slower than a direct access.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I also store an int (ikey) for sorting purposes (or if you are simply storing ints) as well as a char* (ckey) if you need a char for some reason. Of course, this makes each node larger in size, but I don&#39;t think it&#39;s enough to make anything suffer.
</p></div></div><p>
Or you could store an object which contains all that data and a pointer to the real object or to store objects which inherit from the same virtual base class.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It is.<br />That is to say, in C.
</p></div></div><p>
But the question was (indirectly) if there&#39;s any good use for void pointers in C++.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Fri, 30 Dec 2005 04:08:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It is.<br />That is to say, in C.  In C++, one should (almost?) never need to use void pointers. If there&#39;s a situation where they seem nescessary it&#39;s probably a sign that you need to rethink your design.
</p></div></div><p>

I wrote the linked list structure back in my C-hayday of early college years.  When I gradually moved into C++, I just kept using it because it was easy to use.  I guess I should rethink how to do it then...hmmmm...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Fri, 30 Dec 2005 04:08:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And what&#39;s so bad about dereferencing?
</p></div></div><p>Compared to not dereferencing it is dead slow and takes more memory.</p><p>When extreme performance is needed then there is a difference. On the other hand, if extreme performance is needed it is usually best to stay away from lists <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>[edit]
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I guess I should rethink how to do it then...hmmmm...
</p></div></div><p>As I assume you already know how to code a linked list you might want to swich over to STL list. With that you automatically get all sorts of nifty things from &lt;algorithm&gt; header too. A slight speed improvement is also quite probable <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 30 Dec 2005 04:10:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That&#39;s my point indeed.
</p></div></div><p>
I see.  Well, I just got use to my class because it worked so well and I didn&#39;t really know I was losing that much performance.  Of course, I never had a very big list so it&#39;s not like I&#39;d notice a performance flaw anyway.  My original concept of making the linked list structure was &quot;store something in a generic list&quot; (some_list-&gt;data = something) and &quot;read information from the list&quot; (dereference).  Out of the few years I&#39;ve been using it, I&#39;ve never accidentally casted it to the wrong type when I dereferenced it.  Of course, this is all old news now and I&#39;m going to research STL and I guess, put my list to rest.  <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
As I assume you already know how to code a linked list you might want to swich over to STL list.
</p></div></div><p>

Done and done.  I&#39;m gonna tinker around with this when I get a chance.  Is this a good <a href="http://www.msoe.edu/eecs/ce/courseinfo/stl/list.htm">site</a> to start from?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Fri, 30 Dec 2005 04:43:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Is this a good site [msoe.edu] to start from?
</p></div></div><p>Seems to be. Though I usually just use whatever is first result from Google <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 30 Dec 2005 04:51:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Though I usually just use whatever is first result from Google
</p></div></div><p>
It was.  <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Fri, 30 Dec 2005 04:52:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>googling for &quot;c++ iostream&quot; or &quot;stl vector&quot; come up with totally different sites. I usually just google for specific stuff and use several different sites for reference
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 30 Dec 2005 05:30:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>try &quot;stl list&quot;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Fri, 30 Dec 2005 05:31:40 +0000</pubDate>
	</item>
</rss>
