Thomas Sampson


Dynamic Arrays in C++

Update: After looking back at this post I realised that the title “Dynamic Arrays in C++” is pretty misleading, I think this was basically way back when I figured out how do do memory allocation! For anyone looking to do real dynamic arrays / collections, consider the std::vector class or use a linked list.

C++I have only started using C++ properly over the past few months and I’m realising that many of the features and aspects I took for granted in interpreted languages, are implemented in a much more complicated way in c++!

For example if you wanted to create a dynamic array in c# (as below) the .NET framework will do all the memory allocation and management for you and let you initialise an array of your chosen size at run time (when the size of the array cannot be determined at compile time)

int[] temp = new int[varName] //Where varName could be an integer given by the user

Another interesting point at this stage is that after c# initialises the array dynamicaly, for example with a dimension of 3. When you try and access temp[20] for example, the .net framework instantly detects you are breaching the size of the array and throws an “Out of bounds” exception. However in c++ (and this applies to static and dynamic arrays) accesing a position outside the array simply looks up in memory PAST the array and starts reading random data, not even necessarily from your application!

Anyway, to make a dynamic array in c++ we must follow 3 steps

  1. use the new keyword to initialise the array in memory
  2. ensure that the memory allocation for the array was successfull, catching the failure of this event
  3. delete the array from memory after use (as c++ will not dispose of it automatically)

The example below simply gets a number from the user and then initialises an array with the dimension the user has specified.

int mySize( 0);
cin >> mySize;
int* myArray = new int[mySize];
  //catch failure of array initialisation
  //Work with array....
  delete[] myArray;

Reflective Glass

I am aware that there is most likely to be a much simpler implementation of this as part of WPF (Windows Presentation Foundation) but this article shows you how to make a reflective glass effect (similar to that used throughout itunes) using GDI library only (in c#).

Leave a comment

Avioding SQL injection

After much playing around and problems I finally figured how to add safe parameters into an odbc command. Here is a sample….

OdbcCommand postcomment = new OdbcCommand(“insert into comments values (‘”+newguid+”‘,'”+itemid+”‘,?)”, temp);

Here the ? represents the first and only parameter to be passed into the command using..

postcomment.Parameters.AddWithValue(“@comment”, comment);



If you were to include > 1 ? symbols, the parameters must be added in the order they appear in your original command.

Leave a comment

XML Parsing

Today i started work on a desktop cleaning / sorting application called DeskTidy. I realised that I wanted to store a list of user defined filters for automatically tidying the desktop (based on filesize, type, age etc etc). The best way to store such information seems to be an XML document, so after research I found this document showing how to read an XML document from inside C# .NET, making use of the XMLTextReader object.

This should come in handy for anyone wanting to read an XML document from C#. The object loops through nodes inside the xml and picks out variables. I think it can also cache and parse remote XML files from a URL (anyone making yet another rss reader?), not something I have tried though. In addition to the XMLReader object, the object XMLWriter exists to do pretty much the opposite. Here is some msdn documentation for the class

MSDN Article

Hope this is useful!