查看文章strncpy()功能更好的文章
strncpy()函數(shù)
原型:extern char *strncpy(char *dest, char *src, int n);
使用方法:#include <string.h>
功能:把src所指由NULL結(jié)束的字符串的前n個(gè)字節(jié)拷貝到dest所指的數(shù)組中。
說明:假設(shè)src的前n個(gè)字節(jié)不含NULL字符,則結(jié)果不會(huì)以NULL字符結(jié)束。
假設(shè)src的長度小于n個(gè)字節(jié),則以NULL填充dest直到復(fù)制完n個(gè)字節(jié)。
src和dest所指內(nèi)存區(qū)域不能夠重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針(該指向dest的最后一個(gè)元素)
相關(guān)函數(shù):memccpy,memcpy,stpcpy,strcpy
strcpy ,strncpy ,strlcpy地使用方法
好多人已經(jīng)知道利用strncpy替代strcpy來防止緩沖區(qū)越界。
可是假設(shè)還要考慮執(zhí)行效率的話,或許strlcpy是一個(gè)更好的方式。
1. strcpy
我們知道,strcpy 是根據(jù) 作為結(jié)束推斷的,假設(shè) to 的空間不夠。則會(huì)引起 buffer overflow。
strcpy 常規(guī)的實(shí)現(xiàn)代碼例如以下(來自 OpenBSD 3.9):
char *
strcpy(char *to, const char *from)
{
char *save = to;
for (; (*to = *from) != ''; ++from, ++to);
return(save);
}
但通常,我們的 from 都來源于用戶的輸入,非常可能是非常大的一個(gè)字符串。因此 strcpy 不夠安全。
2. strncpy
在 ANSI C 中,strcpy 的安全版本號(hào)是 strncpy。
char *strncpy(char *s1, const char *s2, size_t n);
但 strncpy 其行為是非常詭異的(不符合我們的通常習(xí)慣)。
標(biāo)準(zhǔn)規(guī)定 n 并非 sizeof(s1),而是要復(fù)制的 char 的個(gè)數(shù)。一個(gè)最常見的問題,就是 strncpy 并不幫你保證
結(jié)束。
char buf[8];
strncpy( buf, "abcdefgh", 8 );
看這個(gè)程序。buf 將會(huì)被 "abcdefgh" 填滿,但卻沒有 結(jié)束符了。
另外,假設(shè) s2 的內(nèi)容比較少,而 n 又比較大的話,strncpy 將會(huì)把之間的空間都用 填充。這又出現(xiàn)了一個(gè)效率上的問題,例如以下:
char buf[80];
strncpy( buf, "abcdefgh", 79 );
上面的 strncpy 會(huì)填寫 79 個(gè) char,而不不過 "abcdefgh" 本身。
strncpy 的標(biāo)準(zhǔn)使用方法為:(手工寫上 )
strncpy(path, src, sizeof(path) - 1);
path[sizeof(path) - 1] = '';
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,就不須要我們?nèi)ナ謩?dòng)負(fù)責(zé) 了,僅須要把 sizeof(dst) 告之 strlcpy 就可以:
strlcpy(path, src, sizeof(path));
len = strlen(path);
if ( len >= sizeof(path) )
printf("src is truncated.");
而且 strlcpy 傳回的是 strlen(str),因此我們也非常方便的能夠推斷數(shù)據(jù)是否被截?cái)唷?/p>
[* 一點(diǎn)點(diǎn)歷史 *]
strlcpy 并不屬于 ANSI C,至今也還不是標(biāo)準(zhǔn)。
strlcpy 來源于 OpenBSD 2.4,之后非常多 unix-like 系統(tǒng)的 libc 中都增加了 strlcpy 函數(shù),我個(gè)人在 FreeBSD、Linux 里面都找到了 strlcpy。(Linux使用的是 glibc,
glibc里面有 strlcpy,則全部的 Linux 版本號(hào)也都應(yīng)該有 strlcpy)
但 Windows 下是沒有 strlcpy 的,相應(yīng)的是strcpy_s函數(shù)
///////////////////////////////////////////////////////////////////////////
strncpy
原型:extern char *strncpy(char *dest, char *src, int n);
使用方法:#include <string.h>
功能:把src所指由NULL結(jié)束的字符串的前n個(gè)字節(jié)拷貝到dest所指的數(shù)組中。
說明:
假設(shè)src的前n個(gè)字節(jié)不含NULL字符,則結(jié)果不會(huì)以NULL字符結(jié)束。
假設(shè)src的長度小于n個(gè)字節(jié),則以NULL填充dest直到復(fù)制完n個(gè)字節(jié)。
src和dest所指內(nèi)存區(qū)域不能夠重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。
舉例:
// strncpy.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char *d="Hello, GGV Programmers";
char *p=strdup(s);
clrscr();
textmode(0x00); // enable 6 lines mode
strncpy(d,s,strlen(s));
printf("%s
",d);
strncpy(p,s,strlen(d));
printf("%s",p);
getchar();
return 0;
}
總結(jié)
以上是生活随笔為你收集整理的查看文章strncpy()功能更好的文章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈利波特斯内普守护神怎么用?斯内普守护神
- 下一篇: 排列组合十一个性质公式及证明,错排数公式