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