Feeds:
Posts
Comments

Archive for September, 2007

this is what I read from comp.lang.c, enjoy!

In “Toby Merridan” writes:
>Could some kind soul demonstrate the use of void pointers for me:

>example: void * Pnothing

>When would you youse these?

Seldom, if ever, except as function parameters.

Void pointers have the nice property that they are *automatically*
converted to/from whatever pointer type is required by the context,
so you don’t need to put casts in your code (casts are generally
considered evil in C, especially pointer casts).

Consider malloc(), that returns a void pointer and free(), that takes
a void pointer as argument:

#include

int *array = malloc(100 * sizeof(int));

free(array);

In the absence of the type void, malloc() would have to return a char
pointer and free() would expect a pointer to char, so the above code
would have to look like this:

int *array = (int *)malloc(100 * sizeof(int));

free((char *)array);

Now, imagine that you want to write your own function, that computes
the sum of all the bytes in an object whose address and size are
passed as arguments:

unsigned long bytesum(void *addr, size_t n)
{
unsigned long sum = 0;
unsigned char *p = addr;

for (; n > 0; n–) sum += *p++;
return sum;
}

To get the sum of the bytes of foo, you use bytesum(&foo, sizeof foo),
no matter what is the type of foo.

Note that the addr parameter of the function is pretty useless as such:
it can’t be dereferenced or directly involved in any pointer arithmetic.
This is why the function is using p instead, whose type is adequate for
a byte pointer. If I made the parameter addr of type pointer to unsigned
char, the correct function call would be:

bytesum((unsigned char *)&foo, sizeof foo)

which is clumsier than in my actual implementation.

So, basically void pointers are not adding any new functionality to the
language (anything you can do with a void pointer can also be done with
a pointer to char), but they make your life as a programmer easier, by
removing the need for casts.
Dan

Dan Pop
DESY Zeuthen, RZ group
Email: Dan. …@ifh.de

Read Full Post »

  1. Create the start-up script for hamachi, as shown below. Remember to change the user name "your name" (line 5) to your system user name in Ubuntu (or your linux distribution). Call the script "hamachi."

    #!/bin/bash
    ###################################
    ### Start-up script for Hamachi ###
    ###################################
    USER=your name
    case "$1" in
    start)
    /sbin/tuncfg
    /bin/su – $USER -c "hamachi start"
    ;;
    stop)
    /bin/su – $USER -c "hamachi stop"
    ;;
    restart|force-reload)
    /bin/su – $USER -c "hamachi start"
    /bin/su – $USER -c "hamachi stop"
    ;;
    *)
    exit 1
    ;;
    esac
    exit 0

  2. Make the script executable:

    chmod +x hamachi

  3. Move the script to /etc/init.d/ directory:

    sudo mv hamachi /etc/init.d

  4. Finally, link the script to the appropriate run-level for booting up the system:

    sudo ln -s /etc/init.d/hamachi /etc/rc2.d/S99hamachi

    sudo ln -s /etc/init.d/hamachi /etc/rc2.d/K99hamachi

    It seems 2 is the default level for Debian and Ubuntu. In most other distribution, it is 5.

  5. Finally, reboot your system and Hamachi will be automatically loaded and connected to the server.
    Here's the original post.

Read Full Post »

[ZT]Using SCIM in English Ubuntu

If you are having problem opening SCIM in English Ubuntu, then follow the instruction below.

1) install the following packages:
* scim
* scim-chinese
* scim-config-socket
* scim-frontend-socket
* scim-gtk2-immodule
* scim-server-socket
* scim-tables-zh (option)
* xfonts-intl-chinese
* xfonts-intl-chinese-big
* ttf-arphic-gbsn00lp
* ttf-arphic-gkai00mp
* ttf-arphic-bkai00mp
* ttf-arphic-bsmi00lp

accept all dependencies.

2) System -> Preferences -> Sessions

Startup Programs Tab -> Add Button
Startup Command: scim -d
Order: 80

3) Restart Gnome: CTRL+ALT+SUPPR

4) Open any software and press CTRL+SPACE to activate chinese input
——————————————————————————————————

IF not working you can as an alternative replace step 2) as follow:

2) alt+F2, gedit

type these few lines:
scim -d export
XMODIFIERS=@im=scim

