<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Freeing memory of memfile</title>
		<link>http://www.allegro.cc/forums/view/612772</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 12 Jun 2013 05:48:04 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m loading binary data stored in an array into allegro using al_open_memfile(). The docs of allegro states:</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>It should be closed with al_fclose. After the file is closed, you are responsible for freeing the memory (if needed).</p></div></div><p>

How exactly can I do this?</p><p>Thanks for the help
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Mon, 10 Jun 2013 23:26:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If the memory you pass to al_open_memfile is dynamically allocated, you have to free it with <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a></span> (C) or <span class="source-code"><span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span></span> (C++).</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span> <span class="k3">*</span>buffer <span class="k3">=</span> <span class="k1">new</span> <span class="k1">char</span><span class="k2">[</span><span class="n">1000</span><span class="k2">]</span><span class="k2">;</span>
...
<a href="http://www.allegro.cc/manual/al_open_memfile"><span class="a">al_open_memfile</span></a><span class="k2">(</span>...<span class="k2">)</span><span class="k2">;</span>
<span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span> buffer<span class="k2">;</span>
</pre></div></div><p>

If it&#39;s not dynamically allocated, like:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">static</span> <span class="k1">char</span> buffer<span class="k2">[</span><span class="n">1000</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span> ... <span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>

Then you shouldn&#39;t free it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Mon, 10 Jun 2013 23:30:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks, that&#39;s what I needed
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Mon, 10 Jun 2013 23:40:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can use a function like file_size_ex() (or whatever&#39;s equivalent for Allegro 5) to determine how large your buffer needs to be, if you want to avoid hard-coding file sizes in your program.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (raynebc)</author>
		<pubDate>Tue, 11 Jun 2013 23:02:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612772/984927#target">raynebc</a> said:</div><div class="quote"><p> determine how large your buffer needs to be</p></div></div><p>This is about memfiles, which is when you open a block of memory as a file, not reading files on disk into memory.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Wed, 12 Jun 2013 01:20:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>They can be related. I think it was the TGA loader... it reads a byte/word at a time from file and is really slow. Much faster if you load the file into memory and load it with the memfile addon.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 12 Jun 2013 01:23:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612772/984934#target">Trent Gamblin</a> said:</div><div class="quote"><p>it reads a byte/word at a time from file</p></div></div><p>
This shouldn&#39;t be a problem in itself, because the actual I/O is typically buffered, see <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_692.html" target="_blank">setbuf</a><span class="k2">(</span><span class="k2">)</span></span>. Now maybe it involves back-and-forth seeking.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Wed, 12 Jun 2013 05:29:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Try it yourself. I already have.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 12 Jun 2013 05:48:04 +0000</pubDate>
	</item>
</rss>
