<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>The Danger of c-style Strings</title>
		<link>http://www.allegro.cc/forums/view/591346</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 17 May 2007 06:17:27 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This only applies to c++ programmers.<br /><b>How c-style strings are dangerous</b><br />Let&#39;s say that you are making an editable name. You type this code in:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span><span class="k3">*</span> name <span class="k3">=</span> <span class="s">"bob"</span><span class="k2">;</span>
name<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">'B'</span><span class="k2">;</span><span class="c">//BUG!</span>
</pre></div></div><p>
The first line assigns a name to name, but it&#39;s a const char*! When the second line tries to edit the const char it has an error and might crash. The compiler doesn&#39;t detect this because it sees it as you modifing a char*.<br />You can still assign a different value to the whole pointer though:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span><span class="k3">*</span> name <span class="k3">=</span> <span class="s">"Bob"</span><span class="k2">;</span>
name <span class="k3">=</span> <span class="s">"Joe"</span><span class="k2">;</span>
</pre></div></div><p>
With character arrays this isn&#39;t a problem because the compiler automatically copies the characters into the array.<br /><span class="source-code"><span class="k1">char</span> name<span class="k2">[</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">"Bob"</span><span class="k2">;</span><span class="c">//Works fine.</span></span><br />One of the major problems with character arrays is that they can write on top of uncharted memory. Consider this example:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span> name<span class="k2">[</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">"Bob"</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>name,<span class="s">"This_name_is_too_long!"</span><span class="k2">)</span><span class="k2">;</span>
<span class="c">//Writes into unknown space, possibly over another object!</span>
</pre></div></div><p>this can also happen if the user forgets to put a terminating NULL on the end of the string.
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span><span class="k3">*</span> make_all_ms<span class="k2">(</span><span class="k1">char</span> in<span class="k2">)</span>
<span class="k2">{</span>
<span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> in<span class="k3">&lt;</span>i&gt; <span class="k3">!</span><span class="k3">=</span> <span class="s">'\0'</span><span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
in<span class="k3">&lt;</span>i&gt; <span class="k3">=</span> <span class="s">'m'</span><span class="k2">;</span>
<span class="k2">}</span>
<span class="k1">char</span> name<span class="k2">[</span><span class="n">4</span><span class="k2">]</span><span class="k2">;</span>
name<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">'H'</span><span class="k2">;</span>
name<span class="k2">[</span><span class="n">1</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">'i'</span><span class="k2">;</span>
name <span class="k3">=</span> make_all_ms<span class="k2">(</span>name<span class="k2">)</span><span class="k2">;</span><span class="c">//BUG!</span>
</pre></div></div><p>
<b>The solution to all your problems:</b><br />Use the string class in the std namespace.It has all the functionality of a c-style string exept it is safer and easier to use. Some examples are shown below.
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span>
string str1 <span class="k3">=</span> <span class="s">"Hello World!"</span><span class="k2">;</span><span class="c">//This works.</span>
string str2<span class="k2">;</span>
str1 <span class="k3">+</span><span class="k3">=</span> <span class="s">"I own a lizard."</span>
<span class="c">//concatenates the strings. Now is "Hello World!I own a lizard."</span>
str2 <span class="k3">=</span> str1<span class="k2">;</span>
</pre></div></div><p>
Also if you need to use a c-style string in a function or something you can do this:
</p><div class="source-code snippet"><div class="inner"><pre>string name <span class="k3">=</span> <span class="s">"Bob"</span><span class="k2">;</span>
textprintf<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,<a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,name.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
The string cleans itself up when it goes out of scope.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (someone972)</author>
		<pubDate>Fri, 11 May 2007 07:49:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Of course C strings are dangerous if you do something wrong <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /></p><p>PEBKAC.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 11 May 2007 07:51:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I recommend ropes myself for large chunks of text. In such a case regular strings can wreak havock <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Fri, 11 May 2007 09:16:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What is a rope? How do you use it?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 11 May 2007 09:24:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A rope (IPA: <i>rəʊp</i>) is a length of fibers, twisted or braided together to improve strength for pulling and connecting. It has tensile strength but is too flexible to provide compressive strength (i.e., it can be used for pulling, not pushing). Rope is thicker and stronger than similarly constructed cord, line, string, or twine. Common materials for rope include natural fibers such as Manila hemp, hemp, linen, cotton, coir, jute, and sisal. Synthetic fibers in use for rope-making include polypropylene, nylon, polyester (e.g. PET), polyethylene (e.g. Spectra) and Aramids (e.g. Twaron, Technora and Kevlar). Some ropes are constructed of mixtures of several fibres or use co-polymer fibres. Ropes can also be made out of metal fibers. Ropes have been constructed of other fibrous materials such as silk, wool, and hair, but such ropes are not generally available.</p><p>The use of ropes for hunting, pulling, fastening, attaching, carrying, lifting, and climbing dates back to prehistoric times and has always been essential to mankind&#39;s technological progress.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Fri, 11 May 2007 09:27:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Couldn&#39;t resist , could you?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 11 May 2007 09:36:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I could, but I didn&#39;t.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Fri, 11 May 2007 10:00:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I should just answer my own questions any way.</p><p>Here&#39;s a link to another definition of rope which as I found out is an <br />SGI extension and not part of the C++ standard (their words).</p><p><a href="http://www.sgi.com/tech/stl/Rope.html">http://www.sgi.com/tech/stl/Rope.html</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 11 May 2007 10: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>
The solution to all your problems:<br />Use the string class in the std namespace.It has all the functionality of a c-style string exept it is safer and easier to use. Some examples are shown below.
</p></div></div><p>

No, really? <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /></p><p>Oh come on, the string class was probably the first thing I learned from STL. It&#39;s really not that hard to find and there are a lot of examples out there on how to use it. Your post is just nothing new, sorry.</p><p>Besides, since it&#39;s not a programming <b>question</b>, I believe it doesn&#39;t belong to this forum <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Fri, 11 May 2007 11:49:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I use c-style strings everywhere, I just keep in mind that they&#39;re like arrays of characters, not strings, and treat them as such. If I want to mess with them as a string, I use a string command. (You&#39;ll never catch me copying one string to another with an = sign for example.)</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Fri, 11 May 2007 12:19:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You programmer must know what you do. </p><p>If not, go with some scripting language who will get the job done for you.</p><p>C is not for quiche eater. I said.</p><p>_
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Fri, 11 May 2007 12:48:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
This only applies to c++ programmers.
</p></div></div><p>Do C programmers just know what they&#39;re doing or something? <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Fri, 11 May 2007 16:07:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, the alien have not invaded C, that common knowledge they are residing inside the stl ;-p
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Fri, 11 May 2007 16:14:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Do C programmers just know what they&#39;re doing or something?
</p></div></div><p>They have to learn quickly or risk blowing their foot off.</p><p>Whereas C++ lets you quietly blow your foot off <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 11 May 2007 16:15:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>&quot;How much C could a C++ if a C+ could +C?&quot;</p><p>A lot of experts use C over C++, and the two can be combined in as much or as little quantity as desired. I simply prefer some of the coding conventions of C++ over C and have only recently started taking advantage of some of the more advanced C++ features.</p><p>...C++ string objects still scare me though. <img src="http://www.allegro.cc/forums/smileys/embarassed.gif" alt=":-[" /></p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Fri, 11 May 2007 21:44:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m not sure I understand the overall point of this thread.  Was it just to recommend  people to switch to the string class?  <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>If it was then good job.  Had I not already been using them for a long time I would have been convinced to switch (maybe) <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Fri, 11 May 2007 23:51:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I just keep in mind that they&#39;re like arrays of characters
</p></div></div><p>

What are they then if they are only like arrays of characters? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
...C++ string objects still scare me though. <img src="http://www.allegro.cc/forums/smileys/embarassed.gif" alt=":-[" />
</p></div></div><p>

Why?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sat, 12 May 2007 01:24:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">BAF said:</div><div class="quote"><p>

Why?
</p></div></div><p>
Maybe because they use so many template arguments?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sat, 12 May 2007 02:34:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>C++ string objects do?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sat, 12 May 2007 02:43:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes they do. basic_string doesn&#39;t though IIRC.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Sat, 12 May 2007 02:53:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>basic_string is a templated typedef, but what uses a template argument in basic_string?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sat, 12 May 2007 02:59:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
One of the things I missed when I first switched from Basic to C was proper integrated string support. Now I&#39;ve got used to it though. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Tue, 15 May 2007 15:17:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>just store all strings on the heap, and remember to free them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (m c)</author>
		<pubDate>Tue, 15 May 2007 15:27:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Or do what I do!<br />Store things on the heap and leave them there.</p><p>EDIT:<br />Bonus points if you malloc() the string every loop...</p><p>EDIT2:<br />Without free()ing it first.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (James Stanley)</author>
		<pubDate>Tue, 15 May 2007 17:41:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Or do what I do!<br />Store things on the heap and leave them there.</p><p>EDIT:<br />Bonus points if you malloc() the string every loop...</p><p>EDIT2:<br />Without free()ing it first.
</p></div></div><p>
Oh goody, I&#39;m going to go for the high score!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Tue, 15 May 2007 17:43:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Templated arguments in strings? I have been using strings a lot and I haven&#39;t noticed a single templated thing about them, ever.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trezker)</author>
		<pubDate>Tue, 15 May 2007 17:47:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I asked the same thing. I guess the other string class has templated arguments, but std::string is just a typedef for basic_string&lt;char&gt;, and basic_string doesn&#39;t have templates.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Tue, 15 May 2007 17:59:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m sorry, but I just don&#39;t see why this thread was started. Did anyone NOT know that? Excepting maybe a few beginners who don&#39; have much experience?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Tue, 15 May 2007 20:02:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
basic_string doesn&#39;t have templates.
</p></div></div><p>No, but it IS a template.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 15 May 2007 23:46:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So? That doesn&#39;t make the &quot;templates are confusing&quot; argument valid. You can happily use std::string and be totally ignorant of templates for the most part (you may be confused by some compiler errors, then again, I&#39;ve seen way worse errors).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Wed, 16 May 2007 10:18:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I use std::string, and I have no idea what a template is. I haven&#39;t had a problem with it since the last one I asked about here.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Wed, 16 May 2007 17:48:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I appreciate the free refreshments. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Wed, 16 May 2007 19:24:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I use std::string, and I have no idea what a template is. I haven&#39;t had a problem with it since the last one I asked about here.
</p></div></div><p>

Hear the evidence for yourself!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Wed, 16 May 2007 19:51:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Heh, why do threads like this always end in an argument?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 17 May 2007 06:12:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hmm, I said something about it being a.cc, and expecting a thread not to end up like that. I forgot who sigged it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Thu, 17 May 2007 06:17:27 +0000</pubDate>
	</item>
</rss>