export GTK_IM_MODULE=scim

gnome-session

Save them under your home directory name: .xsession

Then open a terminal and type:
$chmod +x .xsession

Read Full Post »

rate table

#define PHY802_11b__1M  0
#define PHY802_11b__2M  1
#define PHY802_11b__6M  2
#define PHY802_11b_11M  3

Read Full Post »

sudo fontconfig-voodoo -f -s zh_CN,这个是专门针对CJK语系的解决方案,说到底,就是不再像以前自己修改fonts.conf了,命令执行以后把 /usr/share/language-selector/fontconfig/zh_CN,链接到/etc/fonts/language- selector.conf这个文件(安装英文版后没有这个文件)

post updated. The older method is not used. This one is really simple. See the original post here.

Read Full Post »

I tried to install It++ on Ubuntu this weekend. It’s not as smooth as what I experienced last time on cygwin. First it can’t pass make check — many items failed the test. I saw from configure that several required math libs are missing. I guess they are not default libs for the system. Fortunately the Ubuntu package manager is friendly enough and I didn’t spent too much time on figuring out how to install these libs.

After I successfulling installing itpp, I run a simple BPSK test to check if it works. It passed compilation but failed running. It displayed that itpp lib is missing. I then checked on itpp forum and found that it’s something related to shared library. Itpp installs itself as shared library in Linux by default, while as static library in windows by default (cygwin is ?). So as a matter of fact, my testing program is built against shared library, which needs to specify the lib path while running the program, by setting the env. LD_LIBRARY_PATH as the directory of required shared lib.

I did have my program run via this method. I also found that by ./configure –disable-shared or ./configure –enable-static itpp can be built as static library. After rebuilding, I had both libitpp.so and libitpp.a in the lib dir.. However, I recompiled my program and wanted to verify it’s linked against static lib. The test failed again as the same situation as before.

I found the reason in advanced linux programming that when the linker sound both .so and .a, it prefers .so by default. One can let the linker to use static libs by passing -static option to the linker.

Read Full Post »

About the pointer again

I mentioned the usage of pointer to pointer some weeks before. Now I have something more to explain.

First, whenever you initialize a pointer, initialize it with NULL, unless other things can be assigned to. For example, initialize a pointer to a linked list:
struct Node* pLink = NULL;

This means a NULL (or 0) is assigned to pLink. It doesn’t point to anything for now. When you need it to point to something:
pLink = head; // point to the head of the list

It is simple to return a pointer as a RETURN value of a function, like:
struct Node* func() {
struct Node* p = NULL;
if () {
// … do something
return p;
else {
return NULL;
}
}

It’s a little more tricky if you want to have it returned as a parameter of a function. This is where you’ll need a pointer to pointer:
struct Node* p = NULL;
// if you want to modify p in func(),
// pass p’s address as a parameter:
func(…, &p);
// func()’s definition:
void func(…, struct Node** pp) {

// do things, change *pp
}

Because you want to modify a pointer (not what it points to!), it’s natural to have its address (&p) passed as a parameter, and change the value of what the address points to (*pp). So, this is an other way that a pointer to pointer is employed. Enjoy!

For more information, please see here.

Read Full Post »

I found that folding in Vim is really easy and useful. On opening a file of a known type, simply type “:set foldmethod=syntax” to enable folding by syntax.

With folding, use “zi” to switch between fold/extend. It’s cool. zo/zc to open/close a fold. zr/zm to fold reduce/fold more.

After syntax folding, my codes looks like this:

My vimrc files can be downloaded here.

Read Full Post »

Two interesting webpage

Search "Unix Programming" you will find it: Dave Marshall's Home Page. He put lots of course ware on his pages. One interesting course ware is Programming in C. It introduces unix programming in C, some advanced topics for me. I'll go through it when I have time. Another course ware is about Perl: Practical Perl Programming . Check them out!

Read Full Post »

replacing in Vim

type: “:help substitute”, then press ctrl+D, to select help topic you want.

Usage: :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
useful ranges:
%: the entire file
useful flags:
g: replace all occurrence in the same line c: confirm before replacement

An example:
:%s/\<four\>/4/g
:%s/\<four\>/4/gc

Read Full Post »

Older Posts »