Archive for the ‘Programming’ Category.
March 12, 2010, 5:43 pm
I first tried it this way:
$ cd OpenCV-2.0.0
$ mkdir build
$ cd build
$ cmake ..
$ make
But it failed. I got lots of errors when trying to link highgui to the Carbon libraries. The problem is that Carbon is 32-bit only, and by default OpenCV was being built for 64-bit.
Here the (very simple) solution to that:
$ CFLAGS="-arch i386" CXXFLAGS="-arch i386" cmake ..
$ make
October 23, 2009, 10:36 pm
When I started to learn about Lua, I found it a little odd that the numeric 0 (zero) value is treated as a true boolean value by the interpreter, which was different from all other programming languages I had worked with.
However, it turns out that it makes a lot of sense to have 0 as a true value, and that’s when you take short circuit expressions into account.
For those who don’t know, short-circuit expressions are used to conditionally select values without having to code that selection into an if-elseif-else structure. Short-circuit expressions are built using logical (and, or, not) operators. The and operator returns the value of the first false expression, or the last expression, in case all others were true. The or operator returns the value of the first true expression, or the last expression, in case all others were false. Here are a few examples in Lua:
Code:
word = 1 < 2 and "hello" or "world"
print(word)
Output:
Code:
level = 10 > 9 and 2 or 4
print(level)
Output:
Now, consider the following Lua short-circuit expression:
bit = math.random() < 0.5 and 0 or 1
Running it many times will psuedo-randomly assign both 0 and 1 values to the bit variable:
Code:
for i=1,8 do
bit = math.random() < 0.5 and 0 or 1
print(bit)
end
Output:
Now, consider what happens when we try to do the same thing in Python 2.6:
Code:
import random
for i in range(8):
bit = random.random() < 0.5 and 0 or 1
print(bit)
Output:
The first operand of the and expression is always false, because 0 is considered to be a false boolean value by the Python interpreter.
Even though one could argue that it’s just a matter of knowing how to rewrite such logical expressions, I believe it’s good not having to worry about undesired behavior caused by 0’s in short-circuit expressions. It allows me to write expressions just as they come to my mind, which makes me more productive than if I had to re-arrange them.
July 1, 2009, 11:43 pm
Here’s how you translate the following C code
int max(int a, int b)
{
return (a > b ? a : b);
}
to Python:
def max(a, b):
return a if a > b else b
The general syntax is:
TRUEVAL if CONDEXPR else FALSEVAL
Note: I’m not 100% sure, but I think that only works starting from Python 2.5.
May 6, 2009, 4:59 pm
When reading single bytes from a file in C, one must pay attention to the correct usage of feof() and fread(). At first, the following piece of code seems to work correctly:
const char *filename = "hello";
unsigned char byte;
FILE *fp;
fp = fopen(filename, "rb");
if (!fp) {
printf("could not open file\n");
return 1;
}
while(!feof(fp)) {
fread(&byte, 1, 1, fp);
printf("%02x\n",byte);
}
fclose(fp);
Suppose the file “hello” has the following contents:
0000000: 68 65 6c 6c 6f 0a hello.
(which is the string “hello” followed by an LF)
When the code above is run, the following output is produced:
Notice the last character seems to be read twice. The problem is that feof() only returns true after attempting to read past the end of the file. In order to fix this “read-twice” behavior, the return value of fread() must be checked:
if(!fread(&byte, 1, 1, fp)) {
break;
}
Note: Using feof() as the while condition is kind of redundant here. In this situation, one could simply use while(1) and the behavior would be the same.
Update: A much better solution was given by my friend Bryan:
const char *filename = "hello";
unsigned char byte;
FILE *fp;
fp = fopen(filename, "rb");
if (!fp) {
printf("could not open file\n");
return 1;
}
fread(&byte, 1, 1, fp);
while(!feof(fp)) {
printf("%02x\n",byte);
fread(&byte, 1, 1, fp);
}
fclose(fp);
April 25, 2009, 9:13 pm
I am not really a C++ programmer. I usually code in C, and I think all C++ I’ve ever written involved a couple of vectors and maybe one or two classes. So what I’m writing here is certainly old news for C++ programmers.
The other day, while I was reading this essay about the Liskov Substitution Principle, I was intrigued by a situation presented in page 4, which deals with class inheritance and method overriding.
Methods that can be overriden must be declared virtual in C++. I really didn’t know about that. I am used to Java’s behaviour, which I illustrate below:
/*
* A.java
*/
public class A {
public void bar() {
System.out.println("bar");
}
}
/*
* B.java
*/
public class B extends A {
public void bar() {
System.out.println("Bar!");
}
}
/*
* C.java
*/
public class C {
public void call(A o) {
o.bar();
}
}
/*
* Main.java
*/
public class Main {
public static void main(String [] args) {
A a = new A();
B b = new B();
C c = new C();
c.call(a);
c.call(b);
}
}
The code above will produce the following output:
If I wanted to do the same thing in C++, I would have to write it this way:
#include <iostream>
class A {
public:
A() {}
virtual ~A() {}
virtual void bar() { std::cout << "bar" << std::endl; }
};
class B : public A {
public:
B() {}
virtual ~B() {}
void bar() { std::cout << "Bar!" << std::endl; }
};
class C {
public:
C() {};
virtual ~C() {};
void call(A &o) { o.bar(); }
};
int main(int argc, char *argv[]) {
A a;
B b;
C c;
c.call(a);
c.call(b);
return 0;
}
The output will be the same as the Java program, but pay attention to the virtual modifier placed before the declaration of the bar method in A. If I remove it, the output will be:
Even though B declares a method called bar, it is completely shadowed by the implementation inherited from A. In my opinion, Java’s behaviour is a lot more intuitive. I wonder how many people might have spend hours, maybe days, looking for a bug in a C++ program when the problem was that a method was not declared virtual and method calls weren’t occuring as expected.
I talked about this with Otávio, and he found out C# has a similar characteristic. In C#, a method must be declared as virtual for its subclasses to override it AND the subclasses must use override when overriding the method. Here’s the code he provided me:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Alice
{
public virtual void sayHello()
{
Console.WriteLine("Hello, world");
}
}
class Bob : Alice
{
public override void sayHello()
{
Console.WriteLine("Hello, world!");
}
}
class Echo
{
public void say(Alice toto)
{
toto.sayHello();
}
}
class Program
{
static void Main(string[] args)
{
Alice a = new Alice();
Bob b = new Bob();
Echo e = new Echo();
e.say(a);
e.say(b);
}
}
}
Some people argue that this makes code clearer, but I’m not convinced. Why not make overriding implicit?
January 25, 2009, 1:57 am
I have set up an SVN repository for QuickSynergy at my recently-acquired Dreamhost hosting. Their control panel is great and setting everything up was very easy.
However, it doesn’t install any repository browsing tool such as ViewVC, and I really enjoy using that kind of thing. So I went after it and managed to install it successfully at http://svn.cesarbs.org/viewvc.cgi. The installation steps are quite simple and are listed below. I am assuming you have set a up a separate subdomain exclusively for your SVN repositories, such as svn.yourdomain.org.
- Download the latest ViewVC package from the official website.
- Extract the package somewhere in your home directory at Dreamhost.
- Enter the viewvc-version directory and run the viewvc-install script.
- Install ViewVC somewhere in your home directory.
- Edit the /path/to/viewvc/viewvc.conf configuration file.
- Point svn_roots to the path to your SVN repository (look for the svn directory in your home directory).
- Set default_root to svn.
- Copy ViewVC’s scripts from /path/to/viewvc/bin to /home/user/svn.yourdomain.org.
- Create a file named .htaccess under the /home/user/svn.yourdomain.org directory with the following contents:
Options +ExecCGI
AddHandler cgi-script .cgi
You are done installing ViewVC. Just access it at svn.yourdomain.org/viewvc.cgi!
Note: If you don’t want everyone to be able to see you files, you will have to configure authenticated access to your subdomain using the Dreamhost panel tool for that purpose.
December 3, 2008, 7:18 pm
I really think that’s what we should have in mind when writing code:
Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. — Donald Knuth