1. Home
  2. Computing & Technology
  3. C / C++ / C#
photo of David Bolton

David's C / C++ / C# Blog

By David Bolton, About.com Guide to C / C++ / C#

A Silly C# Programming Puzzle!

Wednesday August 20, 2008
August is regarded as the silly season in the UK so here is a silly programming puzzle. What does this short bit of C# code do? Will it compile and/or run? Can you debug it?

# define i
using System;
using System.Text;

namespace ex11
{
    class Program
    {
        static void Main(string[] args)
        {

           int i = 8;
        # line hidden
            ++i;
        # line 5
        # pragma disable
        # warning reverse execution is noitucexe
        # line 4
        # line 12
            System.Console.WriteLine("i = {0}", i) ;
        }
    }
}

Answer tomorrow!

MyGeneration - Open Source C# O/R Mapping

Tuesday August 19, 2008
I really love open source (yes I know that the iPhone and open source are incompatible- sad really) and some of the projects around are quite remarkable. I've been looking at MyGeneration which is a C# open source Code Generator and O/R (Object Relational) Mapper. This is working with data held in databases with an object wrapper around them. The idea is that the relational database tables are not accessed directly or through SQL but instead mapped onto classes which simplify the use of the database.

The code generator takes the tables for a wide variety of databases (12!) and produces the class code. It's template driven (not C++ type templates) so the code generator can output code for JScript, VBScript, C# or VB.NET. It's worth a look not just because it's open source but because of the architecture. This is what the C# looks like for accessing an employee record. (This code example is similar to one on the MyGeneration website).

Employees emps = new Employees() ;
if(emps.LoadByPrimaryKey(31))
{
    emps.LastName = "Bowman";
    emps.Save() ;
}

// Add one new record for Mr Archer
Employees emps = new Employees() ;
emps.AddNew() ;
emps.FirstName = "Mr.";
emps.LastName = "Archer";
emps.Save() ;

// get the last record index
int i = emps.EmployeeID;

Not a sql statement in sight! You'll find mygeneration here and the code on sourceforge. Note, you'll need another sourceforge project- the free 7z archive utility to extract the source code files.

Boost 1.36.0 released - With Four New Libraries

Monday August 18, 2008
Boost is the C++ next generation STL and it's almost six months since 1.35.0 was released on Boost's 10th Anniversary (or pretty near). Boost is a collection of high quality peer-reviewed portable C++ source libraries that work with the C++ Standard Library. It's in a state of continual development and his release has many updates as well as four new libraries for statistical accumulation, transporting data between exceptions and threads, dimensional analysis conversion and unordered associative containers.

The quality of Boost software is kept deliberately high; Ten Boost Libraries are included in the C++ Standard Library's TR1 (The next version) and getting your code accepted into Boost is by no means easy. If you want to become a better C++ programmer, develop an understanding of how and where you might use some of the Boost libraries. It's free with full source code and an extensive list of libraries. Highly recommended.

Programming Challenge 17 - Implement the Cellular Automaton known as Life.

Sunday August 17, 2008
Life - Cellular AutomatonThis isn't due until the end of October but as I thought of it the other day, I thought it best created before I forgot about it. Nearly 40 years ago a British mathematician John Horton Conway devised three simple rules which defined a cellular automaton that he called Life. It runs on a 2d grid with each point being clear or set. The rules determine what points are cleared (die) or are set (born) according to three simple rules.
  1. A set point surrounded by 2 or 3 other set points survives to the next generation.
  2. A set point dies if there are less than 2 other set points or more than 3 around it.
  3. A clear point becomes set if there are exactly three set points around it.
It's quite fascinating seeing the patterns arise before your eyes as each generation is processed. Some shapes (gliders) propel themselves across the grid as the animated image hopefully demonstrates. If not then you can view the original on Wikipedia.

Implementing it so that it runs fast is an interesting challenge. So this challenge is to create code that will implement Life according to those three rules and process a long lived shape of just five dots for 1,000 generations as quickly as possible. By the way, Challenge 15 hasn't had any entries yet!

Buying an iPhone and learning Objective-C/C++

Saturday August 16, 2008
Apple iPhone App StoreI've made the decision to not only buy a 3G iPhone, but a Mac Mini as well so I can try my hand at developing for it. Why? Well I'd been considering getting an iPhone for a while and 8 of my colleagues at work (out of 27) bought one last month. Two of them have even started developing apps for it. In July 2008 the App Store opened and took about $1 million each day of which it passed 70% back to the developers. Many apps are provided free with others ranging from 1$ to $10 or more. It's a bit of a gold rush for developers!

