Month: March 2021

Another maze generator and solver in C

Another maze generator and solver in C

Solved mazeI liked this one; it compiled perfectly without any changes and ran perfectly. It produces a maze of the specified size with a route. That’s not bad for a program written over 20 years ago. By developer Joe Wingbermuehle. You can view the source code here.

It runs in a terminal, just supply width and height characters like this. I compiled it into a file ex1.

./ex1 15 15 s

If you provide the s parameter, it will solve it as the screenshot shows using <> for the solved route. off for just the maze.

How to encrypt text using Xor

How to encrypt text using Xor

Binary
Image by Gerd Altmann from Pixabay

This is not meant to offer protection, but if you want to say hide text by disguising it, then using Xor for reversible encryption will do the trick. It relies on the principle that if you Xor A and B to get C then you can Xor C and A to get B or Xor C and B to get A.

I wrote a short program and tutorials to demonstrate taking a single bit of text then disguising it. To make it more challenging, I only used Xor values from the range 0-255 that had four or more bits with 1 in it, for example 15 which is 00001111 in binary.

You can find the tutorial How to do Xor encryption in C. Please note this is only a very light weight encryption method so don’t use it for anything too important!

How to extend C (99) with a library

How to extend C (99) with a library

toolkitI’m always looking to improve my C code and one way to do this is through others efforts. Today I came across Zpl, a cross-platform header-only library.

The zpl.h file is a whopping 17,495 lines long!  It has code for macro helpers,  memory, collections, string, hashtable, file, memory streamer, print, time, random, sorting and miscellaneous.

Given the length, it would be difficult to make sense but the authors (Vladyslav Hrytsenko and Dominik Madarász from the Ukraine and Slovakia respectively) have provided a folder of example applications that use the library.

It looks a very impressive library and well worth a look.

Did you know? Visual Studio is 32-bit only!

Did you know? Visual Studio is 32-bit only!

Update: This is now obsolete as Visual Studio 2022 which appeared in November 2021 is 64-bit.  D’oh!

ITask Manager screenshott’s quite surprising that in this day and age, that there is still 32-bit software in use. Visual Studio is a prime example.   Windows has been 64-bit for quite a while now.

If you look in Task Manager, you’ll probably notice that programs with a (32 bit) after their name are few and far between.

On the screenshot, only one out of 11 is 32-bit and that’s pretty typical.  Linux and Mac are probably similar.  Here’s a stackexchange question on how to tell if a process is 32 or 64 bit.

In Visual Studio, it’s very easy to switch between 32-bit or 64-bit compile target. Unless I have a real need, I go for 32-bit for programs I write because they are typically not going to need over 4 GB of RAM. And 32-bit code usually runs faster than 64-bit because instructions are typically shorter which means more instructions in the execution cache etc.

However some systems have a lot of code and there are people who want a 64-bit version of Visual Studio.  You can read some of their requests here.

I’m still on a Linux laptop BTW. My new cooler has turned up and tomorrow I hope to install it. The Windows screenshot came from my work laptop which I emailed to myself!

Updated, how I track designs

Updated, how I track designs

Dendron logoBack in August I mentioned WikidPad which I had been using for ideas, design notes etc.

Recently though while waiting to get my Windows PC sorted, I’ve been doing nearly everything on an old laptop that I’ve repurposed by installing Ubuntu. All blogs since March 6th have been done on this laptop.

Today I came across Dendron which is a markdown editor (on steroids!) that runs in VS Code. Anyone who reads this blog knows that I’m a big fan of VS Code so it’s a no-brainer to combine the two.

Markdown is a way of annotating text for example *this phrase* will appear in italics. Dendron lets your have a split view so you type in the markdown text on the left and the page appears on the right.

The idea is that you create your document out of lots of pages, that are hyper linked. Dendron makes it easy to create pages and link them. Markdown is used to add formatting. WikidPad used a similar scheme but it was a Windows application holding pages in a SQLite database not an extension to VS Code as Dendron is that holds pages as individual text files.

Dendron appears a lot more powerful so I will be getting to grips with it. It’s a handy way of designing and documenting a design.

 

 

Is variable++ faster than ++variable?

Is variable++ faster than ++variable?

