<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>fread/fwrite problems!</title>
		<link>http://www.allegro.cc/forums/view/525662</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 07 Sep 2005 23:26:38 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey guys, I&#39;m having some really odd problems, reading and writing using pack_fread, and pack_fwrite, the data just isn&#39;t coming back the same!</p><p>Parts of the code with the problems
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>vars:</td></tr><tr><td class="number">2</td><td>ACTOR <span class="k3">*</span> player<span class="k2">;</span></td></tr><tr><td class="number">3</td><td>ACTOR <span class="k3">*</span> npc<span class="k2">[</span><span class="n">100</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>OBJECT <span class="k3">*</span> obj<span class="k2">[</span><span class="n">100</span><span class="k2">]</span><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">int</span> map<span class="k2">[</span><span class="n">256</span><span class="k2">]</span><span class="k2">[</span><span class="n">256</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td><span class="k1">int</span> map_archive<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">[</span><span class="n">256</span><span class="k2">]</span><span class="k2">[</span><span class="n">256</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>OBJECT <span class="k3">*</span> map_obj_arc<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">[</span><span class="n">100</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>ACTOR <span class="k3">*</span> map_npc_arc<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">[</span><span class="n">100</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td><span class="k1">int</span> map_visited<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td><span class="k1">int</span> dungeon_level<span class="k2">;</span></td></tr><tr><td class="number">13</td><td><span class="k1">int</span> deepest_dungeon_level<span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td>&#160;</td></tr><tr><td class="number">16</td><td>in save:</td></tr><tr><td class="number">17</td><td>  <a href="http://www.allegro.cc/manual/PACKFILE" target="_blank"><span class="a">PACKFILE</span></a> <span class="k3">*</span> fp <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_fopen" target="_blank"><span class="a">pack_fopen</span></a><span class="k2">(</span>filename, F_WRITE_PACKED<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  <span class="k1">if</span> <span class="k2">(</span>fp<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>    <span class="k1">int</span> version <span class="k3">=</span> <span class="n">1000</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>    </td></tr><tr><td class="number">22</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span><span class="k3">&amp;</span>version, <span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>player, <span class="k1">sizeof</span><span class="k2">(</span>ACTOR<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span><span class="k3">&amp;</span>dungeon_level, <span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">25</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span><span class="k3">&amp;</span>deepest_dungeon_level, <span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>    </td></tr><tr><td class="number">27</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>map, <span class="n">256</span><span class="k3">*</span><span class="n">256</span><span class="k3">*</span><span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>npc, <span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>ACTOR<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>obj, <span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>OBJECT<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>    </td></tr><tr><td class="number">31</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>map_visited, <span class="n">20</span><span class="k3">*</span><span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>map_archive, <span class="n">256</span><span class="k3">*</span><span class="n">256</span><span class="k3">*</span><span class="n">4</span><span class="k3">*</span><span class="n">20</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>map_obj_arc, <span class="n">20</span><span class="k3">*</span><span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>OBJECT<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td>    <a href="http://www.allegro.cc/manual/pack_fwrite" target="_blank"><span class="a">pack_fwrite</span></a><span class="k2">(</span>map_npc_arc, <span class="n">20</span><span class="k3">*</span><span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>ACTOR<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td>        </td></tr><tr><td class="number">36</td><td>    <a href="http://www.allegro.cc/manual/pack_fclose" target="_blank"><span class="a">pack_fclose</span></a><span class="k2">(</span>fp<span class="k2">)</span><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>&#160;</td></tr><tr><td class="number">39</td><td>in load:</td></tr><tr><td class="number">40</td><td><a href="http://www.allegro.cc/manual/PACKFILE" target="_blank"><span class="a">PACKFILE</span></a> <span class="k3">*</span> fp <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_fopen" target="_blank"><span class="a">pack_fopen</span></a><span class="k2">(</span>filename, F_READ_PACKED<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">41</td><td>  <span class="k1">if</span> <span class="k2">(</span>fp<span class="k2">)</span></td></tr><tr><td class="number">42</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">43</td><td>    <span class="k1">int</span> version<span class="k2">;</span></td></tr><tr><td class="number">44</td><td>    </td></tr><tr><td class="number">45</td><td>    <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span><span class="k3">&amp;</span>version, <span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">46</td><td>    <span class="k1">if</span> <span class="k2">(</span>version <span class="k3">=</span><span class="k3">=</span> <span class="n">1000</span><span class="k2">)</span></td></tr><tr><td class="number">47</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">48</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>player, <span class="k1">sizeof</span><span class="k2">(</span>ACTOR<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">49</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span><span class="k3">&amp;</span>dungeon_level, <span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">50</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span><span class="k3">&amp;</span>deepest_dungeon_level, <span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">51</td><td>&#160;</td></tr><tr><td class="number">52</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>map, <span class="n">256</span><span class="k3">*</span><span class="n">256</span><span class="k3">*</span><span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">53</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>npc, <span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>ACTOR<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">54</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>obj, <span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>OBJECT<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">55</td><td>&#160;</td></tr><tr><td class="number">56</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>map_visited, <span class="n">20</span><span class="k3">*</span><span class="n">4</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">57</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>map_archive, <span class="n">256</span><span class="k3">*</span><span class="n">256</span><span class="k3">*</span><span class="n">4</span><span class="k3">*</span><span class="n">20</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">58</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>map_obj_arc, <span class="n">20</span><span class="k3">*</span><span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>OBJECT<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">59</td><td>      <a href="http://www.allegro.cc/manual/pack_fread" target="_blank"><span class="a">pack_fread</span></a><span class="k2">(</span>map_npc_arc, <span class="n">20</span><span class="k3">*</span><span class="n">100</span><span class="k3">*</span><span class="k1">sizeof</span><span class="k2">(</span>ACTOR<span class="k2">)</span>, fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">60</td><td>&#160;</td></tr><tr><td class="number">61</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">62</td><td>      </td></tr><tr><td class="number">63</td><td>    <a href="http://www.allegro.cc/manual/pack_fclose" target="_blank"><span class="a">pack_fclose</span></a><span class="k2">(</span>fp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">64</td><td>  <span class="k2">}</span></td></tr></tbody></table></div></div><p>

what&#39;s wrong here????</p><p>edit: I think i&#39;ve got it.</p><p>some of the thing&#39;s like <span class="source-code">OBJECT <span class="k3">*</span> obj<span class="k2">[</span><span class="n">100</span><span class="k2">]</span><span class="k2">;</span></span> Contain null elements, so they probably break it when it needs to save/load.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Sun, 04 Sep 2005 12:32:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>obj an array of pointers to objects. So when you write obj to disk in the way you&#39;re doing it, you&#39;re writing the pointer values+some random bits of junk. Reading back from disk reads in some now-dead pointers and some random junk.</p><p>Oh, at the risk of being redundant, fread()/fwrite() are evil and shouldn&#39;t be touched with a three-metre pole. Your programme and data will at the least be architecture dependent, almost certainly be operating system dependent, probably compiler (and compiler version) dependent and maybe even optimisation switch dependent.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sun, 04 Sep 2005 14:14:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Oh, at the risk of being redundant, fread()/fwrite() are evil and shouldn&#39;t be touched with a three-metre pole. Your programme and data will at the least be architecture dependent, almost certainly be operating system dependent, probably compiler (and compiler version) dependent and maybe even optimisation switch dependent.
</p></div></div><p>What do you suggest should be used instead? Suppose I have this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> THING
<span class="k2">{</span>
  <span class="k1">int</span> a<span class="k2">;</span>
  <span class="k1">char</span> b<span class="k2">;</span>
  <span class="k1">double</span> c<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p> and I want to save one of these struct to a file in a way that is completely indepentent of all those things. I feel that this could actually be quite problematic, because sizeof(int) and sizeof(double) might change, and there is the big/little endian thing to worry about as well.<br />I&#39;ve only recently started to consider these problems, so if you&#39;ve got a quick answer then I&#39;d like to hear it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Sun, 04 Sep 2005 14:45:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>To save a float/double:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/pack_iputl" target="_blank"><span class="a">pack_iputl</span></a><span class="k2">(</span>file, <a href="http://www.delorie.com/djgpp/doc/libc/libc_330.html" target="_blank">floor</a><span class="k2">(</span>fval<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">if</span><span class="k2">(</span>fval-floor<span class="k2">(</span>fval<span class="k2">)</span><span class="k2">)</span>
   <a href="http://www.allegro.cc/manual/pack_iputl" target="_blank"><span class="a">pack_iputl</span></a><span class="k2">(</span>file, <span class="n">1</span><span class="k3">/</span><span class="k2">(</span>fval-floor<span class="k2">(</span>fval<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">else</span>
   <a href="http://www.allegro.cc/manual/pack_iputl" target="_blank"><span class="a">pack_iputl</span></a><span class="k2">(</span>file, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
And to read:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">long</span> whole <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_igetl" target="_blank"><span class="a">pack_igetl</span></a><span class="k2">(</span>file<span class="k2">)</span><span class="k2">;</span>
<span class="k1">long</span> recip <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_igetl" target="_blank"><span class="a">pack_igetl</span></a><span class="k2">(</span>file<span class="k2">)</span><span class="k2">;</span>
<span class="k1">float</span> fval <span class="k3">=</span> whole <span class="k3">+</span> <span class="k2">(</span>recip ? <span class="k2">(</span><span class="n">1</span>.<span class="n">0</span><span class="k3">/</span><span class="k2">(</span><span class="k1">float</span><span class="k2">)</span>recip<span class="k2">)</span> <span class="k2">:</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Should work. To save an int, use pack_igetl/pack_iputl. For char, pack_getc/pack_putc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sun, 04 Sep 2005 14:58:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, thanks.<br />It seems to me that &#39;pack_iputf&#39; and &#39;pack_igetf&#39; would be useful functions to add to Allegro.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Sun, 04 Sep 2005 16:20:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It seems to me that &#39;pack_iputf&#39; and &#39;pack_igetf&#39; would be useful functions to add to Allegro.
</p></div></div><p>
They are Allegro <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sun, 04 Sep 2005 16:24:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I meant like this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> pack_igetf<span class="k2">(</span><a href="http://www.allegro.cc/manual/PACKFILE" target="_blank"><span class="a">PACKFILE</span></a> <span class="k3">*</span>file<span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">long</span> whole <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_igetl" target="_blank"><span class="a">pack_igetl</span></a><span class="k2">(</span>file<span class="k2">)</span><span class="k2">;</span>
  <span class="k1">long</span> recip <span class="k3">=</span> <a href="http://www.allegro.cc/manual/pack_igetl" target="_blank"><span class="a">pack_igetl</span></a><span class="k2">(</span>file<span class="k2">)</span><span class="k2">;</span>
  <span class="k1">return</span> whole <span class="k3">+</span> <span class="k2">(</span>recip ? <span class="k2">(</span><span class="n">1</span>.<span class="n">0</span><span class="k3">/</span><span class="k2">(</span><span class="k1">float</span><span class="k2">)</span>recip<span class="k2">)</span> <span class="k2">:</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

If such a thing is already in Allegro, why didn&#39;t you just say &quot;use pack_igetf&quot;?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Sun, 04 Sep 2005 16:28:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ohh... pack_putf/pack_getf.. I thought those were l&#39;s, not f&#39;s. <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" /> Sorry.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sun, 04 Sep 2005 16:35:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Right, what a pain in the butt, and as soon as I eradicate one, I find another bug, and another bug, I think I&#39;m going to rewrite it the way you suggest...</p><p>Since doubles/floats are defined specifically by IEEE, why arn&#39;t they platform/etc dependant?</p><p>can&#39;t you pack_fwrite(&amp;my_double, 8, fp), or pack_fwrite(&amp;my_float, 4, fp) ?</p><p>In another thread someone said, floats/doubles are even in the same endian on macs as they are on pcs because of the way IEEE has defined them.  Is that wrong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Mon, 05 Sep 2005 01:23:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Wrong or not, you shouldn&#39;t rely on it, lest you compromise platform-independence.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Sporus)</author>
		<pubDate>Mon, 05 Sep 2005 01:27:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
can&#39;t you pack_fwrite(&amp;my_double, 8, fp), or pack_fwrite(&amp;my_float, 4, fp) ?
</p></div></div><p>

use: sizeof(double)</p><p>pack_fwrite(&amp;my_double, sizeof(double), fp)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (marcin)</author>
		<pubDate>Mon, 05 Sep 2005 02:47:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I thought the sizes were defined by the IEEE also! <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>Anyway, I wrote out a couple of functions for writing object *, and actor * s to a packfile, and just looped through calling those functions, it worked out ok, and the compiler warned me anywhere where I was accidently sending a ptr to a ptr instead of just a ptr, etc...</p><p>All is well <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>The only functions I had to use were pack_iputl, pack_igetl, pack_putc, and pack_getc, which is rather nice. <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" /></p><p>edit: NEW PROBLEM.</p><p>this is weird, opening an existing file as F_WRITE_PACKED, doesn&#39;t seem to let me overwrite the file... how come?</p><p>edit: NEVERMIND, the problem was the filename wasn&#39;t the same! duh! sorry! <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Mon, 05 Sep 2005 04:08:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>IEEE754 does not define endianness as interpreted in RAM.  It only specifies the floating point format and how operations are carried out.  It doesn&#39;t matter how the host implementation orders and stores the bits, but it does matter how that implementation is presented (interface) to the user.</p><p>IEEE754 specifies also single precision (32-bit) and double precision (64-bit).  These sizes are defined in the standard.  But the IEEE754 standard is not the C++ standard.  It doesn&#39;t mean that float maps to single precision IEEE754 and double to double precision.</p><p>Now, for practical purposes I think it is safe to assume that floats and doubles are IEEE754 format.  I haven&#39;t seen a machine use a different format.  Even if it is true, I don&#39;t think it is a problem if you actually define your file format properly.  People say &quot;oh, fwrite is not portable because of endianess etc&quot;, but that is only half-right.  If you specify in your file format that data is saved in IEEE754 little-endian format, then your code with fwrite will work properly on Intel platforms.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Mon, 05 Sep 2005 04:52:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sure, fwriting a block of integers should be fine (modulo endianesse), but you are likely to run into problems when fwriting a struct.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Mon, 05 Sep 2005 10:42:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Right cause structs have padding, possible re-ordering, etc, I was only using structs to get it working...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Mon, 05 Sep 2005 13:09:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
possible re-ordering
</p></div></div><p>
Structs don&#39;t reorder. But they can (and do) pad.. sometimes differently depending on optimization switches.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Mon, 05 Sep 2005 13:18:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Maybe simply write functions (one for each struct) for writing data:</p><p>SaveStructA(FILE *file, char *fileName, char *writingMode, struct structA *structA1)</p><p>and use</p><p>fprintf(...)</p><p>for each member:</p><p>structA1-&gt;member1</p><p>...</p><p>edit1:</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Structs don&#39;t reorder. But they can (and do) pad.. sometimes differently depending on optimization switches.
</p></div></div><p>

You can compile module that contains writing of structs without optimization and next link them with optimized code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (marcin)</author>
		<pubDate>Mon, 05 Sep 2005 13:53:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You can compile module that contains writing of structs without optimization and next link them with optimized code.
</p></div></div><p>
What if you use -Os? Still, it can change between major compiler versions, or between compilers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Mon, 05 Sep 2005 14:24:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
What if you use -Os? Still, it can change between major compiler versions, or between compilers.
</p></div></div><p>

If we are talking about gcc:</p><p>-Os means optimize for space rather than speed</p><p>-O0 means disable ALL optimizations, this is default switch
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (marcin)</author>
		<pubDate>Mon, 05 Sep 2005 16:36:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
-Os means optimize for space rather than speed
</p></div></div><p>
Exactly, so it&#39;d stand to reason it could pack structs on capable systems to reduce the amount of space.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 06 Sep 2005 04:11:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Structs don&#39;t reorder.
</p></div></div><p> News to me, I thought I heard someone say that they did -- must have been a different language.  I dunno.  Anyway you always seem fairly knowledgable KC, so I&#39;ll take your word for it.  </p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
SaveStructA(FILE *file, char *fileName, char *writingMode, struct structA *structA1)</p><p>and use</p><p>fprintf(...)</p><p>for each member:</p><p>structA1-&gt;member1
</p></div></div><p>
Essentially, that&#39;s what I did.  Except I used allegros packing routines instead of the standard library&#39;s.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Tue, 06 Sep 2005 05:05:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Some compiler may reorder
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> <span class="k2">{</span>
  <span class="k1">short</span> a<span class="k2">;</span>
  <span class="k1">long</span> b<span class="k2">;</span>
  <span class="k1">short</span> c<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
to
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> <span class="k2">{</span>
  <span class="k1">short</span> a<span class="k2">;</span>
  <span class="k1">short</span> c<span class="k2">;</span>
  <span class="k1">long</span> b<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
under certain circumstance / with certain options given. There should always be a way to prevent them from doing this, though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 07 Sep 2005 13:30:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That was my understanding also.  Is this true?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Wed, 07 Sep 2005 13:34:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That was my understanding also. Is this true?
</p></div></div><p>
Not as far as I&#39;m aware. AFAIK, and in my experiences, the compiler <b>cannot</b> reorder struct members. Maybe some compilers have an option/extension too, but it shouldn&#39;t be the default behavior.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Wed, 07 Sep 2005 15:03:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>As far as I know, the C standard guarentees the order if struct members. This is so that it is safe to use
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> A <span class="k2">{</span>
<span class="k1">int</span> a<span class="k2">;</span> <span class="k1">int</span> b<span class="k2">;</span> <span class="k1">char</span> c<span class="k2">;</span> <span class="k1">int</span> d<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
and
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> B <span class="k2">{</span>
<span class="k1">int</span> alpha<span class="k2">;</span> <span class="k1">int</span> beta<span class="k2">;</span> <span class="k1">char</span> gamma<span class="k2">;</span> <span class="k1">int</span> delta<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
interchangably. They need not even be declared in the same sourcefile. In fact, it&#39;s safe to cast a pointer to
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> C <span class="k2">{</span>
<span class="k1">int</span> i1<span class="k2">;</span> <span class="k1">int</span> i2<span class="k2">;</span> <span class="k1">char</span> c1<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
to either of the other two types and use that instead.<br />If the compiler is allowed to arbitrarily reorder structure members, that would be impossible.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 07 Sep 2005 15:10:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, members of structs are never reordered; polymorphism in C relies on it!
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> BASE_CLASS
<span class="k2">{</span>
  <span class="k1">int</span> a, b, c<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
<span class="k1">struct</span> DERIVED_CLASS
<span class="k2">{</span>
  <span class="k1">int</span> a, b, c<span class="k2">;</span>
  <span class="k1">float</span> extra_data<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
Polymorhism in C is done by type casting to and from stuff that looks like that.</p><p>eg.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">float</span> my_function<span class="k2">(</span><span class="k1">struct</span> BASE_CLASS <span class="k3">*</span>foo<span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">float</span> bar <span class="k3">=</span> foo-&gt;a <span class="k3">*</span> foo-&gt;b<span class="k2">;</span>
  <span class="k1">if</span> <span class="k2">(</span>foo-&gt;c <span class="k3">=</span><span class="k3">=</span> <span class="n">5</span><span class="k2">)</span>
  <span class="k2">{</span>
    bar<span class="k3">*</span><span class="k3">=</span><span class="k2">(</span><span class="k2">(</span>DERIVED_CLASS<span class="k3">*</span><span class="k2">)</span>foo<span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>extra_data<span class="k2">;</span>
  <span class="k2">}</span>
  <span class="k1">return</span> bar<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Wed, 07 Sep 2005 16:15:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Erm that doesn&#39;t seem relibable. Did I miss something or is the struct automatically padded?  And how much padding is it safe to rely on, is there?</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">struct</span> sta</td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td>  <span class="k1">int</span> a, b, c<span class="k2">;</span></td></tr><tr><td class="number">4</td><td><span class="k2">}</span><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">struct</span> stb</td></tr><tr><td class="number">7</td><td><span class="k2">{</span></td></tr><tr><td class="number">8</td><td>  <span class="k1">int</span> a, b, c<span class="k2">;</span></td></tr><tr><td class="number">9</td><td>  <span class="k1">int</span> d<span class="k2">[</span><span class="n">5120</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td><span class="k1">void</span> this_shouldnt_work_should_it<span class="k2">(</span><span class="k2">)</span> <span class="c">//?</span></td></tr><tr><td class="number">13</td><td><span class="k2">{</span></td></tr><tr><td class="number">14</td><td>  sta A<span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  stb <span class="k3">*</span> B <span class="k3">=</span> <span class="k2">(</span>stb<span class="k2">)</span><span class="k3">&amp;</span>A<span class="k2">;</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td>  B-&gt;d<span class="k2">[</span><span class="n">4000</span><span class="k2">]</span> <span class="k3">=</span> <span class="n">15</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

How can one be gauranteed that enough padding will be added?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Wed, 07 Sep 2005 21:50:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That one definitely shouldn&#39;t work. If it compiles then it should segfault if you try writing to the char array
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Wed, 07 Sep 2005 21:53:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Since doubles/floats are defined specifically by IEEE, why arn&#39;t they platform/etc dependant?
</p></div></div><p>
In addition to what gillius said, not all computers use IEEE floats.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Wed, 07 Sep 2005 21:57:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
should segfault
</p></div></div><p>
Right, that&#39;s what I&#39;d expact.</p><p>By those principals -- Why can what Karadoc~~ posted be guaranteed to work?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Wed, 07 Sep 2005 22:25:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Karadoc~~&#39;s code is garanteed to work if c==5 only the type of the object is DERIVED_CLASS. It would be more clear if the variable was named &#39;type&#39;. It&#39;s just a matter of design. It&#39;s up to the programmer to ensure the types are right.<br />To perform polymorphism in C you have to keep track of the type of the object anyway.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (da_flo)</author>
		<pubDate>Wed, 07 Sep 2005 22:34:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if (foo-&gt;c == 5)
</p></div></div><p> Ahh, I didn&#39;t see that before.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Wed, 07 Sep 2005 23:17:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Why can what Karadoc~~ posted be guaranteed to work?
</p></div></div><p>
You can acces the elements common to sta and stb, but you cannot acces elements specific to stb by casting sta to stb.</p><p>Think about it: sta is the base, stb is a derived.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 07 Sep 2005 23:23:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Right that&#39;s what I thought, then I saw Karadocs code and I thought he was implying that you could -- I didn&#39;t see the c==5 thing....
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Wed, 07 Sep 2005 23:26:38 +0000</pubDate>
	</item>
</rss>