There are non-Apple iPhone development tools and you can compile Objective C/Objective C++ with the GNU Compiler Collection (aka GCC) but I suspect (tell me If I'm wrong) that that restricts you to 'Jail Break' applications. These are unapproved applications which only run on an unlocked iPhone.

There is a cat and mouse game between Apple, because rightly or wrongly Apple insists on controlling applications that run on an iPhone. You have to pay $99 to join and Apple has to approve your application but so long as you play nice by their rules I gather this is a formality.

One aspect of Apple that many iPhone developers hate is the NDA. It restricts what people can publish and many are hoping it will be eventually waived. So even if I wanted to (and you wanted me to), I'm not sure just yet how much I can publish about it though I can certainly add Objective C/C++ if you are interested in it. So tell me!

Have You got a Cuda System?

Friday August 15, 2008
I know that it sounds like I've got a cold. Back in May I mentioned about CUDA which is a free download from Nvidia- no point unless you've got an 8 series or later eg GT200 Nvidia graphics card! )for Windows, Linux and Mac). It includes the nvcc Compiler, a Profiler, a gdb debugger for the GPU (alpha available in March, 2008), a CUDA runtime driver (now also available in the standard NVIDIA GPU driver) and the CUDA programming manual.

The whole point of CUDA is to turbo charge your time critical or CPU intensive applications by offloading the number crunching onto the GPU in your graphics card which uses highly parallel operations to do crunch 10 or even 100 times faster than the CPU. Nvidia have been pushing this and you can now find a whole series of free video casts from the University of Illinois as well as other technical resources on the Nvidia site.

The Disruptive Nature of Open Source- Asterisk

Thursday August 14, 2008
You would think that private branch exchange (PBX) software to manage telephones which many companies use, is not the type of thing that would make a success in open source.. Yet since 1999, Asterisk - open source PBX software has grown in popularity and has even got an O'Reilly book out about it.

What would appear to be something more in the domain of big telephone companies has become a software success story. It is a world leader when used as a switch (PBX), gateway, replacement for old PSTN systems letting calls be connected over IP, normal telephone cable and digital connections. Even call center systems have been built around it. The fact that it runs on GNU/Linux for various hardware platforms and is also part of Debian probably helps. As with much Linux software it's written in C and has a very large number of features, particularly with VOIP where no additional hardware is needed. A permanent link has been added in the C Code library.

Answer to C Programming Puzzle

Wednesday August 13, 2008
That was too easy perhaps (malempati sussed what it did) though no one actually gave me the answer I was looking for. It's a recursive program as best I can make out and I had to set the stack size to a very large number (5 MB) on windows to stop it crashing.

If you want to see more examples of obfuscated code (I know I don't!) take a look at the past entries to the IOCCC website. This is the home of the International Obfuscated C Code Contest. If anyone want to send in their own original obfuscated code I'll put them on a special page in the C (or C++) code library.

Microsoft Releases SP1 for .NET/Visual Studio 2008

Tuesday August 12, 2008
Nine months after the original release Microsoft have brought out service packs for the .NET framework and for Visual Studio 2008. The framework update is not just for 3.5 but includes SP2 for .NET 2.0 and .NET 3.0 SP2 as well. There is also a vastly reduced .NET Client framework with the bits that clients use and a streamlined installation for client applications as the framework is reduced down from 197MB to 26.5MB.

I installed the Visual Studio 2008 SP1 update which was just a 0.6MB download followed by a much larger one and pulled down the .NET SP1 update as well. That took 6 minutes (fast Internet helps!) but the installation part took about 15 minutes. You can read what's new in the .NET Framework 3.5 SP1 as well as download it. Note that if you have installed any related beta products then you may have to remove them. there is a full download link near the bottom of the page. There are two download links; one for just the .NET Framework SP1, or one for Visual Studio 2008 SP1 which includes the .NET Framework.

An Interesting C Program and Puzzle

Monday August 11, 2008
I'm not sure if this will correctly format so I've also provided it as a download. It's a great example of how not to write a program and I found it on wikipedia.

#include <stdio.h>
_(__,___,____){___/__<=1?_(__,___+1,____):!(___%__)?_(__,___+1,0):___%__==___/
__&&!____?(printf("%d\t",___/__),_(__,___+1,0)):___%__>1&&___%__<___/__?_(__,1+
___,____+!(___/__%(___%__))):___<__*__?_(__,___+1,____):0;}main(){_(100,0,0);
}

It should compile (really!) and run but you may find it crashes. So the puzzle is, what do you need to do to make it run. Here's a big clue. Don't change any of the source code! You can search wikipedia but that won't tell you what's wrong... Answer on Wednesday. Have fun!

Read Archives

Explore C / C++ / C#

More from About.com

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2008 About.com, a part of The New York Times Company.

All rights reserved.