memcpyhtml
原型:extern void *memcpy(void *dest, void *src, unsigned int count);c++
用法:#include 數組
功能:由src所指內存區域複製count個字節到dest所指內存區域。安全
說明:src和dest所指內存區域不能重疊,函數返回指向dest的指針。能夠拿它拷貝任何數據類型的對象。app
舉例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),會形成b的內存地址溢出。less
memset函數
原型:extern void *memset(void *buffer, int c, int count);spa
用法:#include unix
功能:把buffer所指內存區域的前count個字節設置成字符c。指針
說明:返回指向buffer的指針。用來對一段內存空間所有設置爲某個字符。
舉例:char a[100];memset(a, ''\0'', sizeof(a));
memset能夠方便的清空一個結構類型的變量或數組。
如:
struct sample_struct
{
char csName[16];
int iSeq;
int iType;
};
對於變量
struct sample_strcut stTest;
通常狀況下,清空stTest的方法:
stTest.csName[0]=''\0'
stTest.iSeq=0;
stTest.iType=0;
用memset就很是方便:
memset(&stTest,0,sizeof(struct sample_struct));
若是是數組:
struct sample_struct TEST[10];
則
memset(TEST,0,sizeof(struct sample_struct)*10);]
strcpy
原型:extern char *strcpy(char *dest,char *src);
用法:#include
功能:把src所指由NULL結束的字符串複製到dest所指的數組中。
說明:src和dest所指內存區域不能夠重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。
例:char a[100],b[50];strcpy(a,b);如用strcpy(b,a),要注意a中的字符串長度(第一個‘\0’以前)是否超過50位,如超過,則會形成b的內存地址溢出。
strncpy
原型:extern char *strncpy(char *dest, char *src, int n);
用法:#include
功能:把src所指由NULL結束的字符串的前n個字節複製到dest所指的數組中。
說明:若是src的前n個字節不含NULL字符,則結果不會以NULL字符結束。
若是src的長度小於n個字節,則以NULL填充dest直到複製完n個字節。
src和dest所指內存區域不能夠重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。
STRNCPY的使用方法及與STRCPY的區別
strcpy ,strncpy ,strlcpy
好多人已經知道利用strncpy替代strcpy來防止緩衝區越界。
可是若是還要考慮運行效率的話,也許strlcpy是一個更好的方式。
1. strcpy
咱們知道,strcpy 是依據 \0 做爲結束判斷的,若是 to 的空間不夠,則會引發 buffer overflow。
strcpy 常規的實現代碼以下(來自 OpenBSD 3.9):
char *
strcpy(char *to, const char *from)
{
char *save = to;
for (; (*to = *from) != '\0'; ++from, ++to);
return(save);
}
但一般,咱們的 from 都來源於用戶的輸入,極可能是很是大的一個字符串,所以 strcpy 不夠安全。
2. strncpy
在 ANSI C 中,strcpy 的安全版本是 strncpy。
char *strncpy(char *s1, const char *s2, size_t n);
但 strncpy 其行爲是很詭異的(不符合咱們的一般習慣)。標準規定 n 並非 sizeof(s1),而是要複製的 char 的個數。①一個最多見的問題,就是 strncpy 並不幫你保證 \0 結束。
char buf[8];
strncpy( buf, "abcdefgh", 8 );
看這個程序,buf 將會被 "abcdefgh" 填滿,但卻沒有 \0 結束符了。
②另外,若是 s2 的內容比較少,而 n 又比較大的話,strncpy 將會把之間的空間都用 \0 填充。這又出現了一個效率上的問題,以下:
char buf[80];
strncpy( buf, "abcdefgh", 79 );
上面的 strncpy 會填寫 79 個 char,而不單單是 "abcdefgh" 自己。
strncpy 的標準用法爲:(手工寫上 \0)
strncpy(path, src, sizeof(path) - 1);
path[sizeof(path) - 1] = '\0';
len = strlen(path);
3. strlcpy
// Copy src to string dst of size siz. At most siz-1 characters
// will be copied. Always NUL terminates (unless siz == 0).
// Returns strlen(src); if retval >= siz, truncation occurred.
size_t
strlcpy(char *dst, const char *src, size_t siz);
而使用 strlcpy,就不須要咱們去手動負責 \0 了,僅須要把 sizeof(dst) 告之 strlcpy 便可:
strlcpy(path, src, sizeof(path));
len = strlen(path);
if ( len >= sizeof(path) )
printf("src is truncated.");
而且 strlcpy 傳回的是 strlen(str),所以咱們也很方便的能夠判斷數據是否被截斷。
[* 一點點歷史 *]
strlcpy 並不屬於 ANSI C,至今也還不是標準。
strlcpy 來源於 OpenBSD 2.4,以後不少 unix-like 系統的 libc 中都加入了 strlcpy 函數,我我的在 FreeBSD、Linux 裏面都找到了 strlcpy。(Linux使用的是 glibc,glibc裏面有 strlcpy,則全部的 Linux 版本也都應該有 strlcpy)
但 Windows 下是沒有 strlcpy 的,對應的是strcpy_s函數
文章出處:http://www.diybl.com/course/3_program/c++/cppjs/2008819/136391.html