Blog posts for 2009

News and other things I find interesting


RSS feeds and ATOM feeds

RSS Feed ATOM Feeds


Dec
18
2009

One of the joys of Python - Generators

Last modified: Saturday, July 17, 2010

This post will use Fibonacci numbers as a tool to build up to explaining the usefulness of Python generators.

This post will feature both C++ and Python code.

Fibonacci numbers are defined as the sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ....

Or in general:

F0 = 0
F1 = 1
Fn = Fn-1 + Fn-2

This can be transferred into a C++ function extremely easily:

size_t Fib(size_t n)
{ 
    //Fib(0) = 0
    if(n == 0)
        return 0;

    //Fib(1) = 1
    if(n == 1)
        return 1;

    //Fib(N) = Fib(N-2) + Fib(N-1)
    return Fib(n-2) + Fib(n-1);
}

But if you want to print the first 6 Fibonacci numbers, you will be recalculating a lot of the values with the above function.

For example: Fib(3) = Fib(2) + Fib(1), but Fib(2) also recalculates Fib(1). The higher the value you want to calculate, the worse off you will be.

So one may be tempted to re-write the above by keeping track of the state in main.

//Not supported for the first 2 elements of Fib
size_t GetNextFib(size_t &pp, size_t &p)
{
    int result = pp + p;
    pp = p;
    p = result;
    return result;
}

int main(int argc, char *argv[])
{
    size_t pp = 0;
    size_t p = 1;
    std::cout << "0 " << "1 ";
    for(size_t i = 0; i <= 4; ++i)
    {
        size_t fibI = GetNextFib(pp, p);            
        std::cout << fibI << " ";
    }   
return 0;
}

But this is very ugly, and it complicates our logic in main, it would be better to not have to worry about state in our main function.

We could return a vector of values and use an iterator to iterate over that set of values, but this requires a lot of memory all at once for a large number of return values.

So back to our old approach, what happens if we wanted to do something else besides print the numbers? We'd have to copy and paste the whole block of code in main and change the output statements to whatever else we wanted to do. And if you copy and paste code, then you should be shot. You don't want to get shot do you?

To solve these problems, and to avoid getting shot, we may re-write this block of code using a callback function. Every time a new Fibonacci number is encountered, we would call the callback function.

void GetFibNumbers(size_t max, void(*FoundNewFibCallback)(size_t))
{    
    if(max-- == 0) return;        
    FoundNewFibCallback(0);        
    if(max-- == 0) return;
    FoundNewFibCallback(1);

    size_t pp = 0;
    size_t p = 1;
    for(;;)
    {
        if(max-- == 0) return;
        int result = pp + p;
        pp = p;
        p = result;
        FoundNewFibCallback(result);
    }
}

void foundNewFib(size_t fibI)
{
    std::cout << fibI << " ";
}

int main(int argc, char *argv[])
{
    GetFibNumbers(6, foundNewFib);  
    return 0;
}

This is clearly an improvement, your logic in main is not as cluttered, and you can do anything you want with the Fibonacci numbers, simply define new callbacks.

But this is still not perfect. What if you wanted to only get the first 2 Fibonacci numbers, and then do something, then get some more, then do something else.

Well we could go on like we have been, and we could start adding state again into main, allowing GetFibNumbers to start from an arbitrary point. But this will further bloat our code, and it already looks too big for a simple task like printing Fibonacci numbers.

We could implement a producer and consumer model via a couple threads. But this complicates the code even more.

Instead let's talk about generators.

Python has a very nice language feature that solves problems like these called generators.

A generator allows you to execute a function, stop at an arbitrary point, and then continue again where you left off. Each time returning a value.

Consider the following code that uses a generator:

def fib():
    pp, p = 0, 1  
    while 1:
        yield pp
        pp, p = p, pp+p

g = fib()
for i in range(6):
    g.next()

Which gives us the results:

0
1
1
2
3
5

The yield statement is used in conjuction with Python generators. It saves the state of the function and returns the yeilded value. The next time you call the next() function on the generator, it will continue where the yield left off.

This is by far more clean than the callback function code. We have cleaner code, smaller code, and not to mention much more functional code (Python allows arbitrarily large integers).

Tags:

Add a new comment



Dec
12
2009

Forward declaring enums in C++

Last modified: Saturday, July 17, 2010

Forward declaring things in C++ is very useful because it dramatically speeds up compilation time. You can forward declare several things in C++ including: struct, class, function, etc...

