Wednesday, July 2, 2008

Running the Eclipse 3.4 formatter application

D:\gnu\eclipse\eclipse.exe -application org.eclipse.jdt.core.JavaCodeFormatter -config D:\gnu\jee\workspace\Concurrency\.settings\org.elipse.jdt.core.prefs d:\Foo.java

Thursday, June 12, 2008

variable type checking in C

Put all the files in a directory. Run gcc *.c -o main.exe. The compilation will succeed. The reason is that C does not check type consistency of external variable declaration and reference.
1 one.c

int abc = 1;

2 main.c

#include <stdio.h>
extern float abc;
int main(void) {
printf( "%f", abc );
}

Function type checking in C

Put all the following 3 files in a directory. Run gcc *.c. The compilation will succeed. And running the resulted excecutable file will print I am here, man!. Run g++ *.c The compilation will fail. The reason is that during linking, C only check function names. But C++ check function type.
1. caller.c

#include "callee.h"
int main(void) {
foo();
}

2. callee.h

int foo(void);

3. callee.c

#include
void foo(int v) {
printf( "I am here, man!" );
}

Wednesday, May 14, 2008

Index overflow in Java for loop

The following little code snippet shows the overflow of for loop index in Java.

public class Out {
public static void main(String[] ars) {
for(int i = 0; i <= 2147483647 ; i+=100000000 )
{
System.out.println( i + "\n" );
}
}
}

You may expect that the iteration count is 22. But it is not true. i is 2100000000 after the 22nd iteration. Then 100000000 is added to i(2100000000) again. 2200000000 is bigger than 2147483647. So int overflow will happen. i will become a negative integer. So the loop will continue.

For details of loop index overflow, you can refer to Programming Language Pragmatics.

Friday, May 2, 2008

Genenared methods values and valueOf for Enum

I have recently tried Java Enum when doing software development. The following code is an example:

public Enum FooType {
ABC, DEF;
}

I found two methods valueOf and values in the Javadoc for my enum code. I did not write these 2 methods. And it's superclass java.lang.Enum<FooType> also does not have these 2 methods. Then I checked JLS 3.0. 8.9 Enums says these 2 methods are automatically generated. It is a little magic. One Java class can has methods which do not exist as source code. Too many magic which do not have a consistent rationale can make a language hard to learn and use.

Saturday, April 26, 2008

Modula-2

I tried a little Modula-2 programming today. I used XDS 2.5. I built the following little program successfully in XDS IDE.

MODULE hello;
FROM InOut IMPORT WriteString, WriteLn;
BEGIN
WriteString("Hello, world!");
WriteLn;
END hello.

And I could use xc to compile it. The following error showed up when I used xlink to link it.

XDS Link Version 2.6 Copyright (c) 1995-2001 Excelsior
Fatal error (13): No program entry point


I have googled answers for it and read XDS documentation. Unfornately, I can't find an answer. Again, it shows a quick start guide with some simple examples is extremely important for a beginner to use some new tools.

After wresting with xlink for some time , I worked out the following solution.

xlink hello.obj C:\free\XDS\LIB\x86\libxds.lib C:\free\XDS\LIB\x86\import32.lib C:\free\XDS\LIB\x86\xstart.lib

Tuesday, April 15, 2008

Reading of Programming Language Pragmatics

Programming Language Syntax


The following syntax shows dangling else problem of Pascal.

  • stmt -> if condition then_clause else_clause | other_stmt

  • then_clause -> then stmt

  • else_clause -> else stmt | ε


Sometimes, we have a hard time to use some language. Sometimes it is not because we are not smart enough. It is because of the flaws of the programming language.