C/C++的内存处理函数
来源:互联网 发布:淘宝主图1920显示不全 编辑:程序博客网 时间:2024/05/22 10:55
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'.
0 0
- C/C++的内存处理函数
- C/C++内存处理函数
- C的内存函数
- 【C语言】 字符串的内存拷贝处理函数
- C\C++几个内存处理函数
- C语言内存处理函数原型实现
- C\C++几个内存处理函数
- C/C++几个内存处理函数
- c的内存分配函数
- c有关内存的函数
- C的字符处理函数
- C/C++中常用的字符串处理函数和内存字符串函数
- 处理C#, JAVA内存泄漏的方法
- 处理C#, JAVA内存泄漏的方法
- 【基础C&C++】内存函数
- 【C】C语言内存函数
- C语言字符串处理及内存管理函数
- C内存管理函数
- VB.net中获取二维数组的元素个数
- AppBarLayout的使用
- windows下配置Mysql-5.7.9服务
- [bzoj3531][SDOI2014]旅行
- SSL 1383 车Ⅱ 状压dp
- C/C++的内存处理函数
- 06.3.4工作笔记
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te
- 寒假刷题-最小生成树
- hdu4605 树状数组+离散化+dfs
- 显式调用构造函数
- 程序员每天自我洗脑和催眠
- C# 线程池中取消线程的三种方式
- 元素计算