【glibc源码分析】–strcpy.c 字符串复制

strcpy是常用的字符串复制函数,经常在面试中考到。该文件位于glibc源码的string目录中。

在线资源路径:

http://www.oschina.net/code/explore/glibc-2.9/string/strcpy.c

快速查看源码如下:

1 /* Copyright (C) 1991, 1997, 2000, 2003 Free Software Foundation, Inc.
 2    This file is part of the GNU C Library.
 3 
 4    The GNU C Library is free software; you can redistribute it and/or
 5    modify it under the terms of the GNU Lesser General Public
 6    License as published by the Free Software Foundation; either
 7    version 2.1 of the License, or (at your option) any later version.
 8 
 9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17 
18 #include <stddef.h>  /* 用到了ptrdiff_t */
19 #include <string.h>
20 #include <memcopy.h>
21 #include <bp-checks.h>  /* 定义了CHECK_BOUNDS_LOW和CHECK_BOUNDS_HIGH */
22 
23 #undef strcpy
24 
25 /* Copy SRC to DEST.  */
26 char *
27 strcpy (dest, src)
28      char *dest;
29      const char *src;
30 {
31   char c;
32   char *__unbounded s = (char *__unbounded) CHECK_BOUNDS_LOW (src);
33   const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1;
34   size_t n;
35 
36   do
37     {
38       c = *s++;
39       s[off] = c;  /* 注意这种写法, s[off]相当于 *(s+off) */
40     }
41   while (c != '\0');
42 
43   n = s - src;
44   (void) CHECK_BOUNDS_HIGH (src + n);
45   (void) CHECK_BOUNDS_HIGH (dest + n);
46 
47   return dest;
48 }
49 libc_hidden_builtin_def (strcpy)

以上的算法主要通过计算好两个字符串的偏移量,然后循环遍历src字符串,逐个赋值给dest的相应位置;gnu的源码一般都比较复杂,进行了相应的范围检查等处理;

再来看个简单的,比较独立的代码:

1 char * strcpy(char * dest, const char * src)
 2 {
 3     if ( dest == NULL || src == NULL) // 地址检查
 4     {
 5         return NULL;
 6     }
 7 
 8     if ( dest == src) // 相同地址检查
 9     {
10         return dest;
11     }
12 
13     char * str = dest;
14     while ( ( *str++ = *src++ ) != '\0' )  // 循环复制
15     {
16         ;
17     }
18 
19     return dest;
20 }

注意,以上代码采用了清爽的缩进格式,保证代码的清晰和可读性; 第8行的检查比较容易漏掉,这个是重点;

原文链接: https://www.cnblogs.com/QuLory/archive/2012/09/17/glibc-strcpy.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/63088

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午10:45
下一篇 2023年2月9日 上午10:45

相关推荐