<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wikidot="http://www.wikidot.com/rss-namespace">

	<channel>
		<title>Comments for page &quot;Magic numbers in C&quot;</title>
		<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c</link>
		<description>Posts in the discussion thread &quot;Magic numbers in C&quot;</description>
				<copyright></copyright>
		<lastBuildDate>Sat, 01 Aug 2015 21:49:00 +0000</lastBuildDate>
		
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2232068</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2232068</link>
				<description></description>
				<pubDate>Fri, 13 Feb 2015 18:08:45 +0000</pubDate>
				<wikidot:authorName>citizenfour</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>much safer version is:</p> <p>static_assert(sizeof (seq) == SEQ_SIZE);<br /> ptr += sizeof (seq);</p> <p>also auto and decltype are your good friends.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2193781</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2193781</link>
				<description></description>
				<pubDate>Fri, 02 Jan 2015 08:05:32 +0000</pubDate>
				<wikidot:authorName>ovmjm</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Different language, same conclusions :<br /> <a href="http://wiki.laptop.org/go/Forth_Lesson_6">http://wiki.laptop.org/go/Forth_Lesson_6</a> (see Stylistic Point - Too Many Constants)</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2167861</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2167861</link>
				<description></description>
				<pubDate>Fri, 28 Nov 2014 10:34:43 +0000</pubDate>
				<wikidot:authorName>Maciej Sobczak</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>That will depend on the number itself. Some of them are self-descriptive or are heavily coupled to foundational concepts, like 0, 1, 8 (num of bits in a byte), but some are not (2, 100, 1000, etc.). I tend to use literal values for the former and named values for the latter group.</p> <p>In both cases the idea is that the reader should have no additional questions when reading the code and the meaning of each number (whether named or not) should be clear from the place where it is used.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2167858</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2167858</link>
				<description></description>
				<pubDate>Fri, 28 Nov 2014 10:28:14 +0000</pubDate>
				<wikidot:authorName>martin_sustrik</wikidot:authorName>				<wikidot:authorUserId>939</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <blockquote> <p>Obviously, if you parse two bytes, then you are two bytes further down the stream, aren't you?</p> </blockquote> <p>Yes, but try reading muliple such fields, then try to pad the whole thing to 4-byte boundary. Oops, suddenly you need to know how many bytes were written up to now. The stream abstraction is suddenly broken.</p> <p>Anyway, the whole parser discussion is irrelevant. We are dealing with computers and computers deal with numbers. At some point down the stack you just have to start using numbers (e.g. number 8 in your example). So, when reaching that point, what are you going to do? Use literals, symbolic names or what?</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2167838</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2167838</link>
				<description></description>
				<pubDate>Fri, 28 Nov 2014 09:54:40 +0000</pubDate>
				<wikidot:authorName>Maciej Sobczak</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Do you need to extract half-word (two bytes) from the buffer? Try this:</p> <p>const uint8_t lsb = buffer[current_position++];<br /> const uint8_t msb = buffer[current_position++];</p> <p>const uint16_t value = ((uint16_t)msb &#171; 8) | (uint16_t)lsb;</p> <p>I leave it up to you to decide whether magic number 8 deserves to be named, but certainly there is no need to mess with number 2 - it is implicit in code as a natural consequence of *parsing* two consecutive bytes. Obviously, if you parse two bytes, then you are two bytes further down the stream, aren't you? Note also that if you change the protocol to read full word (4 bytes) instead, there is no need to mess with that number or with sizeof(T). But what is most important, endiannes is covered by means of proper computation, so the code will work correctly no matter what is the CPU architecture, without any preprocessor conditional shit. Certainly this is more robust and more portable.<br /> And as a bonus there are no pointer conversions between unrelated types, it's is all just, well&#8230; parsing.</p> <p>If you want to see a complete non-trivial serializer/deserializer written this way, see here:</p> <p><a href="http://www.inspirel.com/yami4/misra.html">http://www.inspirel.com/yami4/misra.html</a></p> <p>In any case, I recommend a good and established coding standard before doing such work. Low-level programming does not have to mean crappy programming.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2167824</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2167824</link>
				<description></description>
				<pubDate>Fri, 28 Nov 2014 09:09:54 +0000</pubDate>
				<wikidot:authorName>martin_sustrik</wikidot:authorName>				<wikidot:authorUserId>939</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Well, this is the experience from parsing 3GPP protocols: They are so irregular that whatever abstraction you use (uint16_t state = read_short (stream); or whatever) you eventually end up looking directly at the undelying buffer and messing with individual bytes.</p> <p>Also, even if the above was not true, the problem is just shifted to the parsing tool. It has to extract 2 bytes from the buffer. Should it use a symbolic name or constant 2? Another option, as mentioned in the article, is sizeof(uint16_t) which is just a fancy a somehow less readable way of writing constant 2.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2167818</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2167818</link>
				<description></description>
				<pubDate>Fri, 28 Nov 2014 08:56:25 +0000</pubDate>
				<wikidot:authorName>Maciej Sobczak</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Your network protocol example is not convincing. In fact, it is just broken as it relies on pointer conversions between unrelated types. This kind of code would not pass any safety audit, but even if you don't plan for verification, using such crappy code to justify that magic numbers are good for readability is just nonsense. If you start with crap, you will end up with crappy conclusions.<br /> Now, try rewrite your network protocol parser with some real, well&#8230; parsing. You know, by consuming bytes and using them to compute higher-level values. Use some real coding standard as a guideline (MISRA-C, perhaps?) and then you will discover that you don't need any magic numbers to keep it floating. And, as a side-effect, you might even end up with code that can defend itself at safety reviews.<br /> (And no, it will not be any slower. Pointer conversions between single bytes and words don't buy any performance over properly indexed buffer iteration.)</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2167219</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2167219</link>
				<description></description>
				<pubDate>Thu, 27 Nov 2014 07:56:42 +0000</pubDate>
				<wikidot:authorName>martin_sustrik</wikidot:authorName>				<wikidot:authorUserId>939</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Yes, same thing for SPARC and other RISC platforms. However, doing it in a portable way would just make the article less clear.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://250bpm.com/forum/t-1057034#post-2167213</guid>
				<title>(no title)</title>
				<link>http://250bpm.com/forum/t-1057034/magic-numbers-in-c#post-2167213</link>
				<description></description>
				<pubDate>Thu, 27 Nov 2014 07:47:11 +0000</pubDate>
				<wikidot:authorName>Pierre Chapuis</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>This code that reads from &#8216;ptr` is probably already broken anyway, except if the initial address is 3 mod 4 (maybe even 7 mod 8). This kind of unaligned reads works on x86 but probably won&#8217;t on some ARM-based platforms such as Android, causing a SIGBUS.</p> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>