I have the following code to declare and assign a string on the heap.
我有以下代码来声明并在堆上分配一个字符串。
char *string = malloc(10);
string[9] = '\0';
strncpy(string, "welcometotherealworld", 9);
printf("string: %s\n", string);
Do I have to manually set the \0\
to ensure the string ends? string[9] = '\0';
我是否必须手动设置\ 0 \以确保字符串结束? string [9] ='\ 0';
Or, does strncpy
do this for me?
或者,strncpy会为我做这件事吗?
6 个解决方案
#1
2
strncpy
does not null terminate the destination array if the length of the source string (the second argument) is greater or equal than the value of the third argument.
如果源字符串的长度(第二个参数)大于或等于第三个参数的值,则strncpy不会终止目标数组。
So here:
strncpy(string, "welcometotherealworld", 9);
strncpy
will not null terminate.
strncpy不会为null终止。
#2
3
Two things: First malloc(10)
reserves 10 bytes, string[10]
addresses the eleventh byte, so that is illegal. Second: Yes you have to set string[9] to null, because according to the standard strncpy does not ensure the string is null terminated if the source string is longer than count.
两件事:第一个malloc(10)保留10个字节,字符串[10]寻址第11个字节,因此这是非法的。第二:是的,你必须将string [9]设置为null,因为根据标准strncpy,如果源字符串长于count,则不确保字符串为空终止。
#3
0
welcometotherealworld
is definitely longer than 9
characters, so strncpy
should not implicitly add a terminating character.
welcometotherealworld绝对超过9个字符,因此strncpy不应隐式添加终止字符。
#4
0
strncpy(dest, source, n)
copies at most n
bytes from the buffer pointed to by source
into the buffer pointer to by dest
. However, if strlen(source)
is greater than n
, then strncpy
will simply copy the first n
bytes and will not terminate the string dest
with a null byte because there is no space for it. Therefore to ensure that the buffer source
is always null terminated, you must do it yourself. What you are doing will always keep your buffer pointed to by string
null terminated.
strncpy(dest,source,n)将源指向的缓冲区中的最多n个字节复制到dest的缓冲区指针中。但是,如果strlen(source)大于n,则strncpy将简单地复制前n个字节,并且不会使用空字节终止字符串dest,因为它没有空间。因此,为了确保缓冲源始终为空终止,您必须自己完成。你正在做什么将始终保持你的缓冲区指向字符串null终止。
#5
0
strcpy
will always terminate the destination string with a \0
. strncpy
also normally NULL
terminates the string, but may not do. It copies a maximum number of bytes (n
) of the string, but unfortunately (in terms of a useful ABI) does not copy in a NULL if the number of bytes to be copied (i.e. the length of the source string including the terminating NULL
) exceeds the length specified (n
) . So, if you copy the string "1234567890"
(which is ten characters plus a NULL
, so 11) and pass 10
as the last argument to strncpy
, you will get an unterminated string of 10 characters.
strcpy将始终使用\ 0终止目标字符串。 strncpy通常也会NULL终止字符串,但可能不会。它复制字符串的最大字节数(n),但不幸的是(就有用的ABI而言)如果要复制的字节数(即包含终止NULL的源字符串的长度),则不会复制为NULL )超过指定的长度(n)。因此,如果您复制字符串“1234567890”(十个字符加上一个NULL,那么11)并将10作为strncpy的最后一个参数传递,您将获得一个10个字符的未终止字符串。
Here are some safe routes around this:
以下是一些安全的路线:
dest = malloc(10); /* allocate ten bytes */
strncpy (dest, src, 10); /* copy up to 10 bytes */
dest[9] = 0; /* overwrite the 10th byte with a zero if it was not one already */
dest = malloc(10); /* allocate ten bytes */
strncpy (dest, src, 9); /* copy up to 9 bytes */
dest[9] = 0; /* make the 10th byte zero */
dest = calloc(10, 1); /* allocate and zero ten bytes */
strncpy (dest, src, 9); /* copy up to 9 bytes, leaving the NULL in */
#6
0
To insure a proper '\0'
ending, code needs to set the '\0'
.malloc()
does not initialize the the data in string
.
为了确保正确的'\ 0'结束,代码需要设置'\ 0'。 malloc()不初始化string中的数据。
char *string = malloc(10);
strncpy(string, "welcometotherealworld", 9 /* or 10 */);
string[9] = '\0';
strncpy(char *s1, const char *s2, size_t n)
writes n
char
to s1
.
It first uses min(n, strlen(s2))
char
from s2
.
If more are needed, the remainder of s2
is written with null characters.
strncpy(char * s1,const char * s2,size_t n)将n char写入s1。它首先使用s2中的min(n,strlen(s2))char。如果需要更多,则s2的剩余部分用空字符写。
#1
2
strncpy
does not null terminate the destination array if the length of the source string (the second argument) is greater or equal than the value of the third argument.
如果源字符串的长度(第二个参数)大于或等于第三个参数的值,则strncpy不会终止目标数组。
So here:
strncpy(string, "welcometotherealworld", 9);
strncpy
will not null terminate.
strncpy不会为null终止。
#2
3
Two things: First malloc(10)
reserves 10 bytes, string[10]
addresses the eleventh byte, so that is illegal. Second: Yes you have to set string[9] to null, because according to the standard strncpy does not ensure the string is null terminated if the source string is longer than count.
两件事:第一个malloc(10)保留10个字节,字符串[10]寻址第11个字节,因此这是非法的。第二:是的,你必须将string [9]设置为null,因为根据标准strncpy,如果源字符串长于count,则不确保字符串为空终止。
#3
0
welcometotherealworld
is definitely longer than 9
characters, so strncpy
should not implicitly add a terminating character.
welcometotherealworld绝对超过9个字符,因此strncpy不应隐式添加终止字符。
#4
0
strncpy(dest, source, n)
copies at most n
bytes from the buffer pointed to by source
into the buffer pointer to by dest
. However, if strlen(source)
is greater than n
, then strncpy
will simply copy the first n
bytes and will not terminate the string dest
with a null byte because there is no space for it. Therefore to ensure that the buffer source
is always null terminated, you must do it yourself. What you are doing will always keep your buffer pointed to by string
null terminated.
strncpy(dest,source,n)将源指向的缓冲区中的最多n个字节复制到dest的缓冲区指针中。但是,如果strlen(source)大于n,则strncpy将简单地复制前n个字节,并且不会使用空字节终止字符串dest,因为它没有空间。因此,为了确保缓冲源始终为空终止,您必须自己完成。你正在做什么将始终保持你的缓冲区指向字符串null终止。
#5
0
strcpy
will always terminate the destination string with a \0
. strncpy
also normally NULL
terminates the string, but may not do. It copies a maximum number of bytes (n
) of the string, but unfortunately (in terms of a useful ABI) does not copy in a NULL if the number of bytes to be copied (i.e. the length of the source string including the terminating NULL
) exceeds the length specified (n
) . So, if you copy the string "1234567890"
(which is ten characters plus a NULL
, so 11) and pass 10
as the last argument to strncpy
, you will get an unterminated string of 10 characters.
strcpy将始终使用\ 0终止目标字符串。 strncpy通常也会NULL终止字符串,但可能不会。它复制字符串的最大字节数(n),但不幸的是(就有用的ABI而言)如果要复制的字节数(即包含终止NULL的源字符串的长度),则不会复制为NULL )超过指定的长度(n)。因此,如果您复制字符串“1234567890”(十个字符加上一个NULL,那么11)并将10作为strncpy的最后一个参数传递,您将获得一个10个字符的未终止字符串。
Here are some safe routes around this:
以下是一些安全的路线:
dest = malloc(10); /* allocate ten bytes */
strncpy (dest, src, 10); /* copy up to 10 bytes */
dest[9] = 0; /* overwrite the 10th byte with a zero if it was not one already */
dest = malloc(10); /* allocate ten bytes */
strncpy (dest, src, 9); /* copy up to 9 bytes */
dest[9] = 0; /* make the 10th byte zero */
dest = calloc(10, 1); /* allocate and zero ten bytes */
strncpy (dest, src, 9); /* copy up to 9 bytes, leaving the NULL in */
#6
0
To insure a proper '\0'
ending, code needs to set the '\0'
.malloc()
does not initialize the the data in string
.
为了确保正确的'\ 0'结束,代码需要设置'\ 0'。 malloc()不初始化string中的数据。
char *string = malloc(10);
strncpy(string, "welcometotherealworld", 9 /* or 10 */);
string[9] = '\0';
strncpy(char *s1, const char *s2, size_t n)
writes n
char
to s1
.
It first uses min(n, strlen(s2))
char
from s2
.
If more are needed, the remainder of s2
is written with null characters.
strncpy(char * s1,const char * s2,size_t n)将n char写入s1。它首先使用s2中的min(n,strlen(s2))char。如果需要更多,则s2的剩余部分用空字符写。