Skip navigation.

Beyond the Sky

The place where surface stop and share the experience of life

Posts tagged with "programming"

New programming language released on 2nd Jan 2007

D Programming !!

What the heck? it is new programming language after C#, it claims to improves from c#, c++ and java. It comes with some syntax changes, see as bellow:

import std.stdio; // for writefln()
int main(char[][] args)
{
foreach(int i, char[] a; args)
writefln("args[%d] = '%s'", i, a);
return 0;
}



Haih! the earth is turning fast, when will the F programming released? 2020? or maybe F flat? Check out some info from http://en.wikipedia.org/wiki/D_programming_language

In case you wanna try it out, http://dgcc.sourceforge.net/

gdc, lol g++ is to use to compile for c++ , gdc is for D.

vim with cscope

,

Another tools that improve c source code reading beside ctags. With cscope, it make function call search more effective. It allows you to search the function, enum, class within the source code in same directory and also library header such as stdlib.h stdio.h etc.

Its easy to start working with cscope, first apt-get install cscope, at the source code directory type 'cscope -R'. Copy cscope_maps.vim to vim/plugin location, if your vim is 6.x.x. Then, vi your source code and move your cursor to the keyword you wanna search, press 'ctrl \' 's'

The result will be shown at the list with numbers, pick one. To go back 'ctrl t'.

p/s:
my vim plugin directory at /usr/share/vim/vim63/plugin/

This is the complete guide line.
http://cscope.sourceforge.net/cscope_vim_tutorial.html

vim with ctags

,

If you are c/c++ programmer and vim user, maybe know ctags. Most of the time, source code are distributed to many folders and each of them consist of numbers of .cc/.c and .h. Sometimes to search the definition of a function, class, enums etc by using "/" is inefficient. ctags allow fast jumping to function call even the function definition source code are from other directories.

apt-get install ctags


In order to use ctags, first run the command at destination directory where the source codes are located.

ctags -R *

-R is to recursively generate ctags file across certain directories. A 'tags' file will be created. If you have learn vi from vimtutor, you may know that to jump from one keyword to targeted location is to press 'ctrl ]'. Same as ctags works.

Let say when you discover a function call which you wanna see the definition, simply point the cursor to that function and press 'ctrl ]' and it will brings you there. If you wants to go back to where you came from, simply press 'ctrl t'.

Bare in mind, if the function calls are from libraries such as stdio, curses etc, you won't be able to jump to.



dotGNU Portable.NET

, ,

A Senior programmer facing problem with mono and it remains unsolved. This leading the programmer shifting from Microsoft .net / mono to dotGNU portable.NET. As many people knows what .net is about, if you don't, that means you are either not in this field or outdated. Anyway may be you never heard about mono or dotgnu.

Mono is a project led by Novell, Inc. (formerly by Ximian) to create an ECMA standard compliant (Ecma-334 and Ecma-335), .NET compatible set of tools, including among others a C# compiler and a Common Language Runtime. Mono can be run on Linux, FreeBSD, UNIX, Mac OS X, Solaris and Windows based computers.
Quote from wikipedia



DotGNU Portable.NET, an implementation of the Common Language Infrastructure (CLI), more commonly known as .NET, includes everything that you need to compile and run C# and C applications that use the base class libraries, XML, and Systems.Windows.Forms. Currently supported CPUs: x86, PPC, ARM, PARISC, s390, ia-64, Alpha, MIPS, Sparc. Supported operating systems: GNU/Linux (on PCs, Sparc, iPAQ, Sharp Zaurus, PlayStation 2, Xbox,...), *BSD,Cygwin/Mingw32, Mac OS X, Solaris, AIX and Microsoft Windows.
Quote from wikipedia



It seems that mono project and dotGNU are the same, both are open source, but after reading wikipedia, i discover that mono have complicated licensing by Novell, where dotGNU is free software and have complicated philosophy behind it (This page shows you the danger of using microsoft .net and explain their objective of this dotGNU project)

To get dotGNU installed in debian is easy, apt-get install pnet. But if you want it work under RPM based Linux like Fedora or Centos, you need to compile yourself. (Bare in mind, the binaries from apt-get is not uptodate.)

To make it work at least you need treecc, pnet and pnetlib. You can download it.)

Dependencies
gcc gcc-c++ ncurses ncurses-devel flex byacc

Bellow are the simple script to ease the newcomers to try out if you are rpm based linux user. If you successfully install, the snake game will be running.
#!/bin/sh
echo "Solves the Dependencies"
yum install gcc gcc-c++ ncurses ncurses-devel flex byacc

echo "Compile treecc-0.3.8"
tar -zxvf treecc-0.3.8.tar.gz
cd treecc-0.3.8
sh configure
make
make install
cd ..

echo "Compile pnet-0.7.4"
tar -zxvf pnet-0.7.4.tar.gz
cd pnet-0.7.4
sh configure
make
make install
cd ..

echo "Compile pnetlib-0.7.4"
tar -zxvf pnetlib-0.7.4.tar.gz
cd pnetlib-0.7.4
sh configure
make
make install