But can you forward declare an enum in C++?

No you can't.

But why not allow it? If it were allowed you could define your enum type in your header file, and your enum values in your source file. Sounds like it should be allowed right?

Wrong.

In C++ there is no default type for enum like there is in C# (int). In C++ your enum type will be determined by the compiler to be any type that will fit the range of values you have for your enum.

What does that mean?

It means that your enum's underlying type cannot be fully determined until you have all of the values of the enum defined. Which mans you cannot separate the declaration and definition of your enum. And therefore you cannot forward declare an enum in C++.

The ISO C++ standard S7.2.5:

The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enumeration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to the underlying type.

You can determine the size of an enumerated type in C++ by using the sizeof operator. The size of the enumerated type is the size of its underlying type. In this way you can guess which type your compiler is using for your enum.

What if you specify the type of your enum explicitly like this:

enum Color : char { Red=0, Green=1, Blue=2};
assert(sizeof Color == 1);

Can you then forward declare your enum?

No. But why not?

Specifying the type of an enum is not actually part of the current C++ standard. It is a VC++ extension. It will be part of C++0x though.

Tags:

Add a new comment



Dec
10
2009

Pure virtual function call errors and related behavior

Last modified: Saturday, July 17, 2010

What are abstract functions?

Abstract functions, are functions who's implementation is not yet specified.

They are useful because:

  • They allow you to define an interface without defining an implementation.
  • A base class may not have a specific default definition for a function, but you know that derived types will.

In C++ both interfaces, and abstract classes are done via pure virtual functions. Pure virtual functions simply say that derived types must override the function. The base type can have a default implementation (that the derived types can use by calling the base function directly) but the base functions typically have no implementation at all.

In C# there are different constructs for interfaces (interface) and undefined base functions (abstract).

This post discusses what pure virtual function call errors are, and how they work across the following languages: C++, C#, and Python.

What is a pure virtual function call error?

Pure virtual function call errors could potentially happen, in a programming language that allows you to create partially implemented classes. Although not all programming languages can have pure virtual function call errors.

Pure virtual function call errors occur when a call is made to a pure virtual function. Since an abstract base type cannot be created in most languages, they will typically occur before a derived type is fully created, or after a derived type is already destroyed. The call is therefore usually called from the base type. Pure virtual function call errors could potentially also occur when using a pointer to call a function of an already deleted object.


Can C++ have pure virtual function errors?

Yes.

Consider the order of construction for the following C++ code:

class Animal
{
public:
  virtual ~Animal() {}
  virtual void Speak() = 0;
  Animal() {}
};

class Dog : public Animal
{
public: 
  virtual void Speak() { }
};

//.... 
Dog leia;

When you create an instance of Dog the following happens:

  1. Construct Animal
  2. Construct Dog

When the instance of Dog named leia falls out of scope, the following happens on destruction:

  1. Destruct Dog
  2. Destruct Animal

If you happen to call Speak() in the destructor of Animal, or in the constructor of Animal, then a pure virtual function error will occur. Most C++ compilers will give you a compiling error; however, you can get around this compiling error by calling a function that calls a pure virtual function.

Here is a code sample that will produce a pure virtual function runtime error in g++, Visual Studio 2005, and Visual Studio 2008.

class Animal
{
public:
    virtual ~Animal() {}
    virtual void Speak() = 0;
    void SpeakPlease()
    {
        Speak();
    }
    Animal() 
    {
        SpeakPlease();
    }
};

class Dog : public Animal
{
public: 
    virtual void Speak() { }
};

int main(int argc, char* argv[])
{
    Dog leia;
    return 0;
}

Can C# have pure virtual function errors?

No.

C# allows you to create pure virtual functions by using the abstract keyword on each of your abstract function/methods. And if you have even one abstract function/method in your class you must also use abstract before your class declaration.

C# gets around pure virtual function calls though, but arguably in a worse way.

public abstract class Animal
{
    public Animal()
    {
        Speak();
    }

    ~Animal()
    {
        Speak();
    }

    public abstract void Speak();
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }

    ~Dog()
    {
    }
}

Dog::Speak() will be called in the destructor of Animal even know Dog is already destructed. Obviously this can lead to many problems.


Can Python have pure virtual function errors?

Kind of, and only if you follow certain conventions.

Python can't define abstract functions directly, instead you simply raise an exception of type NotImplemented.

