Thursday, December 29, 2011

平淡的幸福

幸福是什么?真是个不好回答的问题,或许每个人根据自己的感受都有不同的答案吧。

老婆和孩子大老远地从北卡一路开车到我这里陪我过圣诞和新年,我很高兴。但是很奇怪,并没有欣喜若狂的感觉,是我太冷漠了吗?不知道。

但是今天下班回家的路上,一个人孤独地走在冷风中,想着如果程锦和贝贝没来,我回到家仍旧面对的是四面冰冷的墙,想着滋味都不好受。但是现在老婆孩子在这里,想到她们,心里就起了一点暖意,吹到身上的冷风也不那么难以忍受了。

也许幸福就是这样,拥有的时候平淡如水,并不会让你如癫似狂;但失去它的时候,你会感到莫名的痛苦。

Saturday, November 19, 2011

贝贝和蒲公英


蒲公英的英文单词是dandelion,很冷僻的一个词,以前从来没见过。

爸爸妈妈的照片



都是贝贝拍的,儿子是个好帮手啊。

Tuesday, November 15, 2011

double free is dangerous

Today, a double free of a pointer caused a crash in the program, which is hard to detect. One of my colleagues helped me pin down the problem and fixed it.

As we all know, if a block memory is allocated through malloc() or new(), we must remember to free() or delete() the pointer (pointing to the memory block allocated) later. And it is perfectly legal in C/C++ to free/delete a null pointer. So we usually don't bother to check whether the pointer is null before freeing it. And fewer programmers will set the pointer to null after the memory is freed.

Yet, this is a bad practice. Consider the following scenario:
---
char *p = (char *)malloc(1024);
... //some string operations involving p
free(p);
free(p);
---
Definitely, it will cause a core dump. Here is the reason: After the first free(), the memory pointed to by p is deallocated and reclaimed by the system. But p is not set to null yet. In this case, it is called a dangling pointer. What is points to is undetermined and unknown. When the second free() is run on the dangling pointer, the system is tring to free a block of memory not in use any more! The best result you can expect is a core dump.

The solution is simple. Just set the pointer to null after it is freed. Usually, we wrap it up in a macro.
---
#define SAFE_FREE(p)        if ((p) != NULL) do {free((p)); (p) = NULL;} while (0)
---
It is a common practice to wrap up multiple statements of a macro into a do {...} while (0) loop, with no terminating semicolon.This allows the macro to be used like a single statement in any location, such as the body of an if statement, while still allowing a semicolon to be placed after the macro invocation without creating a null statement.

References:
http://stackoverflow.com/questions/2468853/freeing-memory-twice
http://en.wikibooks.org/wiki/C_Programming/Common_practices

Monday, November 14, 2011

大楼前面

押犯人回来照相,犯人老大不高兴。

猴癞子

好像猴子还是不太乐意

猴子就是不配合,妈妈的。

在爸爸的工作间

正在玩MechQuest

张牙舞爪的猴子

带猴子在工作楼的一楼大厅照相

叫猴子骑上去,猴子不敢,唉。

猴子爱妈妈


爸爸爱猴子

怎么说也不敢骑上去,我们扶着也不行。

爸爸妈妈相亲相爱

Thursday, November 10, 2011

Torture

早上给猴子和猴妈打电话,问猴子想不想爸爸,猴妈说猴子不想,原因是“爸爸太爱我了,总是亲我,torture我”(猴子的原话),一笑。

真想这个可爱的猴子啊。在这以前,可以说没有一天跟我分开过。

下面这两张照片,是在从北卡到肯州的路上拍的,估计是在田纳西境内的某个加油站。



Saturday, November 5, 2011

童趣两则

1. 贝贝在玩笔记本电脑的时候,我过去把头靠着贝贝,问他:“贝贝,你离开爸爸,会难过吗?”,贝贝回答:“Easy过”,见我脸色迷惘,又补充说:“容易过”。

2. 给贝贝在YouTube上面找了一个U.S. Marine Corps的一次Combat Patrol的实拍录像,可是录像里面只见士兵们打枪,并没有见到他们的阿富汗对手。然后又见这些士兵们没完没了地说着什么,我和贝贝都没听明白,贝贝问他们为什么只talk不fight,我很赞同:“These sons of bitches should stop talking.” 猴子问:“What does bitch means?” 我回答:“female dog”,贝贝继续追问:“But there is no dog in the battle field.” 这下我彻底傻了。

昨晚可真不应该

为了一点小事向猴子发火,把猴子给骂哭了。猴子一开始还忍着,明明想哭,却强作无事,妈妈逗猴子说“你哭、你哭”的时候还强作笑脸。忘记是什么原因了,突然忍不住了,小嘴一咧,哭得好伤心。这点挺像我的。遇到伤心事,我小时候也会尽量忍住泪水,但到最后(经常是见到父母后)仍然会情不自禁地哭泣。

猴子一哭,弄得我心里也挺不痛快。还是要自我检讨一下,其实猴子也没做什么大的错事,就是我在与奶奶用skype聊天的时候,用脚在摇笔记本下面的桌子。当时我正好在和奶奶谈到工作的事情,猴子这么一搞,弄得我没控制住自己的情绪。以前比这大得多的事情我都能容忍,为什么这次不能?事后回想,还是把工作中的浮躁情绪带到家里了,并发泄到猴子身上了。实在是不应该,尤其是过几天猴子和猴妈就要离开猴爸了,疼还来不及,怎么能对孩子发火?尤其是因无关痛痒的事情,更是觉得自己无可理喻。

有的时候,真的很厌恶自己,觉得自己像是一坨屎。

Thursday, November 3, 2011

不知道为啥

在北卡的时候,虽然没有工作,但我心里一点也不痛苦(不知道是不是算没心没肺),感觉过得还是很快乐。快乐地带猴子、快乐地上网、快乐地读自己喜欢的书,过得很充实,从来也不会觉得无聊。虽然程锦偶尔发点脾气(因为我某件事做错了,老婆并不是不讲道理的人),也是过眼烟云,并不会放在心上很久。

但是不知为何,现在有工作了,却感到内心很软弱,也分不清楚是出于对工作的畏难情绪导致的,还是对即将离开的老婆孩子依依不舍,也许两者兼而有之吧。

人真是个奇怪的动物,有时候很坚强,另些时候又无比脆弱,真是矛盾的对立统一体。

喜欢读战争小说的我,对从战场上的老兵最珍惜家庭感到不解。总觉得经过了战争这个壮丽残酷的过程的人,怎么会对平淡的家庭生活那么感兴趣,那么看重?现在,我些许有点感悟。虽然我和一名战场上的老兵不可同日而语,但对于和亲人短暂的分别都感到难以忍受,就更不用说老兵了。士兵在战场上,每时每刻都有可能被流弹打死,对亲情的感觉就更为强烈,也必然更为珍惜。可能这也是电影Gladiator里面罗马皇帝Marcus Aurelius在大战结束之后问Maximus(Russel Crowe)想要什么样的奖赏,得到的回答却是“Home”。

陆游有句诗是这样的:“死去原知万事空,但悲不见九州同(下略)”。我的感想是:“死去原知万事空,但悲不见妻与子”。过于沉重了,其实也不到那个程度,只是感觉心里难受。

第二天上班,还是难受

不舒服的感觉很强烈。我在新加坡工作的时候还有免费Milo以及Nestle Coffee喝,到这里屁的饮料都没有,只有自来水,操!

工作方面,还在摸索code的过程中,还是有些艰难,可能是许久没有写code的缘故导致的,要努力啊。

下午雷哥给我打了个电话问我的情况,说着说着鼻子就有些发酸,又想哭了。想念朋友的关怀,害怕即将失去的的老婆孩子热炕头。

今天中午,我的Recruiter Marc带我去Aerotek补办一些手续,中途问我是否喜欢Lexington,为了不扫他的兴致,我违心地说喜欢。其实还是咱北卡的农村好啊,一点都不喜欢Lexington,虽然它是个城市。

Wednesday, November 2, 2011

今天是在Lexmark工作的第一天

这个公司外表感觉很光鲜,但是似乎里面很烂,工作环境很差,刚开始很多东西不懂,也不知道是不是在家闲了太久的缘故,反正脑袋感觉用得不太得劲。不过,感觉Lexmark的code结构很乱,不如Xerox的code架构简明清晰。

上班上到下午,想到今后要孤孤单单一个人在这鬼不生蛋的地方生活,不禁悲从中来,有点想哭的感觉。老婆真不容易,以前总是不能理解的,总以为自己在新加坡带猴子更为辛苦,其实还是孤独最可怕,程锦能独自熬过三年,不简单。

庄子曾经说过:“子非鱼,安知鱼之乐?”同样,只有在有相同经历的基础上,才能有相似的痛苦。

上班第一天,就有想撂挑子,和老婆孩子一起回家、离开这鬼地方的冲动。唉……

Friday, June 10, 2011

EFI interview - 6

最后一个进来的是一个技术主管的Director,问了我几个理论问题,一个都没答上来。

