std::memset、std::memcpy和std::strcpy的区别

memset

Fill block of memory

<cstring>
void * memset ( void * ptr, int value, size_t num );
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

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

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int main ()
 6 {
 7   char str[] = "almost every programmer should know memset!";
 8   memset (str,'-',6);
 9   cout<<str;
10   return 0;
11 }

Output:

1 ------ every programmer should know memset!

 

memcpy

Copy block of memory

<cstring>
void * memcpy ( void * destination, const void * source, size_t num );
Copies the values of num bytes from the location pointed by source directly to the memory block pointed bydestination.

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

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int main ()
 6 {
 7   char str1[]="Sample string";
 8   char str2[40];
 9   char str3[40];
10 
11   memcpy (str2,str1,strlen(str1)+1);
12   memcpy (str3,"copy successful",16);
13 
14   cout<<"str1:"<<str1<<endl;
15   cout<<"str2:"<<str2<<endl;
16   cout<<"str3:"<<str3<<endl;
17 
18   return 0;
19 }

 

output:

1 str1: Sample string
2 str2: Sample string
3 str3: copy successful

strcpy

Copy characters from string

<cstring>

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

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 int main ()
 5 {
 6   char str1[]= "Sample string";
 7   char str2[40];
 8   char str3[40];
 9   strcpy (str2,str1);
10   strcpy (str3,"copy successful");
11   
12   cout<<"str1:"<<str1<<endl;
13   cout<<"str2:"<<str2<<endl;
14   cout<<"str3:"<<str3<<endl;
15 
16   return 0;
17 }

Output:

1 str1: Sample string
2 str2: Sample string
3 str3: copy successful

posted on 2012-06-17 19:20  NLP新手  阅读(3069)  评论(0编辑  收藏  举报

导航