In Python all functions/methods are virtual.

This is to say pure virtual function support is defined in Python simply by convention instead of language constructs.

Therefore unlike C++ and C#, you can create objects of a class that have some of it's functions/methods as abstract. In that sence you can have pure virtual function errors (via NotImplementedError exceptions)

But Python works like C# in the sense that even before the derived type is constructed, it will call into it. The end result is that it throws an exception that can be caught.

class Animal(object):
  def __init__(self):
    print("Constructing animal")    
    self.Speak()
  def Speak(self):
    raise NotImplementedError
  def __del__(self):
    print("Destructing animal")

class Dog(Animal):
  def __init__(self):
    super(Dog, self).__init__()
    print("Constructing Dog")
  def Speak(self):
    print("Woof!")    
  def __del__(self):
    print("Destructing dog")
    super(Dog, self).__del__()

def Test():
  leia = Dog()

Next time you get an error like: "R6025 Pure virtual function call", perhaps you will wonder less about the source of the error.

Tags:

Add a new comment



Nov
12
2009

Arrays are not the same as pointers!

Last modified: Saturday, July 17, 2010

A common mistake people make in C++ is thinking that arrays and pointers are the same thing. They're not.

char *p = "hello";
char q[] = "hello";

These 2 lines are very different.

The first is a pointer to a string literal. The string literal is in read only memory. Changing p[i] for any index i is undefined.

The second is a char array initialized with 'h', 'e', 'l', 'l', 'o', '\0'. Changing q[i] for any index i in the range 0..5 is fine.

Consequently:

assert(sizeof(p) == sizeof(char*)); 
assert(sizeof(q) != sizeof(char*));
assert(sizeof(q) == 6);

Not only are pointers and arrays 2 different things completely, but you can also have pointers to arrays. Most people think that a char* is a pointer to an array. It's not.

char sz[12];

//This is fine, p points to sz's first element's address
char *p1 = sz;

//Compiling error, Can't convert a pointer to 12 elements to a pointer to a char
char *p2 = &sz;

//This is the correct way to create a pointer to an array
char (*x)[12] = &sz;

//Compiling error, can't convert a pointer to 12 elements to a pointer to 10 elements
char (*y)[10] = &sz;

And of course you can also create references to arrays. But the syntax is just as ugly as the syntax for pointers to arrays.

//r is now a reference to sz
char (&r)[12] = sz;

Tags:

Add a new comment



Sep
21
2009

Introducing the rope data structure!

Last modified: Saturday, July 17, 2010

I've long known about the rope data structure, but it hasn't dawned on me until now in what cases you'd actually want to use it. I also didn't take the time to see how a rope was actually implemented until I looked it up tonight.

Here's an explanation:

The problem with a string data structure is that it's very hard to do insertions into the middle of the string. This means that if you are continually inserting text into the middle of your string, then you will need to re-allocate your buffer a lot of times.

Also to append 2 strings you are forced into copying the 2nd string into the first string's buffer. Assuming the first string's buffer is large enough. If the buffer is not large enough, then the first string will need to re-allocate and copy.

If you have a really really large string, let's say 1GB worth of string data, then a simple insertion of a couple words means you need to move the rest of the characters over and then null terminate. This is an O(N) operation, which means you have to process 1GB worth of data even for any insertion into a 1GB string.

A rope is a special case of a binary tree data structure where each leaf node holds a substring (or an array of characters). Nodes that are not leaf nodes do not hold an array of characters.

A left child that is a leaf is the left part of a string. A right child that is a leaf node is the right part of the string. To get the whole string you process the left child then the right child.

You can concatenate 2 ropes easily by creating a common root node and joining them together.

Example:

RootNode = "Hello"

I want to append " World" so first I create a new rope with a single RootNode....

RootNode = " World"

Then I combine both of those ropes:

RootNode:
 - Left Child: "Hello"
 - Right Child: " World"

Now if we want to make an insertion into the middle of the stirng: "Hello Great World" we simply made a new node and join the leaf node "Hello" with this new node " Great".

RootNode:
  LeftChild:
   - LeftChild: "Hello"
   - RightChild: " Great"
  RightChild: " World"

To get the full string you simply process the tree from the root node, following the left subtree first.

Appending becomes an O(1) operation. Indexing becomes O(logn), and printing everything is just as efficient, other than the rope having a constant factor overhead.