TimingsOne of the things I as told when I learnt C++ and then later C was that a post-inc (i.e. variable++) was faster than a pre-inc i.e. ++variable. Frankly I’m not sure if it is really true but its not a difficult thing to test.

Here’s a short program

#include <stdio.h>
#include "hr_time.h"

#define NUMLOOPS 100000000

int main() {
  stopWatch s;
  startTimer(&s);
  int j=0;
  for (int i=0;i<NUMLOOPS;i++){
    ++j;
  }
  stopTimer(&s);
  printf("PreInc = %10.5f\n",diff(&s));

  startTimer(&s);   
  j=0;
  for (int i=0;i<NUMLOOPS;i++){
    j--;
  }
  stopTimer(&s);
  printf("PostInc = %10.5f\n",diff(&s));  
}

You can get the siurce code including hr_time.h and .c from the timings.zip file on GitHub. I used VS Code with clang to build this on Ubuntu. Here is the tasks.json file to build it. It assumes that the file is in your workspace folder and creates a file called ex1. The timings.zip file contains the json files as well.

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}","${workspaceFolder}/hr_time.c",              
                "-o",
                "${fileDirname}/ex1",                
                "-lm"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Ignore the first three runs which were for 10 million not 100 million. All do indeed show that post-inc is indeed faster. Not by a great margin but each of the last 100 million loops takes between 94% and 96% of the preinc time.

Interesting gcc/clang extensions to C

Interesting gcc/clang extensions to C

C ExampleBoth gcc and clang support extensions to C and while i normally try and make things I write about work on Windows (i.e. Visual Studio), these are useful enough that I thought they deserve a mention. Yes I know you can run gcc/clang on Windows using Cygwin or MinGW, but for various reasons I prefer Visual Studio.

You can add a constructor and destructor functions to a C program; the constructor function runs before main() and the destructor after main().

The syntax is not exactly clean or obvious (those are double underscores before and after the word attribute like Python dunders!) but I got this program to compile/run with clang 10 on Ubuntu as the screenshot shows.  Here’s a listing. I called the two functions ctor and dtor but you can use anything.

#include <stdio.h>

__attribute__((constructor)) void ctor(void)
{
  printf("Constructor runs first\n");
}

__attribute__((destructor)) void dtor(void)
{
  printf("Destructor runs last\n");
}

int main() {
    printf("Main\n");
}

The output  is:

david@DavidPC:~/Projects/Examples$ ./ex1
Constructor runs first
Main
Destructor runs last
Cling – an interactive C++ interpreter

Cling – an interactive C++ interpreter

Computer screen
Image by Markus Spiske from Pixabay

When I first learned to program back in 1976, I had a teletype and a BASIC interpreter. Apart from a couple of years writing BASIC programs in my first job, all my work after that was with compilers.

So I’m a bit rusty with interpreters. The idea is that the interpreter reads a line of code and then executes it; parsing it and calling various routines to execute statements and parse then evaluate expressions. It’s kind if unusual to do this with C++. Cling is built on the top of LLVM and Clang libraries.

This is different to sites like repl.it, codepad.org or ideone.com; they compile the whole program and run it. With an interpreter, it runs line by line and you can print out variables at any time.  Interpreters are probably more like debuggers.

As well as C++, cling can execute C, Objective-C and that even less used language Objective-C++.  Developed at Cern it has a very extensive set of tutorials.

Warzone-2100 C++ cross platform open source

Warzone-2100 C++ cross platform open source

Warzone 2100
Image from Wikimedia

There are some amazing open source games available and Warzone 2100 is definitely one of them. It was originally developed by Pumpkin Studios and published by Eidos Interactive. It was originally released in 1999 on PC and PlayStation.

In late 2004 the source code and most of the data was released and a few years later the rest of it. It is now also available for Windows 7-10, macOS, FreeBSD, AmigaOS, AROS, MorphOS, Linux, NetBSD and OpenBSD. Since then the Warzone 2100 Project has been worked on.

According to the GitHub page “Command the forces of The Project in a battle to rebuild the world after mankind has been nearly destroyed by nuclear missiles. “. You can play it in single-player mode or against other players on a LAN or on the internet.

To get a feel for the game you can view the online guide.

Once you’ve played it, you could consider contributing to the project. It’s an open project and the issue tracker currently shows 213 open issues.