<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] al_ustr_empty_string() questions</title>
		<link>http://www.allegro.cc/forums/view/606378</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 12 Feb 2011 02:43:06 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1) Should the result of al_ustr_empty_string() be freed?</p><p>2) Is this valid code?</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_USTR"><span class="a">ALLEGRO_USTR</span></a> <span class="k3">*</span>str <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_ustr_empty_string"><span class="a">al_ustr_empty_string</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
...
<a href="http://www.allegro.cc/manual/al_ustr_free"><span class="a">al_ustr_free</span></a><span class="k2">(</span>str<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Fri, 11 Feb 2011 18:44:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1) </p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Return a pointer to a static empty string. The string is read only.</p></div></div><p>

2) Valid C, but wrong.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Fri, 11 Feb 2011 18:49:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Shouldn&#39;t the return of <span class="source-code"><a href="http://www.allegro.cc/manual/al_ustr_empty_string"><span class="a">al_ustr_empty_string</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></span> better be a <span class="source-code"><span class="k1">const</span> <a href="http://www.allegro.cc/manual/ALLEGRO_USTR"><span class="a">ALLEGRO_USTR</span></a><span class="k3">*</span></span> then?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 11 Feb 2011 18:59:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;d think so, wouldn&#39;t you?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Fri, 11 Feb 2011 19:00:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, that&#39;s what I would have thought. Or said. Or whatever.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 11 Feb 2011 19:01:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Return a pointer to a static empty string. The string is read only.
</p></div></div><p>

The above is so vague. It can be interpreted in many ways:</p><ul><li><p>the string buffer is read only, but the returned Allegro string is not and must be freed.</p><br /></li><li><p>the string buffer is read only, and the returned Allegro string is read only, and so it must not be freed.</p><br /></li><li><p>the string buffer is read only, but the returned Allegro string is not, but even if it is freed, nothing will happen.</p><br /></li><li><p>the string buffer is read only, and the returned Allegro string is read only, and if it is freed, nothing will happen.</p></li></ul><p>I think there is a problem with this function; the returned string should be available to free, since it cannot be shared.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Fri, 11 Feb 2011 21:39:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The return should be const, yes.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606378/902918#target">axilmar</a> said:</div><div class="quote"><p>The above is so vague. It can be interpreted in many ways:</p></div></div><p>

Only if you overlook the &quot;static&quot; in there. But yes, always better to be explicit, maybe someone can make a patch which adds something like &quot;The string is read only and must not be freed.&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Fri, 11 Feb 2011 21:52:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What does &#39;static&#39; refer to in this context? is the string buffer static or the ALLEGRO_USTR instance static? or both?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Fri, 11 Feb 2011 21:55:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, as I said, someone should make it more clear. It means both apparently.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Fri, 11 Feb 2011 22:02:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>When in doubt, look at the source:
</p><div class="source-code"><div class="toolbar"><span class="name">utf8.c</span><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><span class="c">/* Function: al_ustr_empty_string</span>
<span class="number"> 2</span><span class="c"> */</span>
<span class="number"> 3</span><a href="http://www.allegro.cc/manual/ALLEGRO_USTR"><span class="a">ALLEGRO_USTR</span></a> <span class="k3">*</span><a href="http://www.allegro.cc/manual/al_ustr_empty_string"><span class="a">al_ustr_empty_string</span></a><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="number"> 4</span><span class="k2">{</span>
<span class="number"> 5</span>   <span class="k1">static</span> <span class="k1">struct</span> _al_tagbstring empty <span class="k3">=</span> _al_bsStatic<span class="k2">(</span><span class="s">""</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 6</span>   <span class="k1">return</span> <span class="k3">&amp;</span>empty<span class="k2">;</span>
<span class="number"> 7</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Fri, 11 Feb 2011 23:27:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So it is not thread safe.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Fri, 11 Feb 2011 23:46:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why wouldn&#39;t it be? <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sat, 12 Feb 2011 00:01:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Because empty will be initialized by the first caller of <span class="source-code"><a href="http://www.allegro.cc/manual/al_ustr_empty_string"><span class="a">al_ustr_empty_string</span></a><span class="k2">(</span><span class="k2">)</span></span>, and it might happen that two threads try to do this at the same time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Sat, 12 Feb 2011 00:10:06 +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/606378/902954#target">tobing</a> said:</div><div class="quote"><p>Because empty will be initialized by the first caller</p></div></div><p>

It&#39;s static, it will be initialized before main(). So completely thread-safe.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sat, 12 Feb 2011 01:39:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s static, but inside a function. So it&#39;s only initialized when the first call to the function occurs. </p><p><a href="http://stackoverflow.com/questions/55510/when-do-function-level-static-variables-get-allocated-initialized">http://stackoverflow.com/questions/55510/when-do-function-level-static-variables-get-allocated-initialized</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Sat, 12 Feb 2011 01:50:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Are you sure that isn&#39;t just C++? In C, I can&#39;t even seem to initialize my static local variable with a function... <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" /> It needs to be a constant expression (which sounds like compile-time to me). In C++, it&#39;s no problem.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Sat, 12 Feb 2011 02:15:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In C static variables (no matter if inside a function or not) are initialized before main().</p><p>[edit:]</p><p>Found it in the C99 standard, 6.2.4 §3:</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
An object whose identifier is declared with external or internal linkage, or with the<br /> storage-class specifier static has static storage duration. Its lifetime is the entire<br />  execution of the program and its stored value is initialized only once, prior to program<br /> startup.
</p></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sat, 12 Feb 2011 02:27:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OK, I&#39;m so much used to C++ that I tend to forget that there might so differences like this. Sorry.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Sat, 12 Feb 2011 02:31:22 +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/606378/902974#target">tobing</a> said:</div><div class="quote"><p>OK, I&#39;m so much used to C++ that I tend to forget that there might so differences like this.</p></div></div><p>
So does that mean that in C++ code, the compiler has to insert code to check whether a static variable has been used before and is therefore initialised already, or needs to be initialised now? Or does it do code-flow analysis to figure out where to insert the initialisation?<br />Neither of those sound particularly appealing to me...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sat, 12 Feb 2011 02:38:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The first. It&#39;s checked at runtime if the static is already initialized or not, and the first one coming by does the initialization. </p><p>When it comes to overhead and what the compiler generates, I don&#39;t know... </p><p>The good thing about this is, you can control the initialization, i.e. when the static is initialized. It&#39;s quite simple to implement singletons that way, or control the order of initialization of &quot;global&quot; variables between different compilation units.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Sat, 12 Feb 2011 02:43:06 +0000</pubDate>
	</item>
</rss>
