Sunday, March 29, 2009

struct with bit limit

struct data {
int data_1:4;
int data_2:4;
int data_3;
};

int main(int argc, char** argv){
struct data my_data = {
.data_1 = 0;
.data_2 = 1;
.data_3 = 1;
};

my_data.data_2 = my_data.data_2 + 15;
my_data.data_3 = my_data.data_3 + 15;

printf("struct size = %d.\n", sizeof(struct data) );
printf("data_1 = %d.\n", my_data.data_1);
printf("data_2 = %d.\n", my_data.data_2);
printf("data_3 = %d.\n", my_data.data_3);
}

output:
struct size = 8
data_1 = 0
data_2 = 0
data_3 = 16

Saturday, March 21, 2009

Difference between "my" and "local" keyword

Please refer below two examples:

1.
$a = 3.14159;
{
local $a = 3;
print "In block, \$a = $a\n";
print "In block, \$::a = $::a\n";
}
print "Outside block, \$a = $a\n";
print "Outside block, \$::a = $::a\n";

# This outputs
In block, $a = 3
In block, $::a = 3
Outside block, $a = 3.14159
Outside block, $::a = 3.14159

2.
$a = 3.14159;
{
my $a = 3;
print "In block, \$a = $a\n";
print "In block, \$::a = $::a\n";
}
print "Outside block, \$a = $a\n";
print "Outside block, \$::a = $::a\n";

# This outputs
In block, $a = 3
In block, $::a = 3.14159
Outside block, $a = 3.14159
Outside block, $::a = 3.14159

"local" will make global variable with the same name "invisible" temporarily,
"my" will create a new memory space and you can distinguish local and global scope variable.

How variable definition affect GDB access policy?

Consider this defnition:

int x[] = {5,4,3,2,1};
char y[] = {9,8,7,6,5};

Because sizeof(*x) = 4 and sizeof(*y) = 1
GDB will calculate result of x with alignment value of 4 and result of y with alignment value of 1,
therefore we will have such output in GDB:

p (int)*(x+1) --> 4
p (char)*(y+1) --> 8

Eventually, x will be added by 4 and y will be added by 1 then do type casting.

Sunday, March 15, 2009

Linux in Virtualbox with Windows XP as host system

1. Software we need:

Virtualbox to install Linux and X server running on Windows XP to enable X11 forwarding

Pietty:
http://www.csie.ntu.edu.tw/~piaip/pietty/

Virtualbox:
http://www.virtualbox.org/

Xming:
http://sourceforge.net/projects/xming

Linux (Ubuntu, in this example):
http://www.ubuntu.com/

3. Install all software and Linux system

4. In Virtualbox control window:
a. add a new netwotk adapter with "HOST interface" option

5. Login Linux system and get local IP address binding on "HOST interface" network adapter using "ifconfig" command

6. Make sure that SSH server deamon is listening on specific port

7. Login Linux system using pietty by guest IP address and SSH protocol

8. Modify .bashrc as below:
alias v='gvim -font "Monospace 16"'
alias c='clear'
alias l='ls --color'
alias cl='clear; ls -la'
alias gcc='gcc -Wall'
alias h='history'

You will get "small" font size on gvim probably if you do not set fontsize option

9. Modify .vimrc as below:
behave mswin "enable MS windows hbehavior
syntax on "enable syntax
colors desert "color scheme setting
filetype on "enable file type
set number "enable line number
set tabstop=4 "set soft tab space
set cindent "set auto indent
set ignorecase "set case-insensitive search
set hls "highlight search result
set ruler "show current cursor position
set mousef "focus follows mouse
set nocompatible "enable backspace
set backspace=indent,eol,start "enable backspace
nnoremap q :quit "quit by typing 'q' only

You can get more information at this --> http://www.vim.org/

10. Change pietty setting:
connection --> SSH --> X11
Enable X11 forwarding and set display location as "localhost:0.0"

11. Start Xming, login guest Linux system, try "xeyes &" to hceck X window forwarding

Sunday, April 15, 2007

cygwin header file bug?

剛剛試著在自己的NB上面編himax-cc
偶然間發現一個似乎是cygwin bug的敘述

問題出現在/usr/include/cygwin/types.h
其中
#ifndef __u_int8_t_defined
#define __u_int8_t_defined
typedef unsigned char u_int8_t;
#endif
#ifndef __u_int16_t_defined
#define __u_int16_t_defined
typedef __u_int16_t u_int16_t;
#endif
#ifndef __u_int32_t_defined
#define __u_int32_t_defined
typedef __u_int32_t u_int32_t;
#endif
#ifndef __u_int64_t_defined
#define __u_int64_t_defined
typedef __u_int64_t u_int64_t;
#endif

問題出現在__u_int16_t, __u_int32_t, __u_int64_t並未在之前被定義
所以compiler在跑這一段的時候還是不知道到底u_int16_t, u_int32_t, u_int64_t到底應該用什麼樣的type來組合
因此對照/usr/include/sys/types.h中的敘述
我試著把它改成如下

#ifndef __u_int8_t_defined
#define __u_int8_t_defined
typedef unsigned char u_int8_t;
#endif
#ifndef __u_int16_t_defined
#define __u_int16_t_defined
typedef unsigned short u_int16_t;
#endif
#ifndef __u_int32_t_defined
#define __u_int32_t_defined
typedef unsigned int u_int32_t;
#endif
#ifndef __u_int64_t_defined
#define __u_int64_t_defined
typedef unsigned long long u_int64_t;
#endif

就可以動了
不過我懷疑這是cygwin的一個bug

Thursday, December 21, 2006

HTML tag list 網站整理

HTML
http://www.w3schools.com/tags/ref_byfunc.asp

CSS
http://www.w3schools.com/css/css_syntax.asp