Monday 30 October 2017

C Programming – Arrays and Pointers

One of the things I’ve discovered, through teaching C programming, is that many students find it extremely hard to figure out how arrays work. In most modern programming languages, arrays seem to be simple lists of items like a virtual filing cabinet with numbered drawers for storing things. But that’s quite a ‘high-level’ abstraction of an array. C doesn’t do arrays like that.

In C, you are forced to deal with the way the computer actually stores these items in memory. And that’s when you discover the surprising fact that an array – or a string – is an address. That’s right. It is a memory location. What? But how can that be…?

Let me explain. The address of the array is the same as the address of the first item in the array. To understand this, let’s look C strings. C does not have a dedicated string data type. In C, a string is an array of characters that are terminated by a null character ‘\0’.

Consider this short program:

#include <stdio.h>

int main(int argc, char **argv) {
char str1[] = "Hello world";
printf("%s %c %d %d %d\n", str1, str1[0], &str1, &str1[0], str1);
return 0;

First it displays string – that is, the array of characters – of the variable str1, then the character at index 0, that is, at the first index of the array which here is H of ‘Hello world’. Then it shows the address of the array, which is this number representing a memory location (here, I am using %d in the format string to show it as a decimal value, but many compilers prefer you to use %p to show a hexadecimal value). Now I get the address of the first character. Remember that I said that the address of the array is the same as the address of the first item in the array. Well, if you run this program you will see that is true because the address of the character ‘H’ (which is shown when I use the ampersand ‘address-of’ operator) is the same as the address of the string, shown when I use the address-of operator with the string variable, str1. But look at this last value here. Instead of using the ampersand to get an address, I just display the variable itself, str1, as an integer using %d in my format string. And this shows the same number – the address of the array.

The address of an array is the same as the address of the first item in the array. Because it’s where the array begins. And the name of the array – that is the name you give to the array variable – is also the address of the array.

In other words, we may tend to think of arrays as fixed-length lists. And in many other programming languages, that may be all you need to know about arrays. But, in fact, an array is really the same as an address in memory that defines the beginning of a list of data items. So when you deal with arrays, including strings, you are dealing with addresses.

The video below is taken from my online course, Advanced C Programming: Pointers.

Save 63% by clicking this link to subscribe to the course for just $35 (regular price is $95):

Sunday 22 October 2017

Clearing the Junk out of Windows

How my disk defragmenter led me to find and delete millions of unwanted files cluttering up my hard disk!

I’ve spent the last three weeks defragmenting my hard disk. Yes, really!  And along the way I’ve discovered that I had several million useless files clogging up the disk in hard-to-find, difficult-to-delete subdirectories.

It all started when I decided to move all the programs and files I use most often onto a new PC and leave my old PC free for testing and reviewing software. It seemed to go pretty well until I decided to defragment my old PC’s hard disk. The PC’s startup time had been getting slower and slower over the years. Defragmentation was just one of the measures I took to speed it up. I first tried using the standard Windows defragmenter (right-click the disk drive in Windows Explorer, then select Properties/Tools/Defragment now). That was not only horribly slow but, to make matters worse, it provided very little feedback to show what, if anything, was being defragmented.

Windows Defragmenter - it does the job but is far from informative!

So I tried out Piriform’s free Defraggler instead. This wasn’t exactly quick either but at least it showed me what it was doing. It displays a colour-coded representation of your disk, showing fragmented files, unfragmented files, when data is being read and when data is being written. It also shows a list of fragmented files and you can optionally defragment selected files. The only trouble was that Defraggler got stuck in the initial analysis stage of defragmentation. It showed it had analysed 100% of my disk and then just stayed there, constantly busy, but continuing to show 100%. I suspected that it was stuck analysing a specific set of problematic files, but I didn’t know which ones. Clearly I needed to know which files were taking up most of my disk space.

Defraggler - a free tool that shows you what it's doing!
The rather good free disk mapping too, TreeSize, helped out here.  This draws a map of your disk sorted by the directories containing either the largest amount of data or the largest number of files. It took quite a while to finish and I was prompted a few times to ask if I wanted to abort. But I stuck with it and eventually it showed me exactly what was causing all my problems.

TreeSize - a very useful disk-mapping utility
The folder c:\Windows\sysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5 contained over 11,000,000 files (yes, I really do mean eleven million!) while c:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5 contained another three million. Where the heck did they come from?

It took me a while to figure it out. Then I came across this article written by someone who had experienced a similar problem: That jogged a memory. Years ago – many, many years ago – I has used the Fusion Assembly Binding Log viewer (Fuslogvw.exe) to help the debugging of some software my company was then developing.  This tool keeps track of DLL loading and it writers a tiny HTML file to store each bit of information. This article explains this:

I’d forgotten all about Fuslogvw. And over the years it had been busily writing files, millions of files, that I neither needed nor even knew were there. To stop it doing so, I just went into the Registry Editor and disabled it. This explains how to do that:

OK, so maybe my problem is pretty obscure. But it illustrates a more general problem when doing disk intensive operations such as backups and defrags. Namely: the more files you have, the longer the backup or defrag takes. And millions of files, as I had, can really slow things down. The obvious solution is to delete them. But how?

Using Windows Explorer to delete this number of files is incredibly slow. Besides which, in special Windows system directories you may need Admin privileges. The solution then is to load a command prompt (Windows Menu/All Programs/Accessories/Command Prompt) – but don’t just click Command Prompt. Right-click it and select ‘Run as administrator’. Then navigate to the directory containing all the unwanted files and enter del *.*

Urgh! But deleting files this way is incredibly slow and it gives you no information on whether or not any files are actually being deleted. My solution to that was to write a simple batch file that deleted all the files ending with .htm alphabetically (first delete those starting with ‘A’, then ‘B’, all the way up to ‘Z’, then all those starting with ‘0’ up to ‘9’…) and showed me the times when each group of files had been deleted. That didn’t speed up the file deletion but at least it gave me peace of mind to know that something really was happening. Here’s a short fragment of that batch file to show what I mean:

time /t
del A*.htm
time /t
del B*.htm
time /t
del C*.htm
time /t

Anyway, it took me about three days (night and day) to delete those files. Once I’d done that I ran Defraggler again and this time it got on with the job pretty effectively. Along the way, I discovered a whole load of other directories containing unneeded junk. For example, some programs which I had long ago uninstalled, had left all kinds of stuff under \ProgramData. I duly removed  them. I’m not providing any details, however, since some apparently unneeded files under \ProgramData and other ‘system’ folders may be important and I don’t want to be blamed for encouraging you to delete anything that may cause problems. When you go hacking around in these directories you are on your own! But if, like me, you are frustrated by the slow speed of your defragging or disk backup programs, you may want to take a look, using TreeSize, to see if your disk space is being taken up by junk you didn’t know about. You’ll almost certainly find that it is!

Thursday 5 October 2017

VEGAS Pro 15 Review

VEGAS Pro 15 Edit – € 399 / $399 / £299
VEGAS Pro 15 – € 599 / $599 / £499
VEGAS Pro 15 Suite – € 799 / $799 / £649

If you’ve used VEGAS before, the first thing you’ll notice when you load the new version is that it looks nicer. I’ve criticized VEGAS before for its rather inelegant and screen-hogging user interface so I am pleased to see that version 15 gets rid of some of the unnecessary clutter.

For a general overview of VEGAS see my previous review of VEGAS 14.

The user interface has redesigned icons, full-colour timeline tracks and the addition of four switchable colour schemes. As in previous releases, you can tailor some elements to taste – for example, by selecting alternative track colours. A new feature lets you switch the overall colour scheme of the entire user interface from a choice of four: Dark, Medium, Light or White. However, there is no way of previewing the colour-scheme change when you make it. Any change you make is only applied after you restart VEAGAS which, let’s be frank, is hardly the final word in user-friendliness!

In an attempt to reduce clutter, some new context-sensitive menu configuration tools (called, ‘hamburger’ buttons) have been added. These buttons are shown as three horizontal lines which appear on various UI elements such as the video preview window, the track panel and also in the top right-hand corner of individual video and audio clips. When you click the hamburger button a drop-down menu appears from which you can select various options. For instance, in the video preview window the hamburger menu shows items to go to the next or previous frame, to the start or to the end. In the track pane, it pops down a cascading menu to let you track motion, bypass motion blur, select a compositing mode and various other arcane options. If you decide that you need these features frequently and want a faster way of getting at them you can add them as icons to the window or tool itself.

Hamburger buttons have been around for a while – they are often used in mobile apps and web sites to hide functionality and keep the interface ‘clean’. Some users hate them. I have to say, though, that the way they are used in VEGAS (to hide less frequently used options or configure the selected track or window) they seem to me to be a reasonable way of keeping the user interface under control.

Notice the ‘hamburger’ buttons that provide menus from both the preview window and individual media clips
You can also rearrange the windows and the pages on tabbed panes by dragging them and dropping them into selected areas of the workspace. To redock an undocked pane you have to hold CTRL while dragging. Once you’ve created a layout you like you can save it by name or you can select from one of the redefined layouts – this is just as well as I found that it takes quite a bit of trial and error to redock tabbed panes in exactly the place you want (I kept accidentally messing up the layout in my attempts) once they’ve been undocked!

But never mind the appearance, what about new and improved functionality? Top of my personal list of desirable features would be increased rendering speed, so I was pleased to see that this is one of VEGAS 15’s claimed improvements.

There is no simple way to benchmark the speed of video rendering. The size and format of the project media, the overhead due to any editing and effects that have been added, plus the rendering varying efficiency of different production settings and the specifics of your computer hardware setup all have an impact. Even so, I decided to run a very simple test to get some idea of the speed of video rendering.

I ran this test on a PC with 16Gb of system memory and a NVIDIA GTX 980Ti graphics card with 6 Gb of video memory. I tried rendering a single 5 minute 25fps 1920x1080 video clip, with no editing applied. I enabled the NVIDIA GTX GPU acceleration using the VEGAS Preferences dialog. In VEGAS 14 I rendered as Mainconcept AVC/AAC H.264 MP4 at the original video size (1080p). It took 8 minutes and 52 seconds to render.  With VEGAS 15 the same clip was rendered with MAGIX AVC/AAC MP4 (the replacement for the Mainconcept format). It took 8 minutes and 10 seconds to render. So, in short, not much difference. For the sake of comparison, I rendered the exact same clip in Cyberlink PowerDirector 15 (AVC H.264 MP4, 25fps, 1080p with its hardware video encoder enabled) and the entire video was rendered in 31 seconds. Yes, really! More than 7 and a half minutes faster than VEGAS 15.

You may need to go and take a break while your videos are rendered
Now, as I say, rendering results vary according to numerous different factors and it may be that for certain projects, with certain hardware and rendering settings, VEGAS can be made to render more efficiently. There is, at any rate, a long discussion thread about this here:

All I can say for sure is that, in long experience of video editing and producing, Cyberlink PowerDirector has by far the fastest video rendering of any program I’ve ever used. VEGAS 15 doesn’t, as far as I can see, offer any real challenge in this respect.

One thing I do like is the new interactive sizing and cropping of video clips. Both Picture-in-Picture and Crop editing have been improved so that you can, for example, modify the size and position of a video clip that overlays another clip. For instance, if you have a video clip showing some scenery or a tutorial of some sort and you want to overlay a ‘talking head’ clip, you just drop the overlaid clip onto a new track and apply the ‘picture in picture’ plugin to it. This now lets you move and resize the overlaid clip right inside the video preview window. You can, of course, achieve the same results by panning and cropping the overlaid clip as in earlier VEGAS releases. But traditional VEGAS pan/crop has to be applied in a popup dialog so the new interactive PIP tools add an extra level of convenience. The Crop plugin works in a similar way. You start by dropping Crop from a list of plugins onto a video track. In order to do the actual cropping you have to pop up the Crop dialog box and then you can either make adjustments using sliders or you can drag the edges of the video clip in the preview window.

If you add a video overlay, you can use the Picture In Picture plugin to alter its size, position and rotation either using a dialog box or by dragging and dropping the overlay using the mouse
There are numerous other enhancements to tools throughout this release. These include the ability to selectively paste event attributes’ (such as an audio pitch shift, transitions or pans and crops) from one clip to another. And you can automate video rendering and uploading to YouTube, Facebook or Vimeo.

There are improvements to colour grading using lookup tables and enhanced titling capabilities. An expanded range of 3rd party plugins has been provided (for the Pro and Pro Suite editions). These include plugins some of the best known suppliers – BorisFX, HitFilem and NewBlueFX. You can find the full range of improvements and additions listed online along with a number of short video demos and tutorials:

In summary, this release of VEGAS has some useful improvements both to functionality and interactivity. But it has both the strengths and the weaknesses of earlier versions. Its ability to chain together multiple effects, for example, and tweak individual parameters in the ‘effects chain’ provides a high level of editing control but the downside is that this means that you have to do a lot of work in popup dialog boxes which can be quite fiddly and time-consuming. And its video rendering speed may have been improved a bit but it is still far from being a speed-demon. So if you need precise editing control and don’t mind the extra time it takes to edit and render, VEGAS has a lot to offer. But if you just want to get a whole load of videos edited and uploaded as rapidly as possible, VEGAS may not be the best choice.