Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 947

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/samsongli/meirendaddy.com/blog/wp-includes/kses.php on line 948
November 27, 2008

好久没有写“从硅到搜索引擎”这个主题了,因为我写完前面一篇后,忽然想把这个系列写成对当年发明这些东西的科学家和公司的八卦。但是犹豫之后,还是决定走技术这条路,毕竟八卦不是我的强项。写技术虽然枯燥很多,毕竟不会出大错。

上次说到三极管可以把电流按照比例放大。这对于音响一类需要把小信号放大的应用当然是好事情,但是对于数字信号,就没有这个必要了。数字信号就是0和1,对应的三极管的状态,就是完全没有电流通过(关闭)或者有大量电流通过(开启),中间的状态,比如有100微安的电流进入,导致有0毫安的电流通过,这个状态对于数字芯片里的三极管来说,不仅不必要,反而是有害的,因为:这个状态的三极管就相当于一个电阻,会发热。一个三极管发热不要紧,几百万个三极管一起发热,这个芯片就要融化了。

所以数字芯片里的三极管,从完全关闭到完全开启,速度一定要快,就是因为要减少发热的时间。假如说每次三极管从关闭到开启的过程中,等效的电阻是R,转换的时间是T的话,那么产生的热量就是: W = U^2 / R * T,这里U是芯片的运行电压。

另外一个发热的地方是输入三极管的控制电流,发热的公式也可以写成W’ = U * I * T,这里I是控制电流,所以I越小越好。实际的数字芯片里面的三极管,都不是电流控制的,而是用电压控制的金属氧化物三极管,类似这样:

其中的Gate就是控制三极管开关的管脚。因为Gate和另外两个管脚是绝缘的,所以从Gate到另外两个管脚的电流非常的小,因而产生的热量也很小。当Gate充电/放电的时候,就能控制电流在Source和Drain之间的流动,达到开关的目的。

November 24, 2008

最近乱翻一些论文,发现龙芯还是在走RISC的乱序执行的路。做CPU体系结构的人都知道,乱序执行是能耗大户,而且对于芯片设计的复杂度也是很大的挑战。龙芯现在走的乱序执行的路,似乎和龙芯的商业目标相左。

现在龙芯在商业化上面的目标,是要成为低价笔记本/终端的芯片。这样的定位,其实对于性能的要求,远没有对于能耗的要求高。也就是说,可以牺牲部分性能,来降低能耗。

2002年的时候,我做过一个项目,是用模拟器来计算RISC芯片的能耗,并对体系结构的改变进行能耗上面的评估。当时的结论是,对于典型的应用,如果能够把纯乱序执行变为有限制的乱序执行,那么可以达到30%左右的能耗优化,而性能的影响小于10%。

Intel新的ATOM架构里面,采用了类似的思想,就是一般的指令序列是顺序执行;对于最常见的容易产生流水线空泡的指令序列,即长周期指令(浮点指令)后面紧跟一个短周期指令(整数指令)的序列,才进行乱序优化。

优化的方法是:先判断后面的整数指令是否对前面的浮点指令有数据依赖,没有的话才进行乱序执行。这是典型的80-20分析方法,将有限的能耗预算,用于优化最常见的影响性能的指令序列。

2002年做研究的时候用的基准应用程序,有很多科学计算的东西,龙芯的基准程序集合,因为是针对日常应用,应该有更多的整数指令序列,对乱序执行的要求应该会更少。换句话说,龙芯的乱序执行部件,应该有很多时候都是闲置在那里,空耗能量的。如果改成有限的乱序执行,应该能节省很多能耗。

相应的为了补偿减弱/取消乱序执行带来的性能减弱,可以考虑增加cache的容量。Cache的设计相对芯片逻辑更简单一些。

不知道新的龙芯是否能借鉴这种设计思想。

November 23, 2008

以下食物或者食物组合不能吃,会有毒

  • 发芽的大蒜
  • 发芽的黄豆
  • 发芽的绿豆
  • 葱和豆腐
  • 螃蟹和豆腐
  • 牛肉和土豆
  • 猪肉和萝卜
  • 羊肉和胡萝卜
  • 黄鳝和生姜
November 20, 2008

Google search可以说是用户贡献内容(UGC)的经典之作了。

  • 用户制作的url,为search engine指明了哪些网页更受欢迎
  • 用户写的网页内容,每个词都是一个tag
  • 用户在html页头里输入的关键字,用户在页面里定义的标题,这些都是关键词的来源
  • 最后,search engine给用户看的结果里包含了页面的简介,用户的点击告诉搜索引擎,哪个结果更符合用户口味

不过,这些技巧放在其他应用里,就不那么好用了。Google map,这个金玉其外的产品,在它的ajax动态载入地图的光辉散去之后;我开始发现,在生成路径的时候,其算法经常会做出令人哭笑不得的事情,比如明明有一个地方可以左转的,却一定要我到前面很远的地方去掉头回来;甚至发生过让我去高速上面兜一圈再回来的事情。

Google map也有很多请用户贡献内容的办法,比如让用户去自己输入一个公司的地址之类的,可见google map也知道从tele atlas买的数据有很多误差,可是这么多年过去了,还是有那么多错误的地址,有那么多只在数据库里存在的路名。

还有一个例子就是我最喜欢的google language tool,或者用它的大名:谷歌翻译。如果你用谷歌翻译去翻了一段英文的话,谷歌翻译会在生成的外星语言里,请你去输入地球人的语言。可是这么多年过去了,谷歌翻译还是会告诉我:

It’s time to go to school 的意思是:

它的时间去上学

其实也没错,它的确是该去上学好好学习学习了。

