step 1, # apt-get install smbfs winbind
step 2, add following line to /etc/fstab
//{ip}/{path on window} /{mount point path} cifs guest,rw
step 3, # mount -a
2009年12月19日星期六
2009年8月14日星期五
container_of, offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
This marco is very useful, it will find a pointer pointing to the containner address.
I just use it for a long time, but don't know how it works, today I am going to check it out.
The first line find a pointer __mptr pointer to the member,
second line use __mptr to substract the offset the member's offset in the container.
I like the following trick,
((TYPE *)0)->MEMBER
Cast a '0' to the containter structure pointer,
->MEMBER , will give the offset accordingly.
The marco are inside kernel.h and stddef.h
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
This marco is very useful, it will find a pointer pointing to the containner address.
I just use it for a long time, but don't know how it works, today I am going to check it out.
The first line find a pointer __mptr pointer to the member,
second line use __mptr to substract the offset the member's offset in the container.
I like the following trick,
((TYPE *)0)->MEMBER
Cast a '0' to the containter structure pointer,
->MEMBER , will give the offset accordingly.
The marco are inside kernel.h and stddef.h
2009年8月10日星期一
setup my basic ubuntu
download latest ubuntu ISO image
$sudo passwd root
#apt-get install samba
edit /etc/samba/smb.conf
#smbpasswd -a
#apt-get install nfs-common
#apt-get install nfs-kernel-server
add /mnt/iso *(ro,sync) to /etc/exports
#apt-get install subversion
#apt-set install ssh
#apt-get install build-essential
#apt-get install libncurses5-dev
#apt-get install tofrodos
#apt-get install xinetd tftpd tftp
/etc/xinetd.d/tftp
service tftp
{
protocol = udp
port = 69
socket_type = dgram
wait = yes
user = nobody
server = /usr/sbin/in.tftpd
server_args = /tftpboot
disable = no
}
$sudo passwd root
#apt-get install samba
edit /etc/samba/smb.conf
#smbpasswd -a
#apt-get install nfs-common
#apt-get install nfs-kernel-server
add /mnt/iso *(ro,sync) to /etc/exports
#apt-get install subversion
#apt-set install ssh
#apt-get install build-essential
#apt-get install libncurses5-dev
#apt-get install tofrodos
#apt-get install xinetd tftpd tftp
/etc/xinetd.d/tftp
service tftp
{
protocol = udp
port = 69
socket_type = dgram
wait = yes
user = nobody
server = /usr/sbin/in.tftpd
server_args = /tftpboot
disable = no
}
2009年8月5日星期三
color the shell
Put this in the home directory ~/.bashrc
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "`dircolors -b $DIR_COLORS`"
alias ls='ls --color=auto'
fi
Create a file .dircolors to include the following, where I have to set
DIR 07;31;47, which means, reverse, background color is red, text color is white.
Then it is easy to differenate a directory or normal file.

# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the --color option.
# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.
# Below, there should be one TERM entry for each termtype that is colorizable
TERM linux
TERM linux-c
TERM mach-color
TERM console
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM dtterm
TERM xterm
TERM xterm-color
TERM xterm-debian
TERM rxvt
TERM screen
TERM screen-w
TERM vt100
TERM Eterm
# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00 # global default, although everything should be something.
FILE 00 # normal file
DIR 07;31;47 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
# numerical value, the color is as for the file pointed to.)
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file
# This is for files with execute permission:
EXEC 01;32
# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
.tar 01;31 # archives or compressed (bright red)
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z 01;31
.Z 01;31
.gz 01;31
.bz2 01;31
.deb 01;31
.rpm 01;31
.jar 01;31
# image formats
.jpg 01;35
.jpeg 01;35
.gif 01;35
.bmp 01;35
.pbm 01;35
.pgm 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.tiff 01;35
.png 01;35
.mpg 01;35
.mpeg 01;35
.avi 01;35
.fli 01;35
.gl 01;35
.dl 01;35
.xcf 01;35
.xwd 01;35
# audio formats
.ogg 01;35
.mp3 01;35
.wav 01;35
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "`dircolors -b $DIR_COLORS`"
alias ls='ls --color=auto'
fi
Create a file .dircolors to include the following, where I have to set
DIR 07;31;47, which means, reverse, background color is red, text color is white.
Then it is easy to differenate a directory or normal file.
# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the --color option.
# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.
# Below, there should be one TERM entry for each termtype that is colorizable
TERM linux
TERM linux-c
TERM mach-color
TERM console
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM dtterm
TERM xterm
TERM xterm-color
TERM xterm-debian
TERM rxvt
TERM screen
TERM screen-w
TERM vt100
TERM Eterm
# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00 # global default, although everything should be something.
FILE 00 # normal file
DIR 07;31;47 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
# numerical value, the color is as for the file pointed to.)
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file
# This is for files with execute permission:
EXEC 01;32
# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
.tar 01;31 # archives or compressed (bright red)
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z 01;31
.Z 01;31
.gz 01;31
.bz2 01;31
.deb 01;31
.rpm 01;31
.jar 01;31
# image formats
.jpg 01;35
.jpeg 01;35
.gif 01;35
.bmp 01;35
.pbm 01;35
.pgm 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.tiff 01;35
.png 01;35
.mpg 01;35
.mpeg 01;35
.avi 01;35
.fli 01;35
.gl 01;35
.dl 01;35
.xcf 01;35
.xwd 01;35
# audio formats
.ogg 01;35
.mp3 01;35
.wav 01;35
2009年7月13日星期一
2009年7月2日星期四
2009年6月25日星期四
fls 2.6.4 bug
Do you discover any problem in the following code?
c->space_bits = fls(c->leb_size) - 3;
c->lpt_lnum_bits = fls(c->lpt_lebs);
c->lpt_offs_bits = fls(c->leb_size - 1);
c->lpt_spc_bits = fls(c->leb_size);
n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
c->pcnt_bits = fls(n - 1)
This bug is inside the inline assmeble marco fls, you can not discover it in the C-level.
Let see the fls marco,
2.6.4 include/asm-arm/bitops.h
#define fls(x) \
( __builtin_constant_p(x) ? generic_fls(x) : \
({ int __r; asm("clz%?\t%0, %1" : "=r"(__r) : "r"(x)); 32-__r; }) )
clz, count the number of leading zero,
%0, result register
%1, operand register
"=r", output to C varaible
"r", input from C varaible
Since the clz instruction may alter the code register, so the following divion may be incorrect. There is a fix in 2.6.2x, which put "cc" in the Clobber List, it tell compiler there is a chance the cc will be changed by the inline asemble code.
2.6.2x arm/include/asm/bitops.h
static inline int fls(int x)
{
int ret;
if (__builtin_constant_p(x))
return constant_fls(x);
asm("clz\t%0, %1" : "=r" (ret) : "r" (x) : "cc");
ret = 32 - ret;
return ret;
}
c->space_bits = fls(c->leb_size) - 3;
c->lpt_lnum_bits = fls(c->lpt_lebs);
c->lpt_offs_bits = fls(c->leb_size - 1);
c->lpt_spc_bits = fls(c->leb_size);
n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
c->pcnt_bits = fls(n - 1)
This bug is inside the inline assmeble marco fls, you can not discover it in the C-level.
Let see the fls marco,
2.6.4 include/asm-arm/bitops.h
#define fls(x) \
( __builtin_constant_p(x) ? generic_fls(x) : \
({ int __r; asm("clz%?\t%0, %1" : "=r"(__r) : "r"(x)); 32-__r; }) )
clz, count the number of leading zero,
%0, result register
%1, operand register
"=r", output to C varaible
"r", input from C varaible
Since the clz instruction may alter the code register, so the following divion may be incorrect. There is a fix in 2.6.2x, which put "cc" in the Clobber List, it tell compiler there is a chance the cc will be changed by the inline asemble code.
2.6.2x arm/include/asm/bitops.h
static inline int fls(int x)
{
int ret;
if (__builtin_constant_p(x))
return constant_fls(x);
asm("clz\t%0, %1" : "=r" (ret) : "r" (x) : "cc");
ret = 32 - ret;
return ret;
}
2009年2月15日星期日
bash PS1
linux的世界何其大...我可能只講出一些皮毛=_=
今次談一談我使用bash時發覺如current directory 有太多層,
$cd directory1/directory2/directory3/directory4......directoryn
那麼command line 內的prompt string便顯示了所有的directoy,
directory1/directory2/directory3/directory4...directoryn$
結果可以打字的地方不足夠,經常會跳到下一行,非常不方便,請看下圖。

