<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bugs | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/bugs/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Tue, 28 Mar 2023 11:02:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>bugs | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>Yet another curious bug</title>
		<link>https://learncgames.com/yet-another-curious-bug/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=yet-another-curious-bug</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sun, 26 Mar 2023 23:00:37 +0000</pubDate>
				<category><![CDATA[bugs]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[game]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=3225</guid>

					<description><![CDATA[<p>I&#8217;m aware that C is notorious for unexpected behaviour (UB). Let me describe this bug: My second game in C + SDL for my newest eBook is Match Three and compiled with Clang-11 on my Raspberry Pi 4B. I&#8217;m getting a weird bug that doesn&#8217;t occur when I compile it on Windows under MSVC; the [&#8230;]</p>
The post <a href="https://learncgames.com/yet-another-curious-bug/">Yet another curious bug</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img fetchpriority="high" decoding="async" class="alignleft size-medium wp-image-3227" src="https://learncgames.com/wp-content/uploads/2023/03/matchthree-bug-300x233.png" alt="MatchThree latest version with a bug" width="300" height="233" srcset="https://learncgames.com/wp-content/uploads/2023/03/matchthree-bug-300x233.png 300w, https://learncgames.com/wp-content/uploads/2023/03/matchthree-bug-1024x796.png 1024w, https://learncgames.com/wp-content/uploads/2023/03/matchthree-bug-768x597.png 768w, https://learncgames.com/wp-content/uploads/2023/03/matchthree-bug.png 1036w" sizes="(max-width: 300px) 100vw, 300px" />I&#8217;m aware that C is notorious for unexpected behaviour (UB). Let me describe this bug:</p>
<div data-reddit-rtjson="{&quot;entityMap&quot;:{},&quot;blocks&quot;:[{&quot;key&quot;:&quot;dbbsn&quot;,&quot;text&quot;:&quot;I'm creating a game in C + SDL for an eBook in C and compiled it with Clang-11 on my Raspberry Pi 4B. I'm getting a weird bug that doesn't occur when I compile it on Windows under MSVC; the only differences are the paths to SDL and the sn._s calls but there aren't many of those. Also I compiled it with clang-14 on Ubuntu 22.04 LTS on a Hyper-VM and that behaves properly. &quot;,&quot;type&quot;:&quot;unstyled&quot;,&quot;depth&quot;:0,&quot;inlineStyleRanges&quot;:[],&quot;entityRanges&quot;:[],&quot;data&quot;:{}},{&quot;key&quot;:&quot;34s6i&quot;,&quot;text&quot;:&quot;The weird bug is this. The game is a MatchThree game with 10 x 10 pieces. A 2D array of structs. One field is a field called size. It's 0 normally but if &gt; 0 then it is dec'd and the piece is drawn rotated by 8 degrees. When it reaches 0 the piece is removed. &quot;,&quot;type&quot;:&quot;unstyled&quot;,&quot;depth&quot;:0,&quot;inlineStyleRanges&quot;:[],&quot;entityRanges&quot;:[],&quot;data&quot;:{}}]}">
<div class="" data-block="true" data-editor="35ef0f" data-offset-key="35ef0f_initial-0-0">
<div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr" data-offset-key="35ef0f_initial-0-0"><span data-offset-key="35ef0f_initial-0-0">My second game in C + SDL for my newest eBook is Match Three and compiled with Clang-11 on my Raspberry Pi 4B. I&#8217;m getting a weird bug that doesn&#8217;t occur when I compile it on Windows under MSVC; the only differences between the source code on the PI and Windows are the paths to SDL and the sn._s calls but there aren&#8217;t many of those. Also I compiled it with clang-14 on Ubuntu 22.04 LTS on a Hyper-VM and that behaves properly.</span></div>
</div>
<p>&nbsp;</p>
<div data-offset-key="35ef0f_initial-0-0"></div>
<div class="" data-block="true" data-editor="35ef0f" data-offset-key="4u8u6-0-0">
<div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr" data-offset-key="4u8u6-0-0"><span data-offset-key="4u8u6-0-0">Each piece is held a struct in a 10 x 10 array. One field is  called size. It&#8217;s 0 normally but if it is set to a value, typically  64 then it counts down, one per frame and the piece is drawn rotated by 8 degrees each frame. When it reaches 0 the piece is removed. This is my &#8220;down the plughole&#8221; way of removing pieces. It takes exactly 64/60 seconds to vanish. I use the size field to draw it when size is non-zero so it shrinks as it rotates It&#8217;s a nice effect.</span></div>
</div>
</div>
<p>&nbsp;</p>
<div data-offset-key="4u8u6-0-0"></div>
<div data-offset-key="4u8u6-0-0"></div>
<div data-offset-key="4u8u6-0-0">The bug though is that when it starts up, several pieces start rotating and keep rotating. The size field stays at 63. There&#8217;s no code that explains this behaviour and it only happens on the Raspberry Pi, not on Windows or on Ubuntu. Is it a compiler bug or some weird UB on the PI version. It&#8217;s an interesting bug and I&#8217;ve only wasted a week of evenings on it so far!</div>
<p></p>The post <a href="https://learncgames.com/yet-another-curious-bug/">Yet another curious bug</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3225</post-id>	</item>
		<item>
		<title>Raspberry Pi Sound issues- trying to fix it</title>
		<link>https://learncgames.com/raspberry-pi-sound-issues-trying-to-fix-it/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=raspberry-pi-sound-issues-trying-to-fix-it</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Mon, 19 Dec 2022 00:00:02 +0000</pubDate>
				<category><![CDATA[bugs]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Ebook]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[raspberry-pi]]></category>
		<category><![CDATA[sound]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=3145</guid>

					<description><![CDATA[<p>In working through my Linux/Raspberry Pi eBook(Yes &#8211; the second eBook!), I&#8217;m up to the chapter where sounds are introduced using the SDL_mixer library. And I&#8217;ve hit two sets of problems. It sometimes refuses to initialize the sound code.  This code below hits the LogError line: int success=Mix_OpenAudio(22050, AUDIO_S16LSB, 2, 8192); if (success==-1 ) { [&#8230;]</p>
The post <a href="https://learncgames.com/raspberry-pi-sound-issues-trying-to-fix-it/">Raspberry Pi Sound issues- trying to fix it</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:38,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/openclipart-vectors-30363\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=158394&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20251230124419\/https:\/\/pixabay.com\/users\/openclipart-vectors-30363\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 11:14:50&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-11 00:49:37&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-15 09:57:58&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-22 03:49:13&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-27 11:03:50&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-04 08:58:03&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-10 04:10:04&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-14 23:28:01&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-23 04:24:55&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-26 18:05:22&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-04 12:46:30&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-09 07:36:36&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 11:45:04&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-20 08:36:45&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-20 08:36:45&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:39,&quot;href&quot;:&quot;https:\/\/pixabay.com\/\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=158394&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 11:14:54&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-11 00:49:37&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-15 09:57:57&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-22 03:49:12&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-27 11:03:50&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-04 08:58:03&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-10 04:10:04&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-14 23:28:01&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-23 04:24:56&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-26 18:05:20&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-04 12:46:30&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-09 07:36:39&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 11:45:04&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-19 12:07:41&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-19 12:07:41&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_3146" aria-describedby="caption-attachment-3146" style="width: 295px" class="wp-caption alignleft"><img decoding="async" class="size-medium wp-image-3146" src="https://learncgames.com/wp-content/uploads/2022/12/loudspeaker-295x300.png" alt="Loudpeaker" width="295" height="300" srcset="https://learncgames.com/wp-content/uploads/2022/12/loudspeaker-295x300.png 295w, https://learncgames.com/wp-content/uploads/2022/12/loudspeaker.png 630w" sizes="(max-width: 295px) 100vw, 295px" /><figcaption id="caption-attachment-3146" class="wp-caption-text">Image by <a href="https://pixabay.com/users/openclipart-vectors-30363/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=158394">OpenClipart-Vectors</a> from <a href="https://pixabay.com//?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=158394">Pixabay</a></figcaption></figure>
<p>In working through my Linux/Raspberry Pi eBook(<em>Yes &#8211; the second eBook!)</em>, I&#8217;m up to the chapter where sounds are introduced using the SDL_mixer library. And I&#8217;ve hit two sets of problems.</p>
<p>It sometimes refuses to initialize the sound code.  This code below hits the LogError line:</p>
<pre><code class="language-c" data-line=""></code></pre>
<pre><code class="language-c" data-line="">	int success=Mix_OpenAudio(22050, AUDIO_S16LSB, 2, 8192);
	if (success==-1 ) {
		LogError(&quot;InitSetup failed to init audio&quot;);
	}</code></pre>
<pre><code class="language-c" data-line=""></code></pre>
<p>The other day it was working but not today. Now I have updated the Pi&#8217;s code (<em>sudo apt update</em> <em>etc</em>) but I wouldn&#8217;t have expected that to break it. I&#8217;ve been looking on the internet and find the whole thing a bit complicated.</p>
<p>I&#8217;ve got my Pi running 64-bit Raspberry Pi OS. I&#8217;ve changed the output device to headphones which plug into the headphone socket. If I run the VLC media player and tell it to play into the headphones, it will happily play the .wav files I&#8217;ve got for the asteroids game.</p>
<p>But if I run <strong>speaker-test</strong>, a terminal application with this command line</p>
<pre>speaker-test -c2 -twav -l7 plughw:1,0</pre>
<p>I get</p>
<pre>speaker-test 1.2.4

Playback device is default
Stream parameters are 48000Hz, S16_LE, 2 channels
WAV file(s)
Setting of hwparams failed: Invalid argument</pre>
<p>By running this command:</p>
<pre><code class="language-c" data-line="">aplay -L
</code></pre>
<p>I got 71 lines of output but of these these below are the most important</p>
<pre>output
<strong>hw</strong>:CARD=<strong>Headphones</strong>,DEV=0
    bcm2835 Headphones, bcm2835 Headphones
</pre>
<p>and the speaker-test command using the device parameter &#8211;<strong>Dhw:Headphones</strong> now worked. I&#8217;ve highlighted the bits in the aplay output needed to identify the device.</p>
<p>The new command  is</p>
<pre>speaker-test -c2 -twav -l7 -Dhw:Headphones</pre>
<p>I can now hear a female voice saying <strong>front left</strong> then <strong>front right</strong> a few times in my headphones.</p>
<p>So my Pi&#8217;s sound device is working; I just have to figure why SDL_mixer isn&#8217;t always. I&#8217;ll keep looking.</p>
<p><strong>And the second problem</strong> which only occurs when the mixer is working, is when you play a lot of sounds. The <strong>PlayASound()</strong> function checks the result. On Windows it never had a problem but on Raspberry Pi, when you blow up a lot of asteroids say at one time, it plays a number of explosions then returns an error for each explosion after that. I think there&#8217;s only so many channels; that&#8217;s an easy fix; just ignore the error and return as if it succeeded.</p>The post <a href="https://learncgames.com/raspberry-pi-sound-issues-trying-to-fix-it/">Raspberry Pi Sound issues- trying to fix it</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3145</post-id>	</item>
		<item>
		<title>To brace or not to brace</title>
		<link>https://learncgames.com/to-brace-or-not-to-brace/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=to-brace-or-not-to-brace</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Tue, 29 Sep 2020 23:00:25 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[bugs]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1502</guid>

					<description><![CDATA[<p>This isn&#8217;t exactly confined to C, in fact it applies to many programming languages where braces or curly brackets {like this} are optional after an if. It even applies to Pascal with Begin ends which are optional after an if. You can have the first or the second as shown below. if (expressions) DoSomething; if [&#8230;]</p>
The post <a href="https://learncgames.com/to-brace-or-not-to-brace/">To brace or not to brace</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:494,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/geralt-9301\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2372130&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260128125116\/https:\/\/pixabay.com\/users\/geralt-9301\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 15:26:13&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-10 17:39:43&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-07 20:06:17&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-28 11:18:07&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-04 04:10:45&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-08 00:43:22&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 22:11:44&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-20 19:36:37&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-20 19:36:37&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:495,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2372130&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 15:26:15&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-10 17:39:37&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-07 20:06:17&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-28 11:18:05&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-04 04:10:45&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-08 00:43:20&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 22:11:43&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-20 19:36:37&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-20 19:36:37&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:496,&quot;href&quot;:&quot;https:\/\/www.reddit.com\/r\/C_Programming\/comments\/iyu0ei\/an_argument_on_why_you_should_always_put_curly&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20250319234232\/https:\/\/www.reddit.com\/r\/C_Programming\/comments\/iyu0ei\/an_argument_on_why_you_should_always_put_curly\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 15:26:18&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-10 17:39:37&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-07 20:06:17&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-28 11:18:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-04 04:10:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-08 00:43:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-12 22:11:44&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-20 19:36:37&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-20 19:36:37&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_1504" aria-describedby="caption-attachment-1504" style="width: 300px" class="wp-caption alignleft"><img decoding="async" class="size-medium wp-image-1504" src="https://learncgames.com/wp-content/uploads/2020/09/binary-2372130_640-300x200.jpg" alt="Abstract programming picture" width="300" height="200" srcset="https://learncgames.com/wp-content/uploads/2020/09/binary-2372130_640-300x200.jpg 300w, https://learncgames.com/wp-content/uploads/2020/09/binary-2372130_640.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-1504" class="wp-caption-text">Image by <a href="https://pixabay.com/users/geralt-9301/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2372130">Gerd Altmann</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2372130">Pixabay</a></figcaption></figure>
<p>This isn&#8217;t exactly confined to C, in fact it applies to many programming languages where braces or curly brackets {like this} are optional after an if. It even applies to Pascal with Begin ends which are optional after an if.</p>
<p>You can have the first or the second as shown below.</p>
<pre><code class="language-c" data-line="">if (expressions) 
  DoSomething;

if (expression {
  DoSomething;
}</code></pre>
<p>I saw <a title="Link to discussion on C Programming Reddit" href="https://www.reddit.com/r/C_Programming/comments/iyu0ei/an_argument_on_why_you_should_always_put_curly/" target="_blank" rel="nofollow noopener noreferrer">a recent example</a> where the <strong>DoSomething</strong> was a logging expression which the compiler optimised away so the if, which lacked braces applied to the next statement. As the if was only true on rare occasions. It meant that that statement which should have been run every time was only run on those rare occasions.</p>
<p>So the prevailing wisdom and one that I entirely agree with is that <strong>you should always use braces.</strong> If you don&#8217;t you risk accidentally introducing bugs like this.</p>
<p><em>Note. that link is to a discussion on the C programming Subreddit (on Reddit.com).</em></p>
<p>.</p>The post <a href="https://learncgames.com/to-brace-or-not-to-brace/">To brace or not to brace</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1502</post-id>	</item>
		<item>
		<title>Fun with Raspberry Pi 4</title>
		<link>https://learncgames.com/fun-with-raspberry-pi-4/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fun-with-raspberry-pi-4</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Mon, 20 Apr 2020 23:00:55 +0000</pubDate>
				<category><![CDATA[Game]]></category>
		<category><![CDATA[raspberry-pi]]></category>
		<category><![CDATA[SDL]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[clang]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=425</guid>

					<description><![CDATA[<p>So it turned up and I setup things up similar to the 3B+. My interest was in seeing what frame rate it can manage compared to the 27 FPS that the 3B+ managed. When I eventually got Clang, VS Code etc. installed and setup, compiled etc. it crashed just like it did on the 3B+ [&#8230;]</p>
The post <a href="https://learncgames.com/fun-with-raspberry-pi-4/">Fun with Raspberry Pi 4</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:768,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/RonBerg-3029970\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1593406&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:769,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1593406&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:770,&quot;href&quot;:&quot;https:\/\/wiki.libsdl.org\/SDL_GetError&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20221007092534\/http:\/\/wiki.libsdl.org\/SDL_GetError&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 18:25:56&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-28 07:11:12&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-12 21:16:10&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-19 19:22:09&quot;,&quot;http_code&quot;:404}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-19 19:22:09&quot;,&quot;http_code&quot;:404},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:771,&quot;href&quot;:&quot;https:\/\/releases.llvm.org\/7.0.0\/tools\/clang\/docs\/ReleaseNotes.html&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20251207150448\/https:\/\/releases.llvm.org\/7.0.0\/tools\/clang\/docs\/ReleaseNotes.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 18:25:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 07:11:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 21:16:06&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-03-12 21:16:06&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_428" aria-describedby="caption-attachment-428" style="width: 300px" class="wp-caption alignleft"><img loading="lazy" decoding="async" class="size-medium wp-image-428" src="https://learncgames.com/wp-content/uploads/2020/04/ladybugs-1593406_640-300x168.jpg" alt="Bugs!" width="300" height="168" srcset="https://learncgames.com/wp-content/uploads/2020/04/ladybugs-1593406_640-300x168.jpg 300w, https://learncgames.com/wp-content/uploads/2020/04/ladybugs-1593406_640.jpg 359w" sizes="auto, (max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-428" class="wp-caption-text">Image by <a href="https://pixabay.com/users/RonBerg-3029970/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1593406">Ron van den Berg</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1593406">Pixabay</a></figcaption></figure>
<p>So it turned up and I setup things up similar to the 3B+. My interest was in seeing what frame rate it can manage compared to the 27 FPS that the 3B+ managed.</p>
<p>When I eventually got Clang, VS Code etc. installed and setup, compiled etc. it crashed just like it did on the 3B+ when first run. And again it failed to load the am2.msk file; I will really have to figure out what&#8217;s going on there. But also it would now not load the a1.png image. This is the one with the 24 x 280 x 280 asteroid images.</p>
<p>SDL provides a function <a title="Link goes to the LibSDL2.org Wiki page for SDL_GetError" href="https://wiki.libsdl.org/SDL_GetError" target="_blank" rel="noopener noreferrer">SDL_GetError</a> so I added that to the output when an image file fails to load. The error message that came back was <em>Texture dimensions are limited to 4096&#215;4096</em>. Now this is weird for this Pi has 4 GB of RAM (the 3B+ has 1GB) and is outputting on the same 24&#8243; monitor that the 3B+ used as well. Now it&#8217;s true that the a1.png file has dimensions of 6,720 x 64, so I can understand why that would cause grief but not why it worked on the 3B+. I suspect it has to do with RAM being allocated between the GPU and CPU so I&#8217;ll check that out.</p>
<p>I also installed Clang-6.0 to try that. On the Pi, installing clang defaults to Clang-7 (they changed the name so it no longer has a .0 on the end!). The <a title="Link to Clang-7 release notes" href="https://releases.llvm.org/7.0.0/tools/clang/docs/ReleaseNotes.html" target="_blank" rel="noopener noreferrer">release notes for Clang-7</a> suggested a possibility to do with abi incompatibility between Clang-7 and earlier versions. Bit of a longshot but I thought, that might explain the fail to load am2.msk file but no joy.</p>
<p>Well there&#8217;s nothing like juicy tender bugs to get my teeth into&#8230; the battle is on.</p>The post <a href="https://learncgames.com/fun-with-raspberry-pi-4/">Fun with Raspberry Pi 4</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">425</post-id>	</item>
		<item>
		<title>And the bug is fixed</title>
		<link>https://learncgames.com/and-the-bug-is-fixed/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=and-the-bug-is-fixed</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Mon, 06 Apr 2020 23:00:56 +0000</pubDate>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[bugs]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=320</guid>

					<description><![CDATA[<p>How did I fix it? Did I spend hours debugging, poring over every line? No. I woke up in the middle of the night and thought about it and remembered that I&#8217;d had a similar bug in this chapter when working on the original Ebook. Stupidly I&#8217;d put the fix in the sources for the [&#8230;]</p>
The post <a href="https://learncgames.com/and-the-bug-is-fixed/">And the bug is fixed</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img loading="lazy" decoding="async" class="size-full wp-image-321 alignleft" src="https://learncgames.com/wp-content/uploads/2020/03/bug-tracking.png" alt="Showing the comparison of two sources and tracking a bug down" width="500" height="116" srcset="https://learncgames.com/wp-content/uploads/2020/03/bug-tracking.png 500w, https://learncgames.com/wp-content/uploads/2020/03/bug-tracking-300x70.png 300w" sizes="auto, (max-width: 500px) 100vw, 500px" />How did I fix it? Did I spend hours debugging, poring over every line? No. I woke up in the middle of the night and thought about it and remembered that</p>
<p>I&#8217;d had a similar bug in this chapter when working on the original Ebook.</p>
<p>Stupidly I&#8217;d put the fix in the sources for the next chapter (44) but hadn&#8217;t applied it to the sources in the chapter where it had occurred (42). So I fired up my favourite code comparison tool (Devart Code Compare) and looked for differences between the source files for each chapter.</p>
<p>This was in the function AddObjectToCells and is part of the collision detection. It&#8217;s when an asteroid or player ship is partly on screen and calculating the cell coordinates that the object lies over. The fix was just adding these two lines so it doesn&#8217;t try and access the cells array with negative indices.</p>
<pre><code class="language-c" data-line="">if (cellx &lt; 0) cellx = 0;
if (celly &lt; 0) celly = 0;</code></pre>The post <a href="https://learncgames.com/and-the-bug-is-fixed/">And the bug is fixed</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">320</post-id>	</item>
		<item>
		<title>Bugs and crashes!</title>
		<link>https://learncgames.com/bugs-and-crashes/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bugs-and-crashes</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sat, 04 Apr 2020 23:00:27 +0000</pubDate>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[bugs]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=311</guid>

					<description><![CDATA[<p>I&#8217;m up to chapter 42 in the Linux e-book. The rest have gone very smoothly though I&#8217;m still unable to debug either on my Hyper-V Ubuntu or my Ubuntu running laptop. Both still have the same problem XDG_RUNTIME_DIR not set in the environment. I&#8217;ve been reading up on that and it seems if you switch [&#8230;]</p>
The post <a href="https://learncgames.com/bugs-and-crashes/">Bugs and crashes!</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:786,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/ROverhate-1759589\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1142373&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:787,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1142373&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:14,&quot;href&quot;:&quot;https:\/\/github.com\/David-H-Bolton\/LearnCOnLinux&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20250213114627\/https:\/\/github.com\/David-H-Bolton\/LearnCOnLinux&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 10:35:12&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-09 16:40:14&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 18:24:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-15 22:16:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-18 22:19:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-21 22:22:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-25 04:17:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 04:17:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-03 04:22:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-06 04:38:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-09 08:48:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 09:17:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-15 10:18:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-18 12:43:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 15:14:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-24 15:19:12&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-27 16:38:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-30 19:05:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-02 19:29:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-05 21:51:33&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-08 22:18:13&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-11 22:18:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-14 22:20:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-18 07:27:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-21 10:16:46&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-21 10:16:46&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_312" aria-describedby="caption-attachment-312" style="width: 300px" class="wp-caption alignleft"><img loading="lazy" decoding="async" class="size-medium wp-image-312" src="https://learncgames.com/wp-content/uploads/2020/03/beetle-1142373_640-300x200.jpg" alt="A bug!" width="300" height="200" srcset="https://learncgames.com/wp-content/uploads/2020/03/beetle-1142373_640-300x200.jpg 300w, https://learncgames.com/wp-content/uploads/2020/03/beetle-1142373_640.jpg 640w" sizes="auto, (max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-312" class="wp-caption-text">Image by <a href="https://pixabay.com/users/ROverhate-1759589/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1142373">Ronny Overhate</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1142373">Pixabay</a></figcaption></figure>
<p>I&#8217;m up to chapter 42 in the Linux e-book. The rest have gone very smoothly though I&#8217;m still unable to debug either on my Hyper-V Ubuntu or my Ubuntu running laptop. Both still have the same problem XDG_RUNTIME_DIR not set in the environment.</p>
<p>I&#8217;ve been reading up on that and it seems if you switch into root and the value of the environment variable $XAUTHORITY is blank then that is the problem.</p>
<p>My error logging is picking up problems with sound channels not playing. I think the problem is, when I hit the b key to make lots of asteroids explode, it uses up all available sound channels and there&#8217;s still explosions happening.  Not really a problem.</p>
<p>But to cap it all, there is a segmentation error happening with a core dump. A segfault which kills the program. But it&#8217;s not related to the sound problem. I&#8217;ve proved that by commenting out the entire PlayASound function. It now looks like this which I call the sound of silence.</p>
<pre><code class="language-c" data-line="">void PlayASound(int soundindex) {
	//int success=Mix_PlayChannel(-1, sounds[soundindex], 0);
	//if (success == -1) {
	//	LogError2(&quot;Mix_PlayChannel failed to play audio sound #&quot;,SDL_ltoa(soundindex,buffer,10));
	//}
}</code></pre>
<p>So without being able to run the program in a debugger, I&#8217;m forced to start putting in lots of logging calls and figure out where the segfault happens that way. It&#8217;s an effective way of finding bugs BUT it&#8217;s not the fastest! I have also enabled apport to try and get the crash files created in /var/crash so that may be a faster way forward.</p>
<p>Once I have fixed the segmentation fault, I&#8217;ll upload the fixed code to <a href="https://github.com/David-H-Bolton/LearnCOnLinux" target="_blank" rel="noopener noreferrer">Github</a>. It&#8217;ll be somewhere in the new code added between chapter 37 and chapter 42, to do with collision detection. Time to put on a deerstalker hat!</p>The post <a href="https://learncgames.com/bugs-and-crashes/">Bugs and crashes!</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">311</post-id>	</item>
	</channel>
</rss>
