1、数组无法是实现越界检测;(c无数组);
2、地址空间处理过程中,只有一种情况不传递个数(慎用)
字符空间,只要是字符空间就一定有一个结束标志0;
字符空间:常量行为:字符串;要存东西先考虑是不是字符空间;
char *strcpy(char *dest//目标地址,const *src//源地址){
for(int i = 0;i < x;i++){
if(src[i] == 0)
break;
}
}
--------------
int i = 0;
while(1){
if(src[i] ==0){
break;}
.....src[i];
i++;
}
-------------
int i = 0;
while(src[i] !=0){
.....src[i];
i++;
}
char *strcpy(char *dest,const *src){
int i = 0;
while(src[i] ){
dest[i] = src[i];
i++;}
return dest;}//strcpy的内部结构;
从头到尾依次处理;条件循环用while;
案例:
int strlen (const char *data){
int i = 0;
while(data [i]){
i++;}
return 0;}
strlen 的函数内部结构;
printf(%s,a)字符输出;
-----------------------------------------
涉及到写入空间的,建议加上个数;
openssl 加密软件;漏洞心脏流血 strcpy函数漏洞;
如果源码超过目标函数导致内存越写;工程中不能用strcpy;
可以使用strncpy;
:dest 目标
src 源
src 少
dest多不担心越界问题;空余的用0填充;
strncpy(dest,src,sizeof(dest))
src多
dest少
strncpy(dest,src,sizeof(dest)-1)
减一拷贝的时候留一个空间来加0;其余的空间舍去;
char *strncpy