其實prompt string只顯示最後一個diretoryn 就足夠了。
這個簡單方法是一位高手教我的,在此先行謝過。
原來bash 裡有一個envirnoment variable 空制 prompt string, 那個就是
PS1,
只要在把PS1="\u@\h:\W\$"
有的小楷 w 改為'W' 便可以了,
也可以在$home/.bashrc 修改,那麼以後就不用改了,prompt string還有很多玩法,
有興趣可以去找找。
English version:
The prompt string in bash is too long for me, if the current directoy is very deep in the file system.
$cd directory1/directory2/directory3/directory4......directoryn
directory1/directory2/directory3/directory4...directoryn$
In this case, a single line is not enough space of me to type the command.
To resolve this, there is an envirnoment varible PS1 in bash,
just modify it from 'w' to 'W' , then it is ok.
Save it in home directory $home/.bashrc, then it is effective every time when you login.
今次談一談我使用bash時發覺如current directory 有太多層,
$cd directory1/directory2/directory3/directory4......directoryn
那麼command line 內的prompt string便顯示了所有的directoy,
directory1/directory2/directory3/directory4...directoryn$
結果可以打字的地方不足夠,經常會跳到下一行,非常不方便,請看下圖。
其實prompt string只顯示最後一個diretoryn 就足夠了。
這個簡單方法是一位高手教我的,在此先行謝過。
原來bash 裡有一個envirnoment variable 空制 prompt string, 那個就是
PS1,
只要在把PS1="\u@\h:\W\$"
有的小楷 w 改為'W' 便可以了,
也可以在$home/.bashrc 修改,那麼以後就不用改了,prompt string還有很多玩法,
有興趣可以去找找。
6.9 Controlling the Prompt
The value of the variable PROMPT_COMMAND is examined just before Bash prints each primary prompt. If PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.
http://www.gnu.org/software/bash/manual/bashref.htmlEnglish version:
The prompt string in bash is too long for me, if the current directoy is very deep in the file system.
$cd directory1/directory2/directory3/directory4......directoryn
directory1/directory2/directory3/directory4...directoryn$
In this case, a single line is not enough space of me to type the command.
To resolve this, there is an envirnoment varible PS1 in bash,
just modify it from 'w' to 'W' , then it is ok.
Save it in home directory $home/.bashrc, then it is effective every time when you login.
2009年1月7日星期三
linux software installation from source
There is a lot of package such as .deb, rpm in linux, but I prefer/have to install the software from source, since I can modify the source in the way I want, and I have also need to cross compile for embedded system.
The most generic way to install software from source, and it is easy
1. #./configure
./configure will generate makefile, header file, marco...etc base on the software installed
in the file system
2. #make
compile the source to executable binary with generated files
3.#make install
copy the executable, header file, library to the specific location in the file system.
The most common problem is in the configure and make process.
During configure, it may require another sw to be installed in the file system, but it fails.
The reason may be,
1. the sw is not yet install in the file system
2. the sw is intalled in the file system, but it can't be found during the ./configure
Solution to the first case is easy, you just get the source on the web, and do the ./configure,
make && make install, if no error, then it is done.
Solution to second case is a little bit complicated, since there could be a lot of reason that
./configure can't file the sw which is already install in the file system.m
For easy discussion, let assume software B require software A to be install in the file system.
1. sw A is not install in the default path for example, the default path may be /usr/a, however sw A is install in /home/usera/usr/a.
In this case, you need to check how to tell sw B during ./configure where is the sw A installed. usually an option "--with--sw_a=/path_to_sw_a"
Actually what is the path really does?
1. It provides header file to be included
2. It provides library for static or dynmaic linkage.
This steps can be done in one line,
# LDFLAGS=-Lpath_to_sw_a/?/lib CFLAGS=-Ipath_to_sw_a/?/include ./configure --with_sw_a=/path_to_sw_a
The most generic way to install software from source, and it is easy
1. #./configure
./configure will generate makefile, header file, marco...etc base on the software installed
in the file system
2. #make
compile the source to executable binary with generated files
3.#make install
copy the executable, header file, library to the specific location in the file system.
The most common problem is in the configure and make process.
During configure, it may require another sw to be installed in the file system, but it fails.
The reason may be,
1. the sw is not yet install in the file system
2. the sw is intalled in the file system, but it can't be found during the ./configure
Solution to the first case is easy, you just get the source on the web, and do the ./configure,
make && make install, if no error, then it is done.
Solution to second case is a little bit complicated, since there could be a lot of reason that
./configure can't file the sw which is already install in the file system.m
For easy discussion, let assume software B require software A to be install in the file system.
1. sw A is not install in the default path for example, the default path may be /usr/a, however sw A is install in /home/usera/usr/a.
In this case, you need to check how to tell sw B during ./configure where is the sw A installed. usually an option "--with--sw_a=/path_to_sw_a"
Actually what is the path really does?
1. It provides header file to be included
2. It provides library for static or dynmaic linkage.
This steps can be done in one line,
# LDFLAGS=-Lpath_to_sw_a/?/lib CFLAGS=-Ipath_to_sw_a/?/include ./configure --with_sw_a=/path_to_sw_a
2008年8月5日星期二
va_arg 令我平靜下來 ( update)
va_arg是printf 的重要techqniue 就是variable number and type of argument。
即係話佢個function 不限制arugment既數目(最少一個), 也不限制arugment既type ( int,char*...)
printf好重要,無左個都唔知點debug..
廢話少說係linux kernal 抄個printf source code 出來睇睇!
int
printf(char *fmt, ...)
{
va_list args;
int n;
va_start(args, fmt);
n = vsprintf(sprint_buf, fmt, args);
va_end(args);
write(stdout, sprint_buf, n);
return n;
}
variable argument既語法就係"..."
int printf(char *fmt, ...);
跟住要認識以下既define,
#define _AUPBND (sizeof (acpi_native_int) - 1)
#define _ADNBND (sizeof (acpi_native_int) - 1)
typedef char *va_list;
#define _bnd(X, bnd) (((sizeof (X)) + (bnd)) & (~(bnd)))
#define va_arg(ap, T) (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND))))
#define va_end(ap) (void) 0
#define va_start(ap, A) (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))
_bnd round up X to multiple of bnd
va_arg , get the next argument
va_start, set ap to first agrument
看了這3個marco 還是不很了解?其實可以簡單一點吧..因為va_arg(ap,t)的t 是取決於stack 是16 bit/32bit..這在每個platform是故定的。
第一個fmt argument的作用是說明... 究竟代表了多少arugment, argument是什麼 type。
說了半天還沒有說print如何用... 很累了...有興趣去prom.c 看一看吧!有空再討論!
aa:abcdefg aa:x10dc4 &aa:x10dc4
bb:123 bb:x10dcc &bb:x10dcc
cc:100 &cc:x10de0
dd:10 &dd:x10de6
ee:f &ee:x10de4
ff:x0 &ff:x10ddc
&type_a:x10dec
fmt:x8c14 &fmt:xbef78cd8
args:xbef78cdc &args:xbef78cbc
Offset|00000000 00000004 00000008 0000000C
000000|00008C14 00008CA4 00010DCC 00000064
000010|0000000A 00000066 0000000B 0000000C
000020|00000061 BEF78D24 00000001 00000000
000030|00000000 BEF78D18 40032E44
num_var_arg:0 args:xbef78ce0 &args:xbef78cbc
uuuuuuuu x8ca4
num_var_arg:1 args:xbef78ce4 &args:xbef78cbc
123 x10dcc
num_var_arg:2 args:xbef78ce8 &args:xbef78cbc
100 xbef78cb8
num_var_arg:3 args:xbef78cec &args:xbef78cbc
10 xbef78cb8
num_var_arg:4 args:xbef78cf0 &args:xbef78cbc
f xbef78cb7
num_var_arg:5 args:xbef78cfc &args:xbef78cbc
type_tmp.x 11
這個是function call,
var_arg_fc(arg_1,"uuuuuuuu\0",bb,cc,dd,ee,typ_a);
TYP_A is
typedef struct
{
int x;
int y;
char xx;
} TYPEA;
這個例子充份解釋了,pass by value, pass by reference, stack usage.
看懂了身體更健康,看不懂也不要緊!沒什麼相干的。
即係話佢個function 不限制arugment既數目(最少一個), 也不限制arugment既type ( int,char*...)
printf好重要,無左個都唔知點debug..
廢話少說係linux kernal 抄個printf source code 出來睇睇!
int
printf(char *fmt, ...)
{
va_list args;
int n;
va_start(args, fmt);
n = vsprintf(sprint_buf, fmt, args);
va_end(args);
write(stdout, sprint_buf, n);
return n;
}
variable argument既語法就係"..."
int printf(char *fmt, ...);
跟住要認識以下既define,
#define _AUPBND (sizeof (acpi_native_int) - 1)
#define _ADNBND (sizeof (acpi_native_int) - 1)
typedef char *va_list;
#define _bnd(X, bnd) (((sizeof (X)) + (bnd)) & (~(bnd)))
#define va_arg(ap, T) (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND))))
#define va_end(ap) (void) 0
#define va_start(ap, A) (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))
_bnd round up X to multiple of bnd
va_arg , get the next argument
va_start, set ap to first agrument
看了這3個marco 還是不很了解?其實可以簡單一點吧..因為va_arg(ap,t)的t 是取決於stack 是16 bit/32bit..這在每個platform是故定的。
第一個fmt argument的作用是說明... 究竟代表了多少arugment, argument是什麼 type。
說了半天還沒有說print如何用... 很累了...有興趣去prom.c 看一看吧!有空再討論!
aa:abcdefg aa:x10dc4 &aa:x10dc4
bb:123 bb:x10dcc &bb:x10dcc
cc:100 &cc:x10de0
dd:10 &dd:x10de6
ee:f &ee:x10de4
ff:x0 &ff:x10ddc
&type_a:x10dec
fmt:x8c14 &fmt:xbef78cd8
args:xbef78cdc &args:xbef78cbc
Offset|00000000 00000004 00000008 0000000C
000000|00008C14 00008CA4 00010DCC 00000064
000010|0000000A 00000066 0000000B 0000000C
000020|00000061 BEF78D24 00000001 00000000
000030|00000000 BEF78D18 40032E44
num_var_arg:0 args:xbef78ce0 &args:xbef78cbc
uuuuuuuu x8ca4
num_var_arg:1 args:xbef78ce4 &args:xbef78cbc
123 x10dcc
num_var_arg:2 args:xbef78ce8 &args:xbef78cbc
100 xbef78cb8
num_var_arg:3 args:xbef78cec &args:xbef78cbc
10 xbef78cb8
num_var_arg:4 args:xbef78cf0 &args:xbef78cbc
f xbef78cb7
num_var_arg:5 args:xbef78cfc &args:xbef78cbc
type_tmp.x 11
這個是function call,
var_arg_fc(arg_1,"uuuuuuuu\0",bb,cc,dd,ee,typ_a);
TYP_A is
typedef struct
{
int x;
int y;
char xx;
} TYPEA;
這個例子充份解釋了,pass by value, pass by reference, stack usage.
看懂了身體更健康,看不懂也不要緊!沒什麼相干的。
2008年7月18日星期五
2008年5月19日星期一
function declartion , function definition
Do you know what is different between function declaration and function definition?
If not, please google it first.
Some programmer is lazy to ignore the importances of function declaration, they may
neither not include header file or extern to declare the function before use.
For example, there is a function definition in a.c
void aaa(void)
{
printf("aaaa\n");
}
corresponding declaration
void aaa(void);
in a.h header file.
They will call aaa in any x.c xxx.c , without include a.h or extern void aaa(void) before use.
The consequence will be nothing, or system crash depending how you invoke aaa,
since all the following will be valid during compile time.
aaa(b);
aaa(b,b,b);
There is an example warning during compile time.
warning C4013: 'aaa' undefined; assuming extern returning int
Good practice
1. declare function before use, include header or extern
2. check all the warning
Exercise for you
1. Check my statement correctness, under what situation the system will reset and why.
If not, please google it first.
Some programmer is lazy to ignore the importances of function declaration, they may
neither not include header file or extern to declare the function before use.
For example, there is a function definition in a.c
void aaa(void)
{
printf("aaaa\n");
}
corresponding declaration
void aaa(void);
in a.h header file.
They will call aaa in any x.c xxx.c , without include a.h or extern void aaa(void) before use.
The consequence will be nothing, or system crash depending how you invoke aaa,
since all the following will be valid during compile time.
aaa(b);
aaa(b,b,b);
There is an example warning during compile time.
warning C4013: 'aaa' undefined; assuming extern returning int
Good practice
1. declare function before use, include header or extern
2. check all the warning
Exercise for you
1. Check my statement correctness, under what situation the system will reset and why.
2008年5月17日星期六
編程趣題
不計"開blog了"的文章,最多回應便是有c語言的那一篇, 哈哈,
再來說清楚一下吧,
int a[10]; //a is an array of ten integer
int* b[10];//b is an array of ten integer pointer
int (*c)[10];//c is a pointer to an array of the integer
int (*(*d)(char))[]; //What is d ?
先開估︰
d is a pointer to a function which take a char argument and returning a pointer to an array of integer.
長長的答案,應該沒有錯吧?請各位親見下手寫幾行code去証實一下我的答案!
親自發掘答案你將獲益良多!
不過如果反應熱烈,我也會寫幾行code出來說明一下當中拆解的logic。
可是自己的功力尚淺, 只是藉著多點討論,交流,自己也可以進步一下!如果大家有什麼趣題,
觀迎post出來大家一起研究!
好..再來一題....
q2. write a function to calculate the number of bit '0' in the argument
for example,
input =0xFFF0, it should return 4
input =0xFF00, it should return 8
I welcome all kind of language, c/c++, java, asm...etc...
再來說清楚一下吧,
int a[10]; //a is an array of ten integer
int* b[10];//b is an array of ten integer pointer
int (*c)[10];//c is a pointer to an array of the integer
int (*(*d)(char))[]; //What is d ?
先開估︰
d is a pointer to a function which take a char argument and returning a pointer to an array of integer.
長長的答案,應該沒有錯吧?請各位親見下手寫幾行code去証實一下我的答案!
親自發掘答案你將獲益良多!
不過如果反應熱烈,我也會寫幾行code出來說明一下當中拆解的logic。
可是自己的功力尚淺, 只是藉著多點討論,交流,自己也可以進步一下!如果大家有什麼趣題,
觀迎post出來大家一起研究!
好..再來一題....
q2. write a function to calculate the number of bit '0' in the argument
for example,
input =0xFFF0, it should return 4
input =0xFF00, it should return 8
I welcome all kind of language, c/c++, java, asm...etc...
2008年5月16日星期五
"有錢"工程師
小弟曾經和印度,美國,加拿大的軟件工程師合作.....有出色的,有平凡的, 合作起來尚算順利,
但當中有個"有錢"工程師, 實在令小弟吃了不少苦頭!
1. 在幾萬到十幾萬行的code中,留下一些害蟲!
2.在幾萬到十幾萬行的code中,他只了解幾百行
3.暗地裡可隨意更改api, 他不會告訴我,
4.我寫email給他, 他可以當唔知,當睇唔到,開會又唔出席, 次次佢上司都同我講一句
"我會叫佢reply你email", 下次開會佢又講呢句, 一次又一次.....拖足成個月先reply,
個時我都死掂左啦!
其實一年前己寫信給他上司,狂插..估不到到了今天還可以繼續害我!
算吧, 說了半天廢話, 給大家一個c語言的簡單問題,
但當中有個"有錢"工程師, 實在令小弟吃了不少苦頭!
1. 在幾萬到十幾萬行的code中,留下一些害蟲!
2.在幾萬到十幾萬行的code中,他只了解幾百行
3.暗地裡可隨意更改api, 他不會告訴我,
4.我寫email給他, 他可以當唔知,當睇唔到,開會又唔出席, 次次佢上司都同我講一句
"我會叫佢reply你email", 下次開會佢又講呢句, 一次又一次.....拖足成個月先reply,
個時我都死掂左啦!
其實一年前己寫信給他上司,狂插..估不到到了今天還可以繼續害我!
算吧, 說了半天廢話, 給大家一個c語言的簡單問題,
int a [10];
int *b[10];
int (*c)[10];
你知道a,b,c的分別嗎?
訂閱:
文章 (Atom)