strcpy与strncpy的区别
2015-11-21 15:44
369 查看
首先看c++ reference中关于strcpy与strncpy的详细说明。
参看http://www.cplusplus.com/reference/cstring/strcpy/和http://www.cplusplus.com/reference/cstring/strncpy/。
其次在stackoverflow上有一个相关问题:
问题:
Edit: I've added the source for the example.
I came across this
example:
Which produced this output:
Which makes me wonder why anyone would want this effect. It looks like it would be confusing. This program makes me think you could basically copy over someone's name (eg. Tom Brokaw) with Tom Bro763.
What are the advantages of using
解答:
buffer overflow by requiring you to put a length in it.
on a trailing
which may not always occur.
Secondly, why you chose to only copy 5 characters on 7 character string is beyond me, but it's producing expected behavior. It's only copying over the first
where
the third argument.
The
are all used as defensive coding against buffer overflows. Please use them in lieu of older functions, such as
参看http://www.cplusplus.com/reference/cstring/strcpy/和http://www.cplusplus.com/reference/cstring/strncpy/。
其次在stackoverflow上有一个相关问题:
问题:
Edit: I've added the source for the example.
I came across this
example:
char source[MAX] = "123456789"; char source1[MAX] = "123456789"; char destination[MAX] = "abcdefg"; char destination1[MAX] = "abcdefg"; char *return_string; int index = 5; /* This is how strcpy works */ printf("destination is originally = '%s'\n", destination); return_string = strcpy(destination, source); printf("after strcpy, dest becomes '%s'\n\n", destination); /* This is how strncpy works */ printf( "destination1 is originally = '%s'\n", destination1 ); return_string = strncpy( destination1, source1, index ); printf( "After strncpy, destination1 becomes '%s'\n", destination1 );
Which produced this output:
destination is originally = 'abcdefg' After strcpy, destination becomes '123456789' destination1 is originally = 'abcdefg' After strncpy, destination1 becomes '12345fg'
Which makes me wonder why anyone would want this effect. It looks like it would be confusing. This program makes me think you could basically copy over someone's name (eg. Tom Brokaw) with Tom Bro763.
What are the advantages of using
strncpy()over
strcpy()?
解答:
strncpycombats
buffer overflow by requiring you to put a length in it.
strcpydepends
on a trailing
\0,
which may not always occur.
Secondly, why you chose to only copy 5 characters on 7 character string is beyond me, but it's producing expected behavior. It's only copying over the first
ncharacters,
where
nis
the third argument.
The
nfunctions
are all used as defensive coding against buffer overflows. Please use them in lieu of older functions, such as
strcpy.
相关文章推荐
- C++ 函数指针
- LeetCode(205)Isomorphic Strings
- grads 用arcgis分析站点的网格
- 02 计算字符串表达式 [类似于js eval函数]
- linux自动挂载NTFS格式移动硬盘
- 缓存技术概述
- python 函数定义
- 算法学习之排序:快速排序算法
- 【学神】1-1 linux简介
- VB 加载winIO,实现驱动级键盘模拟
- 项目管理过程4W1H
- FileItem类的常用方法
- LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据[转]
- Effective Objective-C 2.0 — 第一条:了解Objective-C语言的起源
- J2EE实验3:猜数游戏
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
- 虚拟机克隆后eth0网卡变成eth1网卡并且IP地址消失问题解决
- 计算机网络(第6版):自顶向下方法-资源文件
- 链接
- 1136: HH生病了