std::memset、std::memcpy和std::strcpy的区别
来源:互联网 发布:淘宝提前收款服务费 编辑:程序博客网 时间:2024/06/04 07:15
// http://www.cnblogs.com/kevinGaoblog/archive/2012/06/17/2552796.html
memset
Fill block of memory
void * memset ( void * ptr, int value, size_t num );
Parameters
- ptr
- Pointer to the block of memory to fill.
- value
- Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
- num
- Number of bytes to be set to the value.
Return Value
ptr is returned.
Example
#include <iostream>#include <cstring>using namespace std;int main (){ char str[] = "almost every programmer should know memset!"; memset (str,'-',6); cout<<str; return 0;}
Output:
1 ------ every programmer should know memset!
memcpy
Copy block of memory
void * memcpy ( void * destination, const void * source, size_t num );
The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
The function does not check for any terminating null character in source - it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at leastnum bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).
Parameters
- destination
- Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
- source
- Pointer to the source of data to be copied, type-casted to a pointer of type void*.
- num
- Number of bytes to copy.
Return Value
destination is returned.
Example
#include <iostream>#include <cstring>using namespace std;int main (){ char str1[]="Sample string"; char str2[40]; char str3[40]; memcpy (str2,str1,strlen(str1)+1); memcpy (str3,"copy successful",16); cout<<"str1:"<<str1<<endl; cout<<"str2:"<<str2<<endl; cout<<"str3:"<<str3<<endl; return 0;}
output:
1 str1: Sample string2 str2: Sample string3 str3: copy successful
strcpy
Copy characters from string
char * strcpy ( char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the terminating null character.To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
Parameters
- destination
- Pointer to the destination array where the content is to be copied.
- source
- C string to be copied.
Return Value
destination is returned.
Example
#include <iostream>#include <cstring>using namespace std;int main (){ char str1[]= "Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); cout<<"str1:"<<str1<<endl; cout<<"str2:"<<str2<<endl; cout<<"str3:"<<str3<<endl; return 0;}
Output:
1 str1: Sample string2 str2: Sample string3 str3: copy successful
- std::memset、std::memcpy和std::strcpy的区别
- std::strcpy、strncpy、memset、memcpy用法
- STD*_FILENO 和 std*的区别
- std::lock_guard 和 std::unique_lock的区别
- std::strcpy()
- std::vector<std::string>::size_type和 int的区别
- std::set::lower_bound 和std::lower_bound的区别
- std::set::lower_bound 和std::lower_bound的区别
- numpy中std()和pandas中std()的区别
- std
- std
- std
- C++:memset ,memcpy 和strcpy 的区别
- memset ,memcpy 和strcpy 的根本区别?
- memset ,memcpy 和strcpy 的根本区别?
- memset ,memcpy 和strcpy 的根本区别!!!!
- memset ,memcpy 和strcpy 的根本区别?
- memset ,memcpy 和strcpy 的根本区别
- 集深V5-定位与格集-层次坐标
- android wifi驱动开发日记(二)(三)
- Eclipse配置tomcat
- java中HashCode使用
- eclipse配置junit
- std::memset、std::memcpy和std::strcpy的区别
- double ceil(double x);
- Linux/CentOS 笔记集
- 基于DM8168高清视频自动跟踪模块
- keil优化等级设置
- RAC 数据库中的 'log file sync' 等待事件
- Delphi For iOS开发指南(18):让Delphi XE4开发的iOS应用显示为中文名称
- 九段秘书论
- 数据结构和算法系列课程(02) --- 线性表和贪吃蛇