If you are implementing your own rope data structure from scratch you can actually take advantage of the non leaf nodes. They can be used for any type of hierarchical data that would apply to all of its children. For example, perhaps special formatting. Getting a whole item's format would simply involve getting the parent of the nodes all the way up to the root.

You could also do things like make each leaf node a single word. Then you could implement a spell check pretty easily without having to process each node to find the distinct words in each node.

Tags:

Add a new comment



Sep
21
2009

2 books I'm reading...

Last modified: Saturday, July 17, 2010

I'm currently reading:

  1. Gray Hat Python: Python Programming for hackers and reverse engineers by Justin Seitz
  2. C# in depth 2nd edition by John Skeet

I'm about half way through both books. They're both great books so far.

Tags:

Add a new comment



Sep
19
2009

How VNC, Fog Creek Copilot and other remote control software works

Last modified: Saturday, July 17, 2010

About 7 years ago my company and I created a program called Remote Task Manager. It was exactly like the Windows Task Manager, but it had an extra left bar which showed all the computers you wanted to control. And it had a button for View This Process.

The button made any process magically show up on your screen remotely.

This program didn't last long though, my company's focus went to backup software and we shelfed the product at a young age. This program was built by taking screenshots and sending them from computer to computer and was built with a custom protocol.

This article investigates more proper ways for implementing remote-control-computer software.

An introduction to some terms:

Images on computers can be categorized into 2 groups:

  1. Vector graphics: Graphic that are built from stored instructions. These stored instructions are used to draw primitives such as points, lines, curves, shapes, polygons.
    Examples of vector graphic file formats include: SVG, Adobe Illustrator files.

  2. Raster graphics: Graphic that are built from a grid of pixels and their color values. Examples include: BMP files, GIF files, JPG files.

Likewise, displays can also be categorized into 2 groups:

  1. Vector graphic displays: Video output display who's source comes from vector graphics.
  2. A framebuffer display: Video output display who's source comes from a memory buffer containing a frame of data. That is to say it stores a raster graphic.

The VNC protocol works for any framebuffer display. That is to say it relates to #2s above, but not the #1s above. That means it applies to just about every operating system whether it is Linux, Solaris, OS X, or Windows.

The remote framebuffer (RFB) protocol is a protocol used for remotely accessing a GUI. The VNC protocol is built from the RFB protocol. The RFB protocol, like most (but not all) other protocols can be divided into a client and server.

The RFB Client:

The computer that wants to control the remote computer is called the RFB Client. Programming an RFB Client is easier relative to programming an RFB server, mostly because the client is stateless.

Having a stateless protocol is an absolute gift from god. It means that when you disconnect, you can reconnect easily, whether on purpose, or by accident, or by hardware/software failure. An example of a completely stateless protocol is HTTP. An example of a completely stateful protocol is FTP.

After setting the frame format that the RFB Client wants, the RFB Client will request updated frames as it wants them from the RFB Server. For every update the RFB Client obtains, it displays it on the screen. The RFB Client does not need to request the entire frame each time. It can request an x,y position and width,height as well.

The RFB Client also sends Input events such as keyboard presses, mouse presses, mouse moves, and more to the RFB Server.

I won't go into much more details about how things work at the RFB protocol level, but if you'd like to know more please read this document.

The RFB Server:

The computer that actually has the framebuffer that you want to see is called the RFB server. Programming this is harder relative to the RFB Client because it needs to 1) manage one or more RFB Clients, 2) respond to input events from the RFB Client, and 3) Provide updated frames to the RFB Client.

Several requests can occur from the RFB Client and the server may decide to simply send only one frame update.

The RFB Server needs to only send incremental updates to the RFB Client, unless the RFB Client specifically sets the Incremental value to false. Typically all requests form the RFB Client will have Incremental set to True. Except of course for the first request and any first request after a reconnect.

Both the RFB Server and RFB Client can also notify each other about cut text, and the RFB Server can notify the RFB Client to ring a bell if it has one.

TCP hole punching:

One thing that the RFB protocol does not address directly is connecting 2 endpoints that are behind a router/NAT (henceforth referred to as a NAT).

Just about everyone on the internet now days has a NAT. A NAT connects multiple computers to 1 Internet connection. That means that each computer behind that NAT has an inside IP address of its own, but they all share the same outside IP address.

The way that the NAT knows which computer to send which data coming in to its IP to, is via a network address translation table.

