void
*
memcpy
(
void
*dst,
const
void
*src,
size_t
n);
//If copying takes place between objects that overlap, the behavior is undefined
因此标准库提供了地址重叠时的内存拷贝函数:memmove(),
该函数把源字符串拷贝到临时buf里,然后再从临时buf里写到目的地址
strcpy and memcpy
memcpy 可以进行指定字节数量的任意类型数据的拷贝, 可以用于内存拷贝。
char a[100], b[50];
memcpy(b, a,sizeof(b)); //not sizeof(a),which may cause overflow
strcpy只能用于拷贝字符串,遇到' '终止拷贝。
char a[100], b[50];
strcpy(a,b);
void *memset(void *buffer,int c,int count);
把buffer所指内存区域的前count个字节设置成字符c, 一般用于对指定的字符串清零。
below from http://www.cnblogs.com/aprilapril/p/4333173.html
..........strdup("adm"); //as it shows itself, only valid to string type.
memcpy function,return an adress pointed at heap which store"adm"(with string end sign).
.........strcpy(pAim, "4021");
"4021" is copied into the memory started at address pAim.
...........memcpy(void*to,void*from,int size)
copy designated size to the memory.
..........memcmp(void *a, void *b, int size)
copare two storage, if equal,return to 0,otherwise, get a positive or negative value.
estrdup
#include <config.h> #include <stdlib.h> #include <err.h> #include "roken.h" /* * Like strdup but never fails. */ ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL estrdup (const char *str) { char *tmp = strdup (str); if (tmp == NULL) errx (1, "strdup failed"); return tmp; }