C\C++几个内存处理函数
来源:互联网 发布:2. 简述云计算 编辑:程序博客网 时间:2024/05/22 05:19
1、memcpy
void * memcpy ( void * destination, const void * source, size_t num );
复制内存块
从source指向的位置直接复制num个字节的值到destination指向的内存块。
对于这个函数,source和destination指向的对象的底层类型是无关的;结果是数据的一个二进制复制。
这个函数不检查任何结束空字符(null)—— 它总是准确地复制num个字节。
参数
- destination
- 指向内容被复制到的目的数组的指针,类型被转换为void*。
- source
- 指向复制数据的源数组的指针,类型被转换为void*。
- num
- 要复制的字节数。
返回值
返回目的指针destination。
Example:
/* memcpy example */#include <stdio.h>#include <string.h>int main (){ char str1[]="Sample string"; char str2[40]; char str3[40]; memcpy (str2,str1,strlen(str1)+1); memcpy (str3,"copy successful",16); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0;}
Output:
str1: Sample stringstr2: Sample stringstr3: copy successful
2、memmove
void * memmove ( void * destination, const void * source, size_t num );
移动内存块
从source指向的位置复制num个字节的值到destination指向的内存块。复制操作的发生就好像使用了中间缓存,允许destination和source重叠。
对于这个函数,source和destination指向的对象的底层类型是无关的;结果是数据的一个二进制复制。
这个函数不检查任何结束空字符(null)—— 它总是准确地复制num个字节。
参数
- destination
- 指向内容被复制到的目的数组的指针,类型被转换为void*。
- source
- 指向复制数据的源数组的指针,类型被转换为void*。
- num
- 要复制的字节数。
返回值
返回目的指针destination。
Example:
/* memmove example */#include <stdio.h>#include <string.h>int main (){ char str[] = "memmove can be very useful......"; memmove (str+20,str+15,11); puts (str); return 0;}
Output:
memmove can be very very useful.3、memset
void * memset ( void * ptr, int value, size_t num );
填充内存块
参数
- ptr
- 指向需要填充的内存块的指针。
- value
- 要设置的值,这个值是作为int型传递的,但是该函数用这个值的unsigned char类型转换值来填充内存块。
- num
- 要是指特定值的字节数。
返回值
返回指针ptr。
Example:
/* memset example */#include <stdio.h>#include <string.h>int main (){ char str[] = "almost every programmer should know memset!"; memset (str,'-',6); puts (str); return 0;}
Output:
------ 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:
/* memchr example */#include <stdio.h>#include <string.h>int main (){ char * pch; char str[] = "Example string"; pch = (char*) memchr (str, 'p', strlen(str)); if (pch!=NULL) printf ("'p' found at position %d.\n", pch-str+1); else printf ("'p' not found.\n"); return 0;}
Output:
'p' found at position 5.5、memcmp
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
比较两个内存块
参数
- ptr1
- 指向内存块的指针。
- ptr2
- 指向内存块的指针
- num
- 需要比较的字节数。
返回值
返回一个指示两个内存块的内容间的关系的整型值。
0值表示两个内存块的内容是相等的。
一个大于0的值表示,两个内存块中第一个不匹配的字节是ptr1的比ptr2的大,作为unsigned char类型比较的;小于0则表示相反。
Example:
/* memcmp example */#include <stdio.h>#include <string.h>int main (){ char str1[256]; char str2[256]; int n; printf ("Enter a sentence: "); gets(str1); printf ("Enter another sentence: "); gets(str2); n=memcmp ( str1, str2, 256 ); if (n>0) printf ("'%s' is greater than '%s'.\n",str1,str2); else if (n<0) printf ("'%s' is less than '%s'.\n",str1,str2); else printf ("'%s' is the same as '%s'.\n",str1,str2); return 0;}
Output:
Enter a sentence: buildingEnter another sentence: book'building' is greater than 'book'.
- C\C++几个内存处理函数
- C\C++几个内存处理函数
- C/C++几个内存处理函数
- C/C++内存处理函数
- C语言内存处理函数原型实现
- C/C++的内存处理函数
- C语言内存管理的几个函数介绍与区别
- C语言中常用的几个内存申请函数
- c中动态开辟内存的几个函数
- C语言字符串处理的几个常用函数
- C语言中常用的几个字符串处理函数:
- C语言当中几个获取时间和处理的函数
- 几个内存相关面试题(c/c++)
- 几个内存相关面试题(c/c++)
- 【基础C&C++】内存函数
- 【C】C语言内存函数
- C语言字符串处理及内存管理函数
- 【C语言】 字符串的内存拷贝处理函数
- 如何修改Android模拟器的hosts文件
- apriori 算法
- 使用ld的wrap选项替换已有库函数
- 链接队列的基本算法
- WIN7 系统下 android 环境变量配置
- C\C++几个内存处理函数
- Android江湖(含CD光盘1张)
- C++程序设计教程_基于Visual.Studio.2008
- 关于initializer element is not constant错误
- 进程互斥与竞态
- android 如何获取当前时间
- qeum配置
- 十条 Linux 新手必知
- 写代码的,都是诗人