When a computer behind a NAT initiates a connection to a server and a port, the NAT stores the internal IP and performs the connection for that computer. Any data coming back from that server then gets routed back to the original computer that initiated the connection.

This works nicely if the computer behind the NAT initiates the connection, but what if an outside computer wants to connect to one of your internal computers, and it wants to initiate the connection? It's not possible. The outside computer knows only about your outside IP and knows nothing about internal IPs of the computers behind that external IP.

The way around this problem, if you have the source code to the programs you want to make the connections between, is known as TCP hole punching.

TCP hole punching means that both computers connect to a known server, and then the communication will continue after that between the 2 computers. I will not do it justice by explaining it; however, you can read a great article called Peer-to-Peer Communication Across Network Address Translators on the matter by Bryan Ford, Pyda Srisuresh, and Dan Kegel.

Getting across NATs is probably one of the hardest things when doing network programming. The problem is that just about everyone on the internet now days has a NAT. This problem is also shared with P2P protocols such as the Gnutella protocol. Google Talk is another example.

A commonly used library for doing all of this work for you and getting across NATs is STUNT. STUNT stands for: Simple Traversal of UDP Through NATs and TCP too.

Getting across firewalls:

If possible it is best to put your connection to a port like 443 (HTTPS) or port 80 (HTTP). Because almost all firewalls will let you have outgoing socket connections on those ports.

Windows specific, sessions and more:

More advanced Windows specific functionality relating to remotely controlling computers has to do with sessions.

In windows you can have 1 or more sessions. Each session represents one logged on user. A single user can login multiple times and belong to multiple sessions. Each session can have one or more Desktop's. An example of a Desktop is what you are looking at now, another example is your screensaver. Both your screensaver and what you are looking at now belong to the same session, but have a different Desktops.

Every application in windows that is started can be started in any session and in any Desktop. But that Session and Desktop cannot be changed once the application is already started. For this reason, it is typical to see desktop software of all types that have a core program, plus a viewer. The core program can be started on any session and in any desktop, it has no GUI. The viewer can then be started on one or more sessions and desktops and communicates with the Core program and displays a GUI to you.

In Windows Vista and later, Microsoft introduced something called Session 0 isolation. They discuss its impact in this article aptly entitled Impact of Session 0 Isolation on Services and Drivers in Windows Vista. Did it ever have an impact...

The Session 0 isolation change broke many programs that were compatible with Windows XP and Windows 2003. The change created hundreds of thousand of developer hours needed from 3rd party developers. Suddenly complete programs needed to be restructured to accommodate for Session 0 isolation. In general this change means that all Windows services now run in Session 0. Session 0 cannot have any GUI associated with it. No GUI can be seen across sessions anymore. And the applications you see now cannot be in the same session as Session 0. The problems that this causes are far reaching, but for the most part most companies worked around them.

Sessions can be controlled by programmers using the WTS API which is now called the Remote Desktop Services API. This article focus' on the RFB Protocol but you can also accomplish Remote Control in Windows by using the RDP Protocol and related API.

If multiple Sessions and Desktops exist, the RFB Server must decide which one to use. This may or may not be the same as which session and desktop the RFB Server runs from.

Hooking user input:

Another important aspect of remotely controlling computers is called a Hook.

A Hook allows you to get feedback system wide, or per process wide about what events are happening on the system. A typical Hook that you would install is a keyboard and mouse hook. These hooks would be installed on the RFB Server so it can better detect changes to send to the RFB Client.

In Windows, Hooks are implemented in a DLL and Windows will load that DLL and notify it of the events of what the hook is registered to do.

Web Versions:

In some software for remotely controlling computers, the Client side is on the web. This is simply done by HTTP and a lot of AJAX. The web page itself makes the requests directly to the Server and updates the web browser dynamically with the content of the retrieved framebuffers.

Remote-Control-Computer Software:

There are many VNC client/server implementations. Most are open source and licensed under the GPL.

There are also VNC client/server implementations that are based on the VNC protocol but don't follow it exactly. An example is Fog Creek Copilot. I go into more detail about the Fog creek Copilot project here.

Tags:

Add a new comment



Aug
22
2009

The new site is out!

Last modified: Saturday, July 17, 2010

Several months ago I decided that this website needed a revamping.

The work since that thought is finally being released today. The work on the site is still slowly in progress, but I feel that enough of the site is done to be better than the last site.

Powered by Django