1. TCP协议和UDP协议的区别?
还真不知道,以前做工作的时候是直接用网络工具WireShark捕捉网络传输的packet,WireShark上面会直接标出这个包是TCP的包还是UDP的包,所以从来没有关心过。后来面试官告诉我,TCP协议更严格一些,要求一个handshake的过程,比如一个request从client发到server上之后,必须得到server端发回来的acknowledgement才可以继续进行下去,否则就hang在那里等待,要么就timeout中止这个过程。作为对比,UDP就比较宽松一些,这个协议不要求server发回ack,可以继续把数据发向server端。然后他又问我UDP在现实中有哪些应用?也不知道。又是他告诉我说主要用于video的传输或者voice chat,因为这个过程中由于数据量大,delay很常见,无法很奢侈地等待每一个ack回来。


2. 加密算法,何谓symmetric,何谓assymmetric?我说不知道。面试官问我当一台机器让我敲了username和password之后,该怎样验证我的身份?我说它应该用与加密算法相逆的算法将密码还原,他说这样不好。就好象如果他注册了一个网站,结果忘记密码了,问网站要回密码。如果网站真的将他原来的密码找出来寄回给他,他反而会不高兴,因为这意味着网站的服务器有办法反编译他的密码,并有可能泄漏给第三方。相反,很多网站会reset他的密码之后给他寄一个新的密码,这样他登录之后可以立即修改,这样就很安全。

所谓asymmetric的方法,就是当一个密码被加密得到一个hash key之后,当你登录之后,把你敲进来的密码再用同样的算法加密,然后比较两次hash key的结果。

EFI interview - 5

问题:怎样判断一个年份是否是闰年?

这个问题也栽了,当时对if-else的条件捣鼓了半天,还是没有捣鼓对。感觉上,面试官对判断闰年与否的条件给得不是很清楚,算是找个借口吧。

http://support.microsoft.com/kb/214019

In the Gregorian calendar, a normal year consists of 365 days. Because the actual length of a sidereal year (the time required for the Earth to revolve once about the Sun) is actually 365.25635 days, a "leap year" of 366 days is used once every four years to eliminate the error caused by three normal (but short) years. Any year that is evenly divisible by 4 is a leap year: for example, 1988, 1992, and 1996 are leap years.

However, there is still a small error that must be accounted for. To eliminate this error, the Gregorian calendar stipulates that a year that is evenly divisible by 100 (for example, 1900) is a leap year only if it is also evenly divisible by 400.

For this reason, the following years are not leap years: 1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600. This is because they are evenly divisible by 100 but not by 400.

The following years are leap years: 1600, 2000, 2400. This is because they are evenly divisible by both 100 and 400.

To determine whether a year is a leap year, follow these steps:

1). If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
2). If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
3). If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
4). The year is a leap year (it has 366 days).
5). The year is not a leap year (it has 365 days).

这里的算法逻辑很清楚。


http://en.wikipedia.org/wiki/Leap_year

if year modulo 400 is 0
    then is_leap_year
else if year modulo 100 is 0
    then not_leap_year
else if year modulo 4 is 0
    then is_leap_year
else
    not_leap_year

这里的算法非常简洁。

EFI interview - 4

这回是以前一个在Qualcomm面试中问过的问题,关于volatile关键字的作用。我跟面试官谈了一下,不过他说我说的原理是对的,但没有说出volatile的用法的intention所在,真是个傻逼。

再复习一下
---
在K&R的Bible的附录A.8.2里面,提到了这么一段:
The purpose of volatile is to force an implementation to suppress optimization that could otherwise occur. For example, for a machine with memory-mapped input/output, a pointer to a device register might be declared as a pointer to volatile, in order to prevent the compiler from removing apparently redundant references through the pointer.

另外,相关的资料也可参见下面的link:
http://en.wikibooks.org/wiki/Embedded_Systems/C_Programming
http://vault.embedded.com/story/OEG20010615S0107
http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html

EFI interview - 3

Problem: Two integers given, N and M. Suppose M = 10101 (binary system), write code to change N's 2nd to 6th bits to M. For example, N = 100001000, M = 10101, the result is expected to be 101010100.

当时瞎折腾了一通,并没有完全解出来。不过基本思路是有的,回到旅馆之后整理了一下思路,大致应该这么做:

M << 2 first, to move the binary numbers into bit 2~6. Then if we can clear bits 2~6 in N, say the cleared N is N1, then what left is to do N1 | M<<2.

But how to clear bits 2~6 in N? It seems N & ~1111100 can do it. So, could it be done with (N & (~1111100)) | M<<2?

不那么精巧,但似乎可以达成目标。MITBBS上有朋友提出了另外几种答案,不过我看不明白。比较典型的利用类似Bit Twiddling Hacks中的技巧的方法是MITBBS的FCer提出来的:(~k)&N | (M<<2),where k = (1<<7)-(1<<2)。不过看不懂为什么k要这样计算。

EFI interview - 2

问题:什么是callback function?

