Long Distance Commuting

Thankfully this is going to be temporary, but I’m now scheduled to begin my few weeks of work with some long distance commuting.

Lucky for me, I have been hoarding my frequent flyer miles for years, so each trip will only cost me $7.50 in fees for redeeming the award ticket.

By the numbers. Over a period of 21 days, I am scheduled to be flying 14656 in 10 flights visiting 5 different airports (CVG, ATL, SJC, LAX, SEA).

Lots of in-flight wifi will be purchased and lots of working from planes / airports / etc.

Private? The false assurances of C++

Yet another reason I believe that C++ is the wrong language for teaching introductory programming. Because of the way classes are laid out in memory, it is trivial to show (a good thing), but it is also trivial to access those private member variables directly.

TLDR? – View the gist on github

Let’s break this down. We have a class called Point

point1

The point class in this instance represents a 2D and has the private member variables of x and y.

point2

If we have, say, a local variable (Point p;) we can do some basic calculations. This may be compiler / OS / architecture dependent, but this works in 32-bit linux w/ gcc on i386.

point3

When we run this code, we see that the sizeof(point) is identical to the sizeof(int) + sizeof(int). Since classes in C++ are based on structs in C. This is just a tight packing of the member variables.

And if the base pointer for &p is 0xa43c then we see that the address of x is the same, and y is off by 4 bytes (the size of an int).

Now – if we just try to access the private member variable directly, i.e. p.x = 27; the compiler will detect that we’re trying to do something bad and give us an error (good!).

However, through the magic of memory addresses and pointers we can change that. It is a simple cast. First we get a pointer to our instance of the Point class, and then we re-interpret that as a pointer to an int. From there we can start derferencing and using pointer math.

point4

That’s it. A simple cast and some pointer math and private doesn’t mean anything. Who needs encapsulation anyway?

DRY, Shy and Tell the Other Guy

It has been a little while since I’ve pulled out this article by the pragmatic programmers (Andy Hunt and Dave Thomas), Keep it DRY, Shy, and Tell the Other Guy.

I’m going to use this in the first week of teaching CS 1 students classes (in C++) for the first time. To me, the critical point is the “tell the other guy” part. The way they present it is the more useful of the OO metaphors. It isn’t calling a function on a object to achieve some goal. It is sending a message to some object, with apathy towards the details of how it is done.

I think this is the key to breaking from procedural programming and moving successfully towards object-oriented programming.