Some stats on the new site:

  • Was developed on my mac
  • Was written using vi for all editing
  • Was built using Django 1.1
  • Is running with Python 2.6
  • Is served through IIS with PyISAPIe. (PyISAPIe is a great project created by Phillip Sitbon which brings Django to IIS.)
  • JQuery was used for a couple of post page served alterations
  • The database is built using SQLite
  • The blog posts with code samples are made with Google's code prettify
  • All blog posts are rendered using Django Markdown (which makes for easy posting of great content)

Markdown is a hassle free, easy to use markup language that allows you to easily post links, post images, and format great blog posts with little work.

The old site was built in ASP .NET beta, and slowly upgraded over time to newer versions. Overall for me, coding in Django is a much better and funner experience than coding in ASP .NET ever was.

Before deciding on Django for the new site, I also had an attempt at Ruby on Rails. I wasn't a fan from the start, the language was simply too different from what I am used to and know.

I like the new design a lot better than the old one. I hired Bradd Bezaire to do this design over 2 years ago in August of 2007. This design sat in my inbox since then because I've been extremely busy, but I'm glad his work didn't go to waste after all.

Tags:

Add a new comment



Aug
21
2009

PyISAPIe with Django 1.1 and Python 2.6

Last modified: Saturday, July 17, 2010

The PyISAPIe help files currently claim you have to use Python 2.5 and uses Django pre 1.0.

But with some small changes to one of the config files, you can actually have Django 1.1 and Python 2.6.

The only change from their help file that you need to do is replace their pyisapie.py with this file that I modified.

Tags:

Add a new comment



Aug
18
2009

Overloading logical operators

Last modified: Saturday, July 17, 2010

It is no surprise that the following function returns true

bool test()
{
  bool b1(true), b2(false);
  return b1 || b2;
}

But why does it return true? Most people think that there are 2 expressions here, b1 and b2 and that if either of them are true then the language magically evaluates the total expression to true.

But what's actually happening is that there is a built in global operator that is defined.

bool operator||(bool, bool)

This function is called for every 2 boolean values that you stick an || in between.

This raises the question, if there is an || operator for boolean values, can you define some for other types too? The answer is: Yes! (But not for built-in types)

It is not obvious, because most people haven't seen code like this, but you can also overload logical operators to work with types other than boolean values.

bool test()
{
  A a1, a2;
  return a1 || a2;
}

Is a1 and a2 implicitly converted to bool? No. What's happening here is that the class A has an overloaded logical operator.

Here's how to overload the logical OR operator:

class A
{
  public:
    A(bool b) : b(b)
    {
    }
    bool operator||(A & a)
    {
      return b || a.b;
    }

  bool b;
};

What we have above is an overloaded operator for logical OR and it takes in 2 different A types.

So what happens if you stick a bool value on the right hand side of a value with type A? It will give you a compiling error.

bool test()
{
  A a;
  bool b(true);
  return a || b; // Error no overloaded operator defined for bool A::operator||(bool)
}

What's seems even more strange though, is that you can overload logical operators so that they do not return a bool type.

Why would you ever want to do that? One good reason comes to mind.

Consider you would like to define a new type similar in every way to boolean values, but it can have 3 states: true, false, and not_set. You could define a tristate type which works exactly the same way as a boolean value in every way, but that has 3 different states.

This is in fact how boost::tribool is implemented:

namespace boost {
  namespace logic {
    class tribool;
    bool indeterminate(tribool, unspecified = unspecified);
    tribool operator!(tribool);
    tribool operator&&(tribool, tribool);
    tribool operator&&(tribool, bool);
    tribool operator&&(bool, tribool);
    tribool operator&&(indeterminate_keyword_t, tribool);
    tribool operator&&(tribool, indeterminate_keyword_t);
    tribool operator||(tribool, tribool);
    tribool operator||(tribool, bool);
    tribool operator||(bool, tribool);
    tribool operator||(indeterminate_keyword_t, tribool);
    tribool operator||(tribool, indeterminate_keyword_t);
    tribool operator==(tribool, tribool);
    tribool operator==(tribool, bool);
    tribool operator==(bool, tribool);
    tribool operator==(indeterminate_keyword_t, tribool);
    tribool operator==(tribool, indeterminate_keyword_t);
    tribool operator!=(tribool, tribool);
    tribool operator!=(tribool, bool);
    tribool operator!=(bool, tribool);
    tribool operator!=(indeterminate_keyword_t, tribool);
    tribool operator!=(tribool, indeterminate_keyword_t);
  }
}