cd samples/simple
ilrun snake.exe

pure virtual function

,

What is the different between pure virtual function and virtual function?

Virtual function is used to ease polymorphism implementation, which allows the c++ compiler to differentiate the actual "type" of the objects created which derived from a base class.

Pure virtual function is also virtual function, but it do not have a body. It looks just like a function declaration, and it forced the derived class to implement the function defination. (compiler gives error if the derived class do not define the pure virtual function). The base class that contains pure virtual function is call abstract class. It defines generic interfaces without specified the functionalities.

How to declare virtual function and pure virtual function?
That is simple, just add the magic keyword infront of the function you wanna turn it to virtual function:
class CPolygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area () //THIS MAKES THE FUNCTION VIRTUAL
      { return (0); }
  };

How about Pure Virtual Function?
class CPolygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area ()=0; //PURE, NO INLINE DEFINATION
  };

I remember when I first learn about pure virtual function, it was 6 years ago. Never used and exam also didn't covers. Its been awhile I didn't really touch Object-Oriented C++.

c++ constant pointer

,

To declare a const pointer point to an array, look at the four lines

1 int const* q = new int[50];
2 int* const q = new int[50];
3 const int* q = new int[50];
4 const* int q = new int[50];


Do they means the same? Not all are the same. Line 4 is invalid declaration, line 1 and 3 are identical.
For line 1 and 3, the value in the array are constant, you can move your pointer like q++ but you cannot change the value like *q=8 or q[0]=8
For line 2, the pointer is constant, that means you may change the value but you may not move your pointer like q++.

It can be very confusing, thats why i blog it for future reference.

Memory leak example

,

//: C13:BadVoidPointerDeletion.cpp
//// Deleting void pointers can cause memory leaks
#include <iostream>
using namespace std;
class Object {
   void* data; // Some storage
   const int size;
   const char id;
   public:
   Object(int sz, char c) : size(sz), id(c) {
   data = new char[size];
   cout << "Constructing object " << id
        << ", size = " << size << endl;
             }
   ~Object() {
   cout << "Destructing object " << id << endl;
   delete []data; // OK, just releases storage,
                  // no destructor calls are necessary
             }        };

int main() {
         Object* a = new Object(40, 'a');
         delete a;
         void* b = new Object(40, 'b');  
         delete b; //delete b without calling constructor will cause the data[] unreleased 
                   //and leads to memory leak.
           } ///:~ picked from pg 584 of thinking in c++ by bruce


When you compile this code, it do have warning:
$ g++ -o memleak{,.cc}
memleak.cc: In destructor 'Object::~Object()':
memleak.cc:17: warning: deleting 'void*' is undefined
memleak.cc: In function 'int main()':
memleak.cc:26: warning: deleting 'void*' is undefined

If you run it, you get the output:
Constructing object a, size = 40
Destructing object a
Constructing object b, size = 40

Destructor of object b is not called, it causes memory leak.

Singleton Class

,

//: C10:Singleton.cpp
// Static member of same type, ensures that
// only one object of this type exists.
// Also referred to as the "singleton" pattern.
#include <iostream>
using namespace std;
class Egg {
static Egg e;
int i;
Egg(int ii) : i(ii) {}
Egg(const Egg&); // Prevent copy-construction
public:
static Egg* instance() { return &e; }
int val() const { return i; }
};
Egg Egg::e(47);
int main() {
//! Egg x(1); // Error -- can't create an Egg
// You can access the single instance:
cout << Egg::instance()->val() << endl;
} ///:~

pick from thinking in c++ by Bruce Eckel, pg 454

This is an example code that a class where only a single object can be create.

const in class

,

When you want to create a constant in class which every object initialized from the class has its own different const value. That means the value must be initialized when instanciate and it do not allow to change for the rest of the time. This is the first time I read about this.

//: C08:ConstInitialization.cpp
// Initializing const in classes
#include <iostream>
using namespace std;
class Fred {
const int size;
public:
Fred(int sz);
void print();
};
Fred::Fred(int sz) : size(sz) {} // THE IMPORTANT PART
void Fred::print() { cout << size << endl; }
int main() {
Fred a(1), b(2), c(3);
a.print(), b.print(), c.print();
} ///:~

Object oriented programming in C++

,

On the halfway of reading "Thinking in C++ by Bruce Eckel", I just realized that, Object Oriented Programming in C++ can be written without using "class".

//: C05:Class.cpp
// Similarity of struct and class
struct A {
private:
int i, j, k;
public:
int f();
void g();
};
int A::f() {
return i + j + k;
}
void A::g() {
i = j = k = 0;
}
// Identical results are produced with:
class B {
int i, j, k;
public:
int f();
void g();
};
int B::f() {
return i + j + k;
}
void B::g() {
i = j = k = 0;
}
int main() {
A a;
B b;
a.f(); a.g();
b.f(); b.g();
} ///:~

The source code picked from the book page 290 shows that struct A and class B are the same. Object can be defined in struct exacally the same way as class, except that class defaults to private, whereas struct defaults to public.
December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31