The Lazy Programmer

November 18, 2008

Functional C++

Filed under: C#,Programming — ferruccio @ 9:56 pm
Tags: , ,

It’s been awhile since my last post. It was near the end of a product shipping cycle and, well, you know how that goes. Then I got a bit addicted to stackoverflow and spent way too much time each night reading and answering  questions. Eventually, I started a couple of side-projects which may eventually yield something interesting to write about.

Anyway, a little over a month ago, I answered a question on Stack Overflow titled “What is the one programming skill you always wanted to master but haven’t had time?” I didn’t have to think much to come up with an answer to that: Functional Programming.

I understand some of the fundamental concepts behind functional programming and occasionally I dabble a bit with LISP or I read a bit more of SICP but the practical applications of FP have been elusive to me.

(more…)

August 10, 2008

Adding a lock() statement to C++

Filed under: C#,Multi-threading,Programming — ferruccio @ 9:43 am
Tags: , ,

One of the things I like about C# that I miss in C++ is the lock() statement. It provides a simple way to control access to an object across multiple threads.

When I first started writing Windows apps, I used the Windows critical section API directly. After getting tired of EnterCriticalSection/LeaveCriticalSection, (I suspect like everybody else) I created a CriticalSection class that was a simple wrapper around the raw API.

Eventually, I got around to using mutexes from the Boost Thread class. You can create a scope guard whose destructor releases its referenced object, but it still lacks the simple elegance and clarity of being able to say:

lock (some-object) {
    do something to some-object
}

(more…)

August 3, 2008

Formatting Output with Boost

Filed under: C#,Programming — ferruccio @ 9:20 pm
Tags: , ,

Sometimes a GUI is overkill for a project. You just need a simple tool to do some task. Perhaps it needs to be scripted. So you whip up a console mode program and you eventually have to output something. At this point, many developers will simply ignore the C++ iostreams library and reach for good old printf(). I can certainly understand why. The iostreams objects are easy enough to use for simple formatting tasks. However, when you need to do something more sophisticated, you will often find yourself digging through reference material, muttering "this should be easy…"

(more…)

June 4, 2008

C++: Using Boost ranges to simplify enumerations

Filed under: C#,Programming — ferruccio @ 8:58 pm
Tags: , , ,

Let’s say you’ve created a class which encapsulates an STL-style container and you now need to expose the data in that container. There are a few possibilities which we’ve probably all tried and found to be unsatifactory.

(more…)

May 14, 2008

D Development with Emacs

Filed under: D,Programming,Windows — ferruccio @ 8:44 pm
Tags: ,

A few months after discovering the D programming language, I finally got around to setting up a reasonable development environment just for writing D programs. After playing around a bit with Visual Studio and a few other Windows-based text editors, I settled on using Emacs to drive my work.

Not that I’ve used Emacs much before. I’ve often had to tweak configuration files on various *nix systems. As a result I’ve gotten to know Emacs, Vi, Joe, Jed, Nano, Pico and occasionally even Ed.

Now, when I say that I “know” these editors, I mean that I can fire them up, change some text, save and exit. Get in. Get out. Nobody gets hurt.

So I was doing some late night surfing a few days ago and I ran across two very serendipitous finds:

(more…)

April 30, 2008

Objective-C Cheat Sheet

Filed under: Objective-C,Programming — ferruccio @ 5:43 am
Tags:

I purchased my first Mac about six months ago and have been having fun with it ever since. Being a software developer, I started poking around XCode and built a few trivial apps based on some of Apple’s tutorials just to get a feeling for Cocoa and Objective-C. I’ve put together a small Objective-C cheat sheet. It’s definitely incomplete, but it has helped me greatly with some of the unfamiliar syntax.

Objective-C Cheat Sheet Download Objective-C Cheat Sheet v. 0.1

Any comments, suggestions, errata, etc. are welcome. I will update it as necessary.

April 22, 2008

Using Boost to tokenize strings

Filed under: C#,Programming — ferruccio @ 9:55 pm
Tags: , ,

Like most C++ programmers that came from a C background, whenever I needed to parse a string into a bunch of tokens, I reached for strtok(). Sure, it’s not thread-safe. But I can take care of that later if it becomes an issue, right?

Eventually, I ran across the STL and learned to put my strings and other objects into containers. But strtok would eventually rear it’s ugly head to break the abstraction of STL strings just so I could split up a string without putting in too much effort.

At some point, I started playing with Boost. If you’re a C++ programmer and you don’t know what Boost is, you need to go to boost.org and take a look at it right now because I guarantee that you are doing way too much unnecessary work without it. Go ahead, I’ll wait until you’re done looking around.

(more…)

April 17, 2008

Benchmarking stack_array<T>

Filed under: C#,Programming — ferruccio @ 10:45 pm
Tags: , ,

In my last post I presented a stack_array template/macro combination that I use to create dynamically sized arrays on the stack. By allocating the space on the stack we get the dual benefits of not having to worry about freeing memory and better performance. I knew the performance had to be better, but how much better? Would I have been better off just using STL vectors?
(more…)

April 2, 2008

C++: How to create variable length arrays on the stack

Filed under: C#,Programming — ferruccio @ 9:12 pm
Tags: , ,

A very common pattern in C++ programming is allocating a variable size array in a function and releasing it before the function exits:

void function(int size)
{
   char* pbuf = new char[size];

   ... function body ...

   delete [] pbuf;
}

This works but is error-prone. Especially if there are multiple exit points or if an exception is thrown. You could use STL vectors, but you usually don’t need to change the size of the array once it has been created. Also, you may not want the heap allocation overhead.
(more…)

March 22, 2008

C++ Exception Specifications

Filed under: C#,Programming — ferruccio @ 4:44 pm
Tags: ,

Every once in a while C++ surprises me. Something that I thought I understood turns out not be exactly what I thought it was.

In C++, a function may have an exception specification which tells the compiler what exceptions the function may throw. e.g.

void test(void) throw(std::exception)

Tells the compiler that the function test() may throw an exception of type std::exception.

(more…)

« Previous PageNext Page »

Create a free website or blog at WordPress.com.