Overaloading logical operators can be powerful, but I would advise to only do it if your type can be logically and implicitly considered true or false.

Tags:

Add a new comment



Aug
5
2009

Wedding Anniversary

Last modified: Saturday, July 17, 2010

Already Shannon and my 2 year anniversary. We have 2 baby boys so we're averaging a child per year.

Time to go visit the wedding web page and browse the photo albums.

Tags:

Add a new comment



Jul
28
2009

Slow compilation time in C/C++

Last modified: Saturday, July 17, 2010

Compilation in C/C++ is a very big operation due to C/C++'s complex grammar. Source files typically residing in .cpp are always only compiled one time; however, header files typically residing in .h files are compiled once per compiler execution. Each header file needs to be recompiled because there could be different effects made from the preprocessor.

Since an individual header file is often compiled many times, header compilation as a whole can make up a large part of your total C/C++ compilation time.

Two of ways you can do to reduce this portion of compilation time is:

  1. Forward declarations
  2. Precompiled headers

Forward declarations

Extensively using forward declarations at all times will give you the biggest performance in compilation time.

Forward declaration means to declare something without defining it in a header file. Include the header file instead in the source file where it will be compiled and parsed only once.

c.h:

class C
{
public:
  C()
  {
  }
};

d.h:

class C; //<--- This is a forward declaration

class D
{
public:
  D()
  {
  }

  C c;
};

Notice that d.h does not include c.h even know it uses a class declared in c.h

main.cpp:

#include "c.h"
#include "d.h"

int main(int argc, char **argv)
{
  D d;
  return 0;
}

In main.cpp it is important that you include c.h before d.h; otherwise, the compiler will complain about C being an undefined type.

Note you can also perform forward declarations with template types:

template <typename T>
class CMyClass;

Precompiled headers

Precompiled headers allow you to speed up compile time when compiling C++ source code. You typically put anything in a precompiled header that doesn't change often or ever such as the standard library includes or boost includes.

Precompiled headers are available for most C++ compilers including GCC and Visual C++. Both of those implementations are similar.

Only 1 precompiled header can be included per compilation, so therefore at a minimum per file. But in a single project you can have several different precompiled headers.

In Visual C++ the compiled headers have an extension of .pch and in GCC they have an extension of .gch.

In GCC you compile headers just like any other file but you put the output inside a file with a suffix of .gch.

So for example if you precompile stdafx.h you will have a precompiled header that will be automatically searched for called stdafx.h.gch anytime you include stdafx.h

stdafx.h:

#include <string>
#include <stdio.h>

a.cpp:

#include "stdafx.h"

int main(int argc, char**argv)
{
  std::string s = "Hi";
  return 0;
}

Then compile as:

> g++ -c stdafx.h -o stdafx.h.gch
> g++ a.cpp
> ./a.out

Your compilation will work even if you remove stdafx.h after step 1.

Tags:

Add a new comment | 1 comment(s)

Jack on Wednesday, July 28, 2010 (04:07:30) says:
Brian, Unfortunately your forward declaration example does not seem to be demonstrating anything. main.cpp will compile just fine without the forward declaration in d.h. Perhaps there is something missing from the example? I am interested because I have a rather large C++ project that I would love to speed the compile time up for. Jack



Apr
1
2009

Python's holy grail

Last modified: Saturday, July 17, 2010

Google has a new project named Unladen Swallow. It's primary goal is to increase the performance of CPython by five times.

They will do these optimizations in a branch of CPython and those changes will hopefully be eventually merged into the CPython trunk.

The open source Low Level Virtual Machine (LVVM) project provides a compiler infrastructure framework that can be used to construct virtual machines and native code generators. Several front ends already exist including C and C++.

Google plans to use LVVM to build a just in time (JIT) compilation to replace Python's own virtual machine.

Tags:

Add a new comment



Feb
5
2009

The twins have arrived

Last modified: Saturday, July 17, 2010

February 5th 2009 my twin boys were born.

Ronald Brian Bondy was born at 4:34pm and weighed 5 pounds 11 ounces. Lincoln Edward Bondy was born at 4:35pm and weighed 5 pounds 10 ounces.

Both boys are home now and are both doing well. Ronnie has gained almost 2 pounds from his birth weight and Link has gained over 1 pound since his birth weight.

Myself I'm as proud as ever and love them to pieces.

Tags:

Add a new comment