Author: David

I’m back and so is my PC

I’m back and so is my PC

H60 PC TemperatureI’m not the greatest with hardware, preferring to deal with software but sometimes you have to get your hands dirty, so here is the saga of my broken PC cooler and how I fixed it.

Three weeks ago my PC broke. I’d had it five and a half years and the cooler (it had a five years warranty! ) broke. It was a liquid cooler and following the advice of the supplier I bought a Corsair H60 cooler which was the same brand but slightly different cooler to mine. So I carefully photographed the orientation of the broken cooler and the connectors before removing it. I was determined to do things right so I bought earthing straps, to prevent static electricity from frying anything it shouldn’t.

So I took out the old cooler, removed the dregs of the thermal paste from the base plate (using Q-tips- works very well!)  and carefully put the new cooler in, and also swapped the fan which cools the radiator. It looks like a car radiator and has the same function. Two thick pipes run from the cooler which sits on top of the CPU and run to the radiator. It’s a closed system so you never see the cooling liquid.

I hooked up the fan connector – no problem. I hooked up the cooler connector and found a 15 pin connector there as well. That wasn’t in the old cooler!  It was a 15 pin SATA power connector. Following more advice from the supplier I bought a 15 pin SATA to Molex lead. Molex are those four pin power connectors that you plug into external hard disks. My PSU (a Corsair CS750M) came with a bunch of cables and one of them was a PSU SATA to Molex connector. One side of the PSU is full of connector sockets- apparently it can power up to 8 SATA devices. So that was the cooler wired up.

So I switched it on, it started booting and then wham- CPU fan error beep beep beep. Now the H60 cooler fan connector only had one wire going into it compared to the three wires on the older cooler. On that older cooler the power came through those wires but the H60 was powered by the SATA cable. So I wondered if maybe this connector belonged elsewhere and studied the motherboard diagram. The Motherboard was an ASUS X99-S.

It turned out that there were several connectors. I had been plugging into CPU FAN1 which was able to drive a fan with over 1 Amp of power. There was another connector CPU FAN which couldn’t supply that much power, so that’s why the old cooler was plugged into CPU FAN1 as that powered it. I plugged my cooler into CPU FAN and it worked. Time wise my PC was out of action for three weeks given delays from emails plus buying the cooler (£64) and the 15 pin-to Molex cable. (£7.00 for three).

I’ve had my PC running for the last half hour and it’s showing the temperature figures shown in the screenshot. It helped that the cooler could use the same plate and screws as the old cooler and came with thermal paste already applied. Until I did this, I hadn’t even realised that my PC was liquid cooled. I’m always nervous as hell when doing things with hardware but feeling quite chuffed that it worked.

The free temperature utility came from here and is very good.

 

 

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!

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