November 19, 2008

没事玩什么颜色主题。原来的灰色挺好的,现在还要我自己改,我懒得改。我喜欢gmail主要是因为按主题归类,另外是速度快,界面平实。玩颜色主题,令人痛恨。

November 14, 2008

今天看到一个特别简单的题目,就是说有两个数组A和B,假设都是整数类型的,而且都是从小到大排好序的,A里面,从下标0开始,有X个数;B里面有Y个数,A的长度是X+Y,B的长度是Y,就是说A里面能放下所有A和B的数。目标是设计一个算法,把所有这些数字都放到A里面去,当然结果要是排序的。

这个题目再简单不过了,就是倒着做merge sort。具体说就是用两个下标,分别从A和B的数据尾巴开始,做合并。合并的结果从A的最后一个位置开始放,最后A里面就是A和B的合并的并且排序的结果了。

说到这个题目,想起另外一个比较难的题目来。不过还是用一个简单的题目开始吧:假如有一个很长的整数数组,要求用这个数组实现两个堆栈。

这个题目很简单,就是用两个栈指针,然后分别从数组的两头开始,向中间生长。具体写程序的时候,检查一下两个指针是否相遇就可以了。

然后题目就难了一点:如何用一个数组实现三个堆栈?

答案其实是上面那个题目的一个挺巧妙的扩展:三个堆栈里面的两个,还是按照上面的办法做;然后第三个堆栈,从数组三分之一的地方开始,向三分之二的方向生长。具体说来:

假设数组是A,长度是L,那么三个堆栈分别从下标0,L,和1/3L的地方开始生长。假设三个堆栈的栈顶指针分别是x,y和z,那么当栈生长到一定时间的时候,空间会不够用,可能会有以下情况:

  1. x生长到了1/3L
  2. y和z相遇

这两种情况下,数组都会有一段空间空闲(如果三个栈一样长,那就结束了),或者是A[x,1/3L],或者是A[y,z]。那么把这个空间继续按照上面的办法分割,直到空间用完为止。

November 10, 2008

大学里面头两年,我经常去一个高中同学宿舍玩,通常周六中午就冲过去,一起吃饭,
然后打牌。慢慢的他们宿舍就成了他们年级的俱乐部。

他们年级有个哥们,此人的特点就是:瘦长。瘦长的身子瘦长的脸,瘦长的手指里夹着
根瘦长的烟。那个时候大家还挺自觉的,抽烟都到外面去,别管冬天有多冷,都得去屋
子外面抽。这哥们偏不,他也知道大家烦他抽烟,所以他就站在门口抽,我们也拿他没
办法。

不过除了抽烟以外他还是不错一人,平时打牌下四国的时候,也不怎么赖,如果赖了,
也不怎么承认。

有时候大家会有点好吃的,比如从亲戚家里带的,这个时候我们也会请几个女生来一起
吃。有一次我们请了几个女生过来,里面还有我老婆。大家正在聊天吃东西,忽然闻见
一股烟味儿,然后门开了,这位哥们儿靠在门框上,脸上似笑非笑。

我同学他们宿舍老大说:“xx,来,进来一起吃!”

我同学说:“先把烟掐了!没看见有女士么!”

这位轻轻的摇摇头:“我心情不太好”

一位女生笑嘻嘻的说:“那你吃点东西吧,吃了心情就好了”

这位轻轻的说:“我作了一首诗”

我同学宿舍的老幺发出了剧烈的咳嗽

那个女生不知深浅的说:“好啊好啊,念给我们听听吧?”

这位用瘦长的手指,弹了弹瘦长的烟头上面瘦长的烟灰,挺直了瘦长的身子,朗声念道:

“啊~~~!阿波罗,你的光辉&**%&$^%$#”
“啊~~~!阿波罗,你的……%¥#&%……”
“啊~~~!阿波罗,你的(*)(&(*&)(”
“啊~~~!阿波罗,啊~~~!”

经典的面试题目,通常假设是连通图。

首先要明确的,是这个图是有向图,还是无向图。

无向图比较简单,就用染色法好了。对每个访问过的结点进行标记,遇到标记过的结点,就说明图里有圈。

有向图稍微复杂一点,先找出所有没有进入边的结点,把它们作为起点集合,从他们中间选一个出发。如果这个集合是空,那么有圈。

然后的方法,就是每个结点有两个标志位:一个用来标志是否访问过,另外一个用来标志是否当前“活跃”。从出发点集合选 一个没有被访问过的结点,开始进行深度优先搜索。遇到的没有访问过的结点,标记访问过,并且活跃;回退的时候,清除活跃标志。

如果遇到的结点是访问过的,但是不活跃,那么不访问这个结点。

如果遇到的结点是活跃的,那么有圈。

直到找到圈,或者所有出发集合里的结点都被访问过为止。

November 8, 2008

题目:写一个函数,给一个C的字符串S,返回一个字符串,其内容是S的反向。比如,S是“abc”,返回“cba”

参考代码:

char* reverse(const char* s) {

if (!s) return s;  //check if s is NULL (1)

size_t size = strlen(s);

if (! size || size == 1) return s; // check for empty string or single char string (1)

char* r = new char[size + 1];  // (2)

for (size_t i = 0; i < size; ++i) {

r[i] = s[size - i - 1];  // reverse (3)

}

r[size] = ‘\0′;  // (4)

return r;  // caller should delete r after use (5)

}

点评:

本题的考点有以下几点:

  1. 边界条件的检查
  2. C字符串的结构
  3. C字符串的寻址方式
  4. C字符串的结尾方式
  5. 内存管理
Next Page »