C\C++几个内存处理函数

来源:互联网 发布:软件界面设计培训 编辑:程序博客网 时间:2024/05/01 17:33
  1. */  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. int main ()  
  6. {  
  7.   char str1[]="Sample string";  
  8.   char str2[40];  
  9.   char str3[40];  
  10.   memcpy (str2,str1,strlen(str1)+1);  
  11.   memcpy (str3,"copy successful",16);  
  12.   printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);  
  13.   return 0;  
  14. }  

Output:

[cpp] view plaincopyprint?
  1. str1: Sample string  
  2. str2: Sample string  
  3. str3: copy successful  

2、memmove

void * memmove ( void * destination, const void * source, size_t num );

移动内存块

从source指向的位置复制num个字节的值到destination指向的内存块。复制操作的发生就好像使用了中间缓存,允许destination和source重叠。

对于这个函数,source和destination指向的对象的底层类型是无关的;结果是数据的一个二进制复制。

这个函数不检查任何结束空字符(null)—— 它总是准确地复制num个字节。

为了避免溢出,source和destination两个参数指向的数组的大小应该至少是num字节。

参数

destination
       指向内容被复制到的目的数组的指针,类型被转换为void*。
source
       指向复制数据的源数组的指针,类型被转换为void*。
num
       要复制的字节数。

返回值

       返回目的指针destination。

Example:

[cpp] view plaincopyprint?
  1. /* memmove example */  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. int main ()  
  6. {  
  7.   char str[] = "memmove can be very useful......";  
  8.   memmove (str+20,str+15,11);  
  9.   puts (str);  
  10.   return 0;  
  11. }  

Output:

[cpp] view plaincopyprint?
  1. memmove can be very very useful.  
3、memset

void * memset ( void * ptr, int value, size_t num );

填充内存块

将ptr指向的没存块的前num个字节设置为特定的值value(被解释为一个unsigned char类型的值)。

参数

ptr
       指向需要填充的内存块的指针。
value
       要设置的值,这个值是作为int型传递的,但是该函数用这个值的unsigned char类型转换值来填充内存块。
num
       要是指特定值的字节数。

返回值

返回指针ptr。

Example:

[cpp] view plaincopyprint?
  1. /* memset example */  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. int main ()  
  6. {  
  7.   char str[] = "almost every programmer should know memset!";  
  8.   memset (str,'-',6);  
  9.   puts (str);  
  10.   return 0;  
  11. }  

Output:

[cpp] view plaincopyprint?
  1. ------ every programmer should know memset!  

4、memchr

const void * memchr ( const void * ptr, int value, size_t num );      void * memchr (       void * ptr, int value, size_t num );
在内存块中定位字符
在ptr指向的没存块的前num个字节中搜索value值得第一次出现(被解释为一个unsigned char类型的值),并返回指向该值的指针。

参数

ptr
       指向需要被执行搜索的内存块的指针。
value
       需要被定位的值。这个值是作为int型传递的,但是该函数用这个值的unsigned char类型转换值来进行逐位的搜索。
num
       要是分析的字节数。

返回值

       返回指向ptr指向的内存块中value值第一次出现位置的指针。

       如果没有找到value值,则该函数返回NULL。

Example:

[cpp] view plaincopyprint?
  1. /* memchr example */  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. int main ()  
  6. {  
  7.   char * pch;  
  8.   char str[] = "Example string";  
  9.   pch = (char*) memchr (str, 'p', strlen(str));  
  10.   if (pch!=NULL)  
  11.     printf ("'p' found at position %d.\n", pch-str+1);  
  12.   else  
  13.     printf ("'p' not found.\n");  
  14.   return 0;  
  15. }  

Output:

[cpp] view plaincopyprint?
  1. 'p' found at position 5.  
5、memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

比较两个内存块

将ptr2指向的内存块的签num个字节与ptr2指向的内存块的前num个字节进行比较,如果它们都相符合则返回0,反之则返回一个指示其中一个较大的非零值。

参数

ptr1
       指向内存块的指针。
ptr2
       指向内存块的指针
num
       需要比较的字节数。

返回值

返回一个指示两个内存块的内容间的关系的整型值。

0值表示两个内存块的内容是相等的。

一个大于0的值表示,两个内存块中第一个不匹配的字节是ptr1的比ptr2的大,作为unsigned char类型比较的;小于0则表示相反。

Example:

[cpp] view plaincopyprint?
  1. /* memcmp example */  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. int main ()  
  6. {  
  7.   char str1[256];  
  8.   char str2[256];  
  9.   int n;  
  10.   printf ("Enter a sentence: "); gets(str1);  
  11.   printf ("Enter another sentence: "); gets(str2);  
  12.   n=memcmp ( str1, str2, 256 );  
  13.   if (n>0) printf ("'%s' is greater than '%s'.\n",str1,str2);  
  14.   else if (n<0) printf ("'%s' is less than '%s'.\n",str1,str2);  
  15.   else printf ("'%s' is the same as '%s'.\n",str1,str2);  
  16.   return 0;  
  17. }  

Output:

[cpp] view plaincopyprint?
  1. Enter a sentence: building  
  2. Enter another sentence: book  
  3. 'building' is greater than 'book'.  
0 0
原创粉丝点击