|
发表于 2011-5-31 02:27:18
|
显示全部楼层
这个问题值得大家继续发掘探讨,因为参数不同变换,问题还真不少哈
先贴上strncat 的源代码,大家尽量发掘下问题的根源所在吧:你行的!  - char *strncat (char *s1, const char *s2, size_t n)
- {
- reg_char c;
- char *s = s1;
-
- /* Find the end of S1. */
- do
- c = *s1++;
- while (c != '\0');
-
- /* Make S1 point before next character, so we can increment
- it while memory is read (wins on pipelined cpus). */
- s1 -= 2;
-
- if (n >= 4)
- {
- size_t n4 = n >> 2; // n4 = n/4; 进行n4 次搬运,每次搬运4 个字节。
- do
- {
- c = *s2++;
- *++s1 = c;
- if (c == '\0')
- return s;
- c = *s2++;
- *++s1 = c;
- if (c == '\0')
- return s;
-
- c = *s2++;
- *++s1 = c;
- if (c == '\0')
- return s;
- c = *s2++;
- *++s1 = c;
- if (c == '\0')
- return s;
- } while (--n4 > 0);
- n &= 3;
- }
-
- while (n > 0) // 事实上该程序从这里开始依旧可以,上边搞啥搞?
- { // 搞了那么多,是想告诉我们算法的基础: 空间可以换时间!!
- c = *s2++;
- *++s1 = c;
- if (c == '\0')
- return s;
- n--;
- }
-
- if (c != '\0')
- *++s1 = '\0';
-
- return s;
- }
复制代码 |
|