memcopy, memset, strcopy, strncpy用法總結

memcpyhtml

原型:extern void *memcpy(void *dest, void *src, unsigned int count);c++

用法:#include 數組

功能:由src所指內存區域複製count個字節到dest所指內存區域。安全

說明:srcdest所指內存區域不能重疊,函數返回指向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所指的數組中。

說明:srcdest所指內存區域不能夠重疊且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個字節。

      srcdest所指內存區域不能夠重疊且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

C代碼
  1. 第一種狀況:   
  2. char* p="how are you ?";   
  3. char name[20]="ABCDEFGHIJKLMNOPQRS";   
  4.   
  5.   
  6.   
  7. strcpy(name,p);                             //name改變爲"how are you ? OPQRS "      ====>錯誤!   
  8. strncpy(name,p,sizeof(name))      //name改變爲"how are you ?       "        ====>正確!   
  9.   
  10. 第二種狀況:   
  11. char* p="how are you ?";   
  12. char name[20];   
  13.   
  14. strcpy(name,p);                             //name改變爲"how are you ? 未知字符 "      ====>錯誤!   
  15. name[sizeof(name)-1]='\0'           //和上一步組合,獲得正確的結果!   
  16. strncpy(name,p,sizeof(name));        //name改變爲"how are you ?       "       ====>正確!   
  17.   
  18. 第三種狀況:   
  19. char* p="how are you ?";   
  20. char name[10];   
  21.   
  22. strcpy(name,p);                             //name改變爲"how are yo"      ====>無結束符'\0',錯誤!   
  23. name[sizeof(name)-1]='\0'              //和上一步組合,彌補結果。但要注意,字符傳遞錯誤!   
  24. strncpy(name,p,sizeof(name));      //和單純的一步strcpy結果同樣!   
  25.   
  26.   
  27.   
  28. 總結:strcpy   
  29. 若是源長>目標長,則將源長中等於目標長的字符拷貝到目標字符串   
  30. 若是源長<目標長,則源長所有拷貝到目標字符串,不包括'\0'  
  31. strncpy   
  32. 若是目標長>指定長>源長,則將源長所有拷貝到目標長,自動加上'\0'  
  33. 若是指定長<源長,則將源長中按指定長度拷貝到目標字符串,不包括'\0'  
  34. 若是指定長>目標長,error happen!   
  35.   
  36.   
  37.   
  38.   
  39.   
  40.   
  41.   
  42. /***************************************************************************************************   
  43.   
  44.   
  45. strcpy(),以源串中的'\0'爲拷貝結束標誌,直到遇到該NULL爲止,而後將NULL拷貝上.   
  46.   
  47. strncpy()以第三個參數N爲拷貝結束標誌,若是source的長度小於N,則剩餘的字符所有用NULL填充.   
  48.   
  49.                若是source的長度大於N,則從source中截取前N個字符,拷貝過去.  
相關文章
相關標籤/搜索