这个还真的问到我的痛脚了。以前写code的时候倒是见过几个callback function,用过,但是自己没有写过。只是有印象它们是用函数指针(function pointer)实现的,经常用于对某种event或是signal的相应,有点类似于微软可视化编程里面的消息驱动机制。不过,说实话,怎样把某个event和某个function pointer联系起来,我还不是很清楚。以前在code里面看到要register一下,不过怎样register呢?细节还是有点模糊。

可以读一读下面这几篇文章:
http://stackoverflow.com/questions/142789/what-is-a-callback-in-c-and-how-are-they-implemented
http://stackoverflow.com/questions/2738463/implementing-callback-functions-in-c
http://www.cprogramming.com/tutorial/function-pointers.html

EFI interview - 1

题目:打印一个Pascal's Triangle(也称杨辉三角)。

解开这个题目,至少有三种方法。前两个方法可以在Wikipedia上面得到启示:http://en.wikipedia.org/wiki/Pascal's_triangle

1. 利用二项式展开的方法,启示杨辉三角的每一个元素都是二项式(x+y)^n的系数,可以用下面的公式来计算:




值得注意的是,如果行数从0算起,那么杨辉三角每一行的列数都等于行数+1。



2. 任意一行的任意一个元素,其实都是其所在的行数、列数以及其前一个元素的某种组合(函数)。
This algorithm is an alternative to the standard method of calculating individual cells with factorials. Starting at the left, the first cell's value v0 is 1. For each cell after, the value is determined by multiplying the value to its left by a slowly changing fraction:






where r = row + 1, starting with 0 at the top, and c = the column, starting with 0 on the left. For example, to calculate row 5, r = 6. The first value is 1. The next value is 1 × 5/1 = 5. The numerator decreases by one, and the denominator increases by one with each step. So 5 × 4/2 = 10. Then 10 × 3/3 = 10. Then 10 × 2/4 = 5. Then 5 × 1/5 = 1. Notice that the last cell always equals 1, the final multiplication is included for completeness of the series.



3. 利用杨辉三角的一个重要性质:底下一个元素是其上一层两个元素之和。下面的代码是MITBBS的网友speeddy提供:

int p1[20];
memset(p1, 0, 20*4);
p1[0] = 1;

for(int i=0; i<10; i++) {
  int next = 0;
  int prev = 0;

  for (int j = 0; j < i+1; j++) {
    if (j == 0) {
      p1[j] = 1;
      prev = 1;
    } else if (j == i) {
      p1[j] = 1;
    } else {
      next = p1[j];
      p1[j] += prev;
      prev = next;
    }
    cout << p1[j] << " ";
  }
  cout << endl;
}

result:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Wednesday, April 6, 2011

Qualcomm Onsite 之三

一个Interviewer问我以前的工作中code base有多大,我说大概有几万个文件,总共的size超过1GB。然后他又问我文件的大小是多少,我说从几百个字节到几千个字节不等。

接着,他又问我如果一个function超过一百行,会不会有问题?我说还行,我还见过更长的。话刚说完,我就有点知道他问的意思了。马上再补充说,这样的函数往往集合了太多的功能,developer最好应把它分解成几个sub-function,每个sub-function完成某项特定的任务,这样就可以实现较好的readibility。不过也有代价,就是stack的开销增加了,因为call每个sub-function都要把它们push进stack,执行完了再pop it up,也许会对performance有影响。Developers must balance between readibility and performance.

Interviewer接过话茬说,有没有一种方法,可以让我们enjoy the best of both worlds?我说很难。他提示说其实gcc的compiler提供了一种优化的方法可以做到不至于让多个sub-function影响performance, without using the keyword "inline". For example, gcc -O1/O2/O3

查了一下gcc的document,还真是如此:
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

-O2 turns on all optimization flags specified by -O. It also turns on the following optimization flags:

        -fthread-jumps
        -falign-functions -falign-jumps
        -falign-loops -falign-labels
        -fcaller-saves
        -fcrossjumping
        -fcse-follow-jumps -fcse-skip-blocks
        -fdelete-null-pointer-checks
        -fdevirtualize
        -fexpensive-optimizations
        -fgcse -fgcse-lm
        -finline-small-functions
        -findirect-inlining
        -fipa-sra
        -foptimize-sibling-calls
        -fpartial-inlining
        -fpeephole2
        -fregmove
        -freorder-blocks -freorder-functions
        -frerun-cse-after-loop
        -fsched-interblock -fsched-spec
        -fschedule-insns -fschedule-insns2
        -fstrict-aliasing -fstrict-overflow
        -ftree-switch-conversion
        -ftree-pre
        -ftree-vrp

看样子,就是其中的-finline-small-functions这个开关起的作用了。以前在施乐写程序的时候,没太关注Makefile,看来付出代价了。