The first picture (Claire sleeping) was taken by my good friend Matt.




![]() | Fast-forward ahead a few months, and we have hardwood floors, new drywall, paint, and we're moved in. |
#include<stdio.h>Now I write a program that uses this library. Here's the normal, non-dynamic, static linking, built like a pyramid way of using the library.
void SpecialPrint(char* payload) {
printf("Special Print:%s\n", payload);
}
#include<stdio.h>With the above code, I compile the SpecialPrint library with my main code and link them together, producing a single executable file. However, I can use the same speciallib file with code that loads the library dynamically while the program is running. The code might look something like this:
#include"speciallib.h"
int main() {
printf("normal printf argument\n");
SpecialPrint("special print's argument");
}
#include<stdio.h>In the above, the show error function is optional, I added it so that any errors that occur would be displayed. When you compile the above code, you'll need to make the speciallib into an .so file and make sure that the speciallib's directory (the current directory in my example) is listed as one of the places that we should look for shared object files. Here's what the steps to compile look like:
#include<dlfcn.h>
void ShowError() {
char *dlError = dlerror();
if(dlError) printf("Error with dl function: %s\n", dlError);
}
int main() {
void *SharedObjectFile;
void (*SpecialPrintFunction)(char*);
// Load the shared libary;
SharedObjectFile = dlopen("./speciallib.so", RTLD_LAZY);
ShowError();
// Obtain the address of a function in the shared library.
SpecialPrintFunction = dlsym(SharedObjectFile, "SpecialPrint");
ShowError();
printf("normal printf argument\n");
// Use the dynamically loaded function.
(*SpecialPrintFunction)("special print's argument");
dlclose(SharedObjectFile);
ShowError();
}
export LD_LIBRARY_PATH=`pwd`For an explanation of what the above compiler options mean, and further explanation on .so files, see these two IBM articles on using shared object files on Solaris and linux.
gcc -c -fpic speciallib.c
gcc -shared -lc -o speciallib.so speciallib.o
gcc main.c -ldl
def CalculateTax(amount):JavaScript:
return amount * 0.18
class Receipt(object):
def __init__(self, items=None):
self.items = items or []
def CalculateTotal(self):
return sum([item + CalculateTax(item) for item in self.items])
function calculateTax(amount) {
return amount * 0.18;
}
function Receipt(items) {
if (items) {
this.items = items;
} else {
this.items = new Array();
}
}
Receipt.prototype.calculateTotal = function() {
var total = 0;
for (var i = 0; i < this.items.length; i++) {
total += this.items[i] + calculateTax(this.items[i]);
}
return total;
}To use the above code, you might write something like this:my_order = Receipt([5.50, 10, 7.89])JavaScript:
print my_order.CalculateTotal()
var myOrder = new Receipt([5.50, 10, 7.89]);Now let's say someone asks you to change the tax rate which is used when calculating the total. Here's the catch, you're not allowed to change the existing code. It turns out this is actually really easy. You can define a new function, then make an existing function name point to the new function. Here's an example of how to inject our new code:
alert(myOrder.calculateTotal());
def CalculateHigherTax(amount):JavaScript:
return amount * 0.25
CalculateTax = CalculateHigherTax
print my_order.CalculateTotal()
function calculateHigherTax(amount) {
return amount * 0.25;
}
calculateTax = calculateHigherTax;
alert(myOrder.calculateTotal());After adding the above code to the foundation we started with, you will notice that the calculate total method now uses calculate-higher-tax instead of the original function, even though you are calling the same method on the same object as before. Congratulations, you have just witnessed late binding in action. #include<stdio.h>If you try to set CalculateTax to a new function definition, you will get an error at compile time because a function cannot be changed once it is bound. Early binding tends to produce more efficient programs. However, if you want to, you can still use the flexiblity available in late binding in C.
float CalculateTax(float amount) {
return amount * 0.18;
}
typedef struct {
float* items;
int num_items;
} Receipt;
float CalculateTotal(Receipt this_order) {
int i;
float total = 0;
for(i = 0; i < this_order.num_items; i++) {
total += this_order.items[i] + CalculateTax(this_order.items[i]);
}
return total;
}
int main(void) {
Receipt my_order;
float my_items[3] = {5.50, 10, 7.89};
my_order.items = my_items;
my_order.num_items = 3;
printf("%f\n", CalculateTotal(my_order));
}
#include<stdio.h>There you have it!
float CalculateTax(float amount) {
return amount * 0.18;
}
float CalculateHigherTax(float amount) {
return amount * 0.25;
}
typedef struct {
float* items;
int num_items;
} Receipt;
float (*TaxCalculator)(float) = &CalculateTax;
float CalculateTotal(Receipt this_order) {
int i;
float total = 0;
for(i = 0; i < this_order.num_items; i++) {
total += this_order.items[i] + (*TaxCalculator)(this_order.items[i]);
}
return total;
}
int main(void) {
Receipt my_order;
float my_items[3] = {5.50, 10, 7.89};
my_order.items = my_items;
my_order.num_items = 3;
printf("%f\n", CalculateTotal(my_order));
TaxCalculator = &CalculateHigherTax;
printf("%f\n", CalculateTotal(my_order));
}
sequence "intro":
playSample("beat", start=0:32.1, end=0:33.5, beats=[1,3,9,11,15])
playSample("moog", beats=[5,7,11])
shiftPitch(start=A4, end=C4, duration=bars(8))
sequence "solo":
playSample("guitarRiff", start=1:15.3, end=2:09.0)
tempo 150 BPM
play("intro", now)
play("solo", end("intro"))
play("solo" now()+bars(5))


