模拟实现memcpy,memmove,strtok----缓冲区重合实质
-
memcpy和memmove
void * memcpy ( void * destination , const void * source , size_t num ) ;
void * memmove ( void * destination , const void * source , size_t num ) ;作用;从source的位置开始向后复制num个字节的数据到destination的内存位置
注意:destination和source所在的内存位置可能存在缓冲区重合的现象,如果出现缓冲区重合现象,则不能使用memcpy,只能使用memmove,
-缓冲区重合
只要destination的首地址在source的首地址到source的末地址之内则是缓冲区重合
图示:
注意:并不是只要内存重合就会产生缓冲区重合
图示:
从上面的介绍可以很免得看出来只要"source的首地址<destination的首地址<=source的末地址"就会发生缓冲区重合
memcpy函数使用方式:
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="123456";
memcpy(str,str+1,6);
puts(str);
return 0;
}
结果为:
memcpy函数模拟实现:
#include <stdio.h>
void* my_memcpy(void* destination, void* source, size_t num)
{
while (num > 0)
{
*(char*)destination = *(char*)source;
--num;
++(char*)destination;
++(char*)source;
}
}
int main()
{
char str[] = "123456";
memcpy(str, str + 1,6);
puts(str);
}
结果为:
memmove函数使用方式:
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="123456";;
memmove(str,str+1,7); //str和str+1缓冲区重合
puts(str+1);
return 0;
}
结果为:
memmove函数模拟实现:
#include <stdio.h>
void* my_memmove(void* destination, const void* source,size_t num)
{
//缓冲区重合
if ((char*)source < (char*)destination&&(char*)destination <= (char*)source + num)
{
(char*)destination = (char*)destination + num - 1;
(char*)source = (char*)source + num - 1;
while (num > 0)
{
*(char*)destination = *(char*)source;
--num;
--(char*)destination;
--(char*)source;
}
}
//缓冲区没重合
else
{
while (num > 0)
{
*(char*)destination = *(char*)source;
--num;
++(char*)destination;
++(char*)source;
}
}
}
int main()
{
char str[] = "123456";
my_memcpy(str+1, str,7);
puts(str+1);
}
结果为
:
2.strtok
char* strtok ( char * str , const char * sep ) ;
作用:分割字符串,只要遇到了sep字符串里面的字符,就会进行一次分割
注意:
- sep参数是个字符串,定义了用作分隔符的字符集合 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记
- strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:strtok函数会 改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
- strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
- strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
- 如果字符串中不存在更多的标记,则返回 NULL 指针。
函数使用方式:
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="= :hello>,wo?rd";
char *p;
printf("要分割的字符转为:\"%s\"\n分割好的字符串为:\n",str);
p=strtok(str,"= :>?");
while(p!=NULL)
{
printf("%s\n",p);
p=strtok(NULL,"= :>?");
}
return 0;
}
结果为:
函数模拟实现:
#include <stdio.h>
#include<assert.h>
char* my_strtok(char* str1, const char* str2)
{
assert(str2 != NULL);
static char* str1_tmp;
if (str1 == NULL)
str1 = str1_tmp;
else //起始赋值
str1_tmp = str1;
int cnt = 0;
const char* str2_tmp = str2;
while (*str1)
{
while (*str1_tmp != '\0' && *str2 != '\0' && *str1_tmp != *str2)//判断是否为分隔符
++str2;
if (*str1_tmp == '\0')//不写会让str1_tmp+1变成无效字符串,使它指向\0的后面
{
*(str1 + cnt) = '\0';//让在str1中的分隔符变成'\0'
return str1;
}
if (*str1_tmp == *str2 && cnt != 0)//遇到分隔符,而且要保证不是字符串的起始位置
{
++str1_tmp;
*(str1 + cnt) = '\0';
return str1;
}
if (*str1_tmp == *str2 && cnt == 0)//遇到在字符串起始位置的分隔符
{
++str1_tmp;
++str1;
str2 = str2_tmp;
}
if (*str2 == '\0')//没遇到分隔符
{
++str1_tmp;
++cnt;//用来记str1的起始位置到分隔符的位置,相当于分隔符所在的下标
str2 = str2_tmp;
}
}
return NULL;
}
int main()
{
char str[] = "= :hello>,wo?rd";
char *p;
printf("要分割的字符转为:\"%s\"\n分割好的字符串为:\n", str);
p = my_strtok(str, "= :>?");
while (p != NULL)
{
printf("%s\n", p);
p = my_strtok(NULL, "= :>?");
}
}
结果为: