C++ Study Notes (0) – Syllabus

Development, Python January 29th, 2007

Course Information: Advanced topics on C++: template, STL, boost, metaprogramming etc.

Text:

Supplementary Reading:

Objective:

  • Review C++ standards
  • Revisit the STL for iterators, function objects and algorithms
  • Overview of Boost library
  • Metaprogramming instroduction based upon Boost MPL library
  • Boost.Python library study

Schedule:

  • Read the textbooks in the Metro, on the toilet, before sleep…
  • Start coding practice in the weekend.

Project: TBD
Supported services

  • News group

The light of the dark side

Development July 18th, 2006

I went to the dark side in the last few days to meet the milestones. The C code is just simply a FORTRAN translation regardless the coding style. The messy code really annoys me when I decided to debug it. So I stepped aside to find a pain relief first.

Here is the light of the dark side, GNU indent, it does not refine your algorithm or logic, but polish your code to make it more readable. The indent has 3 built-in styles: K&R, GNU and BSD/KNF. None of them is my cup of tea, then I decided to hack a BSD/Allman from BSD/KNF style, like this:

-nbad -bap -bbo -nbc -bli0 -c33 -cd33 -ncdb -nce -nlp -ci3 -cli0 -cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -ts4 -i4 -ip0 -l75 -npcs -nprs -npsl -saf -sai -saw -nsc -nsob -nss

You can use the long argument above in indent, or copy it to $HOME/.indent.pro as the default indent style.

Return of the Lex

Development July 7th, 2006

I don’t touch the flex/yacc since the final exam of the class, until today I have to cope with the FORTRAN 90 input data. The input file is like:

80000.00 50.00
80000.00 0.00
120000.00 0.00

with various data formats, flexible lengths of white space, scattered white lines. I first tired the standard string search/match, and soon lost the patience for tedious if..else. Therefore, I decided to try Lex, the very first flex in action.

First, the flex rule. We just need the float pointer number, and ignore all the spaces, new-lines and end-of-file. We expect the token is returned one by one, so we return the flag as soon as the pattern matches.

%option noyywrap

space   [ \t]+
number  [-+]?[0-9]*\.?[0-9]*

%%

{number}      { return 1;  }

{space} |
\n          ;

%%

In main.c, we would like to override default input file handle stdin for yyin if additional argument is fed ( for some reason, this cause the wordpress 503 error, so take a look at the tarball, sorry for the inconveniece). Then we fetch the token one by one, convert the matched yytext to float-point number until the end of file.

extern FILE* yyin;
extern char* yytext;
extern int yylex(void);

int main(int argc, char* argv[] )
{
        double x;
        assert( yyin );
        if( argc == 1 )
                yyin = stdin;

        while( yylex() )
        {
                fprintf(stderr, "text = %s ", yytext );
                x = atof( yytext );
                fprintf(stderr, "x = %f\n", x );
        }

        return 0;
}

At then end, Makefile glues all the parts together.

LEX = flex
LEXFLAGS = -t  
CFLAGS = -g
OBJS = obstac.o main.o
TARGET = scan

all : ${TARGET}

${TARGET} : ${OBJS}
                ${CC} ${CFLAGS} -o ${TARGET} ${OBJS}

%.c : %.l
                ${LEX} ${LEXFLAGS} $<  > $@

%.o : %.c
                ${CC} ${CFLAGS} -o $@ -c $<
.PHONY: clean  
clean :
                ${RM} ${TARGET} ${OBJS}

Here is the tarball for the source code and example input file.

Pitfalls on porting FORTRAN 90 to C

Development June 29th, 2006

I am porting a 3000-line Fortran 90 source code to ANSI C in these days, and I would like to mark the pitfalls in this adventure:

  • Use 1-index Don’t try to act professional to use 0-index, that would confuse yourself in a few minutes, use 1-index, we can afford the one element waste.
  • Be careful of for loop FORTRAN support for loop with increment/decrement, while C does not.
  • ABS != abs Surprising? abs in C return integer, not double. I am quite annoyed to find the compiler ( gcc-4.1) does not even issue a warning for the implicit conversion. Use fabs instead
  • Prepare your debugger if you have a 300 line function, and messed global variables, taking some time to learn gdb is definitely worthy, — Intel FORTRAN, idb also supports gdb mode.

Alert: Never overlook warning

Development June 28th, 2006

I hate to search the “real error message” in the overwelming warning message when compiling the C code, since most of warning come from the type conversion. Today, I paid my price, 30 minutes debugging session.

The bug is in

double min_val( double* data, int size )

I test this code first, then put it into the main.c, later I refactor the code, move it to utilities.c. Everything seems fine, compiles, run. Oops, it is not fine, the result is totally wrong. I launched gdb, insert serveal breakpoints, inspect variables, eventually, I find the return value of callee side, i.e min_value is right, but the value in the caller side is wrong. It is kind weired…

Then, I realize that I might not declare min_val, and the compiler regards the return value is integer, instead of double. Then I rebuild the whole project with -Wall flag, yes, the warning message shows that there are “implicit conversion”, that is the source of bug!

I would put -Wall in the CFLAGS since then.