My XO laptop arrived in the mail recently and it is quite an amazing little machine. Conclusion up front: I'm extremely satisfied with it and in some ways this laptop computer is better than ones that sell for ten times the price.sudo apt-get install:lynx (optional)If you are using a laptop, you will likely want to install the following modules:
screen (optional)
gcc (optional)
xorg
x-window-system-core
firefox
acpiWith the above installed you can check the battery's charge, remaining time, etc. by running
acpid
acpi on the command line. For the graphical desktop window manager, I chose iceWM. I installed it by adding:icewmIn the past I've worked quite a bit with Fluxbox as a window manager, but it seems like iceWM is easier to configure, especially under Ubuntu. The liQuid theme looks quite nice.
iceconf
icewm-themes
startx. I connected to my wireless network using wpa_supplicant and running iwconfig.
Vanessa prepared a wonderful meal for me this Valentines day. Four courses, the first one pictured here, all delicious. This was the roasted red pepper tomato soup with an artistic heart made of sour cream. And this was only the first course. Have I mentioned that the meal was delicious. I'm very thankful, I married quite a cook. Not only that but she decorated too.z = x * y;it means that you want the computer to "store the value of x times y in the varibale z, then display z on the screen." As you can see, some programming syntax is borrowed from math. This example includes arithmetic and a function. Functions can also be though of as verbs, with variables as the nouns. In object oriented programming, variables can be nouns which are capable of performing actions. If you had a digital carrier pigeon, and you wanted to tell it to carry a letter to your grandmother's house, you might say something like:
print(z);
myPidgeon.payload = myLetter;In human language, there are always multiple ways to say the same thing, and the same applies in programming. The programmer might just as easily design the program to give grandma the letter like this:
myPideon.flyTo(grandma.house);
myLetter.setRecipient(grandma);Now for some fun. What do the following code snippets mean?
myPideon.deliver(myLetter);
if (jack.getWorkPercent() == 100.0 &&
jack.getPlayPercent() == 0.0) {
jack.dullBoyFlag = true;
}
Pie aPie = new Pie();
Song aSong = new Song(sixpence);
aSong.sing();
fill(pocket, rye);
aPie.add(new Blackbird()[24]);
Mouse mice[3];
for (i in range(3)) {
mice[i] = new BlindMouse();
}
observe(run(mice));
party = new Party(jack, jill);
party.setTarget(water);
party.equip(pail);
party.ascend(hill);
/* A function that returns multiple values */
int, char, int myFunction(int a, int b, int c, char d) {...}
...
/* Invoke the function and store the results */
int x, y;
char c;
{x, y, c} = myFunction(5, 6, 7, 'Z');
structs. I like to think of a struct as the precursor to a class. It allows the arbitrary grouping of variables into a single collection where they can be referred to by name. (In a couple of earlier posts, I showed how you could use structs to simulate classes in C.)/* Create a 2 member struct to hold the return value */This works ok, but I would like to avoid having to create a new struct for each one of my functions. It might be easier if I didn't have to worry about types at all, so the natural choice is to have the function return a type-less void pointer (
struct myFuncReturn {
int first;
char second;
};
/* A function that returns an int and a char */
struct myFuncReturn myFunc(int a, int b, int c, char d) {
struct myFuncReturn to_return;
to_return.first = (a+b)*c;
to_return.second = d;
return to_return;
}
int main() {
struct myFuncReturn pattern;
pattern = myFunc(2, 3, 4, 'Z');
printf("Pattern: %i, %c\n", pattern.first, pattern.second);
}
void*). The calling code would then be responsible for interpreting the function's return struct correctly. If I want to return a new anonymous struct from a function, it might look something like this:void* myFunc(int a, int b, int c, char d);If I use the above, I'll need to allocate memory for the struct and return it's address. This is a bit of a bother as well, because now I need to worry about cleaning up that memory later. Instead of having the function allocate a new structure to return, why not pass in a structure and have the function modify it? The code I would need to write would be more aesthetically pleasing (in my opinion) for both the function definition and the calling code which invokes it, and it might even be more efficient.
/* Function definition, the out parameter is the return value */Look ma, no type declarations! Now you might say that writing out the entire struct definition each time is a bit unpleasant, but you could always define a struct and use it instead. I wanted to show that you don't really need to declare a type for each function, which could create a bit of a mess if you start using multi-return functions everywhere. With the above pattern, you could also start to play some interesting games by having functions that actually return different structs in different situations (provided the out pointer's reserved space is large enough for the data you want to send back). If I've lost you by now, I do apologize.
void myFunc(void* out, int a, int b, int c, char d) {
((struct{int first; char second;}*)out)->first = (a+b)*c;
((struct{int first; char second;}*)out)->second = d;
}
int main() {
struct{int first; char second;} pattern;
myFunc(&pattern, 2, 3, 4, 'Z');
printf("Pattern: %i, %c\n", pattern.first, pattern.second);
}
myFunc like this:void myFunc(void* out, int a, int b, int c, char d) {
((struct{int first;}*)out)->first = (a+b)*c;
((struct{int x; char second;}*)out)->second = d;
}Or if you want to go even further, like this:void myFunc(void* out, int a, int b, int c, char d) {
*((int*)out) = (a+b)*c;
((struct{int x; char second;}*)out)->second = d;
}Ah, the joys of programming. It's little games like this that make programming lots of fun. It's like working on a big wide open puzzle that you get to build yourself. No wonder I'm spoiled.
@username notation, you can let everyone know your post was directed to a specific person so that they have a window into the conversation.ship.2-OLPC-655.vmx file and started it up.yum install gccand it downloaded and installed all prerequisites.
yum install lynxI ran
lynx www.google.com and searched for firefox and downloaded the tar.gz Linux version. After downloading I unpacked the archive using tar zxvf firefox-2.0.0.11.tar.gzI tried running firefox, but got an error about a missing shared object library. Yum was able to find this as well. One final time as root, I ran
yum install libstdc++.so.5After installing the C++ library, firefox ran just fine. The menus and graphics in Firefox matched the XO's theme, which I thought was pretty nifty. I was also able to install Flash. Well there you have it, why not take it for a test drive yourself.