strcpy_s
char src[5]="abcd";
char *des=new char[str.length(src)+1]; // length()不计\0
strcpy_s(des,length(src),src); // 崩溃
strcpy_s(des,length(src)+1,src); // √
因为strcpy_s的边界保护机制 size(src)<=复制size<=size(des)
strcpy_s源码:
errno_t __cdecl _FUNC_NAME(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC)
{
_CHAR *p;
size_t available; /* validation section */
_VALIDATE_STRING(_DEST, _SIZE);
_VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE); p = _DEST;
available = _SIZE;
while ((*p++ = *_SRC++) != && --available > )
{
} if (available == 0)
17 {
_RESET_STRING(_DEST, _SIZE);
_RETURN_BUFFER_TOO_SMALL(_DEST, _SIZE);
}
_FILL_STRING(_DEST, _SIZE, _SIZE - available + );
_RETURN_NO_ERROR;
}
当源字符串_SRC不到\0字符,复制长度available减到0时,有边界测试会引发断言。 是strcpy的安全版本,其他的str类型函数也有安全版本。
strcpy复制到src的\0字符停止,但是当size(dest)<size(src)时,只要两个指针指向的内存可读写,循环就可以继续下去,会超出Dest的边界覆盖其他内存。
复制char[]无\0字符时,会一直复制到遇到下一个\0字符。 用char[]保存字符串,在最后加上\0,
否则strcpy边界不对时,会有隐形错误。或者在strcpy之后给char[]加上\0
char* p="how are you ?";
char name[10];
strcpy(name,p);
name[sizeof(name)-1]='"0'
strncpy源码:
char *strncpy(dest, source, count) - copy at most n characters
;
;Purpose:
; Copies count characters from the source string to the
; destination. If count is less than the length of source,
; NO NULL CHARACTER is put onto the end of the copied string.
; If count is greater than the length of sources, dest is padded
; with null characters to length count.
;
; Algorithm:
; char *
; strncpy (dest, source, count)
; char *dest, *source;
; unsigned count;
; {
; char *start = dest;
;
; while (count && (*dest++ = *source++))
; count--;
; if (count) //如果count大于零 即count is greater than the length of sources,'\0'已写入,不够的字符数都填充\0
; while (--count)
; *dest++ = '\0';
; return(start);
; }
;
;Entry:
; char *dest - pointer to spot to copy source, enough space
; is assumed.
; char *source - source string for copy
; unsigned count - characters to copy
;
;Exit:
; returns dest, with the character copied there.
;
strcpy(des,src,size);
size<src长度时,des最后字符无\0,打印时会乱码直到遇到\0
size>src长度时,不够的位全部补充\0
strcpy_s的更多相关文章
- 关于strcpy_s
#include"stdafx.h" #include<iostream> #include<cstring> int main() { using nam ...
- 对 strcpy_s 若干测试
今天发现如果strcpy这函数,目标buffer太小,会有意想不到的崩溃.而且不容易调试.以后尽量要用strcpy_s了. strcpy_s是strcpy的更安全的版本 1.当目标字符串参数是一个字符 ...
- wcscpy wcscpy_s strcpy strcpy_s的区别
原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h 功能:把从src地址开始且含有NULL结束符的字符串赋值到以des ...
- strcpy_s与strcpy的比較
strcpy_s和strcpy()函数的功能差点儿是一样的.strcpy函数,就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它仅仅能假定缓冲足够大来容纳要拷贝的字符串.在程序执行时,这将 ...
- 新版本的strcpy_s
char a[32] = "1234"; char b[32] ="123"; strcpy_s(b,sizeof(b), a + 2);//可以用strlen ...
- C++ strcpy strcpy_s strncpy strlcpy
strncpy的用法:它与strcpy的不同之处就在于复制n个字符,而不是把所有字符拷贝(包括结尾'\0'). 函数原型:char * strncpy(char *dst,const char * s ...
- strcpy_s与strcpy对照
strcpy_s和strcpy()函数功能几乎相同.strcpy函数.就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它仅仅能假定缓冲足够大来容纳要拷贝的字符串.在程序执行时,这将导致不可 ...
- C++string函数之strcpy_s
strcpy_s和strcpy()函数的功能几乎是一样的.strcpy函数,就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它只能假定缓冲足够大来容纳要拷贝的字符串.在程序运行时,这将导致 ...
- strcpy_s和strcpy()
转自: https://www.cnblogs.com/hrhguanli/p/4570093.html strcpy_s和strcpy()函数功能几乎相同.strcpy函数.就象gets函数一样,它 ...
随机推荐
- 循环-21. 求交错序列前N项和
/* * Main.c * C21-循环-21. 求交错序列前N项和 * Created on: 2014年8月18日 * Author: Boomkeeper ***********测试通过**** ...
- BootstrapTable+KnockoutJS
BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查 前言:之前博主分享过knockoutJS和BootstrapTable的一些基础用 ...
- MySQL 表分区的几种方法和注意
分区方法1:Hash分区 例子: create table thash(x int ,y int) partition by hash(x) partitions 4; 就这么一句话表就分好区了.下一 ...
- linux下如何查看系统和内核版本
1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@cro ...
- 再探Delphi2010 Class的构造和析构顺序
发了上一篇博客.盒子上有朋友认为Class的构造和析构延迟加载.是在Unit的初始化后调用的Class的构造.在Unit的反初始化前调用的Class的析构函数. 为了证明一下我又做了个试验 unit ...
- mysql 性别存储
大家在设计数据库时,碰到 性别.状态等 这些 值比较固定的列时,数据类型 是如何定义? 通常都是采用 : 1 create table `XXX` 2 ( 3 ........ 4 sex int(1 ...
- POJ 3693 Maximum repetition substring(后缀数组+ST表)
[题目链接] poj.org/problem?id=3693 [题目大意] 求一个串重复次数最多的连续重复子串并输出,要求字典序最小. [题解] 考虑错位匹配,设重复部分长度为l,记s[i]和s[i+ ...
- 如何成为uber司机,uber司机详细注册流程
怎样注册uber司机 如何注册加入uber司机 全国加入Uber 的要求 车辆要求:要求裸车价8万以上,车龄5年以内,第三者责任险保额30万以上,不支持20万以下的面包车/商务车,不支持4座以下车辆. ...
- HTML本地存储,localstorg的应用实例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- swig include使用方法
{% block content2 %} {% include "footer.html" %} {% endblock %} include语句必须放到 block模块中,不然不 ...