Thursday, January 29, 2009

does uninitialized variables have null or zero value?

If an uninitialized variable is local it will have an undefined value. Remember that local variables are allocated in the stack and while a space will be allocated for that variable in the stack, whatever value the memory have at the time of the allocation will be the initial value of that uninitialized variable. Try running below code and take note of variable "y"'s value.

void do_test_uninitialized_var()
{
int x, y;
for (x=0; x<5; x++) { y++; }
printf("Y[%d]\n", y);
return;
}

int main(int argc, char *argv[])
{
do_test_uninitialized_var();
return 0;
}


The following output results after several run:

c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208861771]
c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208206411]
c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208845387]
c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208575051]
c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208214603]

For variables declared in the heap (global variables/static local variables), they are usually initialized to zero.

int k; //globals are heaped

void do_test_uninitialized_var()
{
int x, y; //locals are stacked

printf("Y[%d] K[%d]\n", y, k);
for (x=0; x<5; x++) { y++; k++; }
printf("Y[%d] K[%d]\n", y, k);

return;
}

int main(int argc, char *argv[])
{
do_test_uninitialized_var();
return 0;
}

output:
c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208964176] K[0]
Y[-1208964171] K[5]
c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208546384] K[0]
Y[-1208546379] K[5]
c0r3dump@debian:~/workspace2/testUtils/Debug$ ./test_uninitialized_var
Y[-1208673360] K[0]
Y[-1208673355] K[5]



helpful linux utilities

apt
synaptic
ftp
telnet
vim editor
putty
eclipse cdt
sdl
gtk

useful linux commands

ls -ltr
df -aH
du -s [directory/filename]
chown -r [user:group] [directory/filename]
chmod ugo+rwx [filename]
ps -ef | grep [process name]

find . -name [filename]
lsmod
ipcs
ifconfig -a
netstat -nr
netstat -na
route add default gw [gateway address]
route delete default gw [gateway address]
time
apt -get update
dpkg -i --force-overwrite [package_name]
diff -r -N -c [file1] [file2] > [output_file]
addusr
addgroup
passwd
who
finger
export
od
id