关于clc字符串lib的问题 [英] Question about the clc string lib

查看:124
本文介绍了关于clc字符串lib的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的函数中,大小可以是0(零)吗?

char * clc_strdup(const char * CLC_RESTRICT s)

{

size_t size;

char * p;


clc_assert_not_null(clc_strdup,s);


size = strlen(s)+ 1;

if(size == 0)

p = NULL;

else if((p = malloc(size))!= NULL)

memcpy(p,s,size);


返回p;

}

解决方案

2006-01-26,Jeff< jo ****** @ gmail.com>写道:

在下面的函数中,大小是否可以为0(零)?


我从来没有听说过clc string lib - 我在哪里可以找到它?

char * clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char * p;

clc_assert_not_null(clc_strdup,s);


不知道这是如何工作的,需要函数指针作为第一个参数的
。它是一个将其第一个参数字符串化的宏吗?

打印错误?

size = strlen(s)+ 1;


此作业的结果不能为零。

if(size == 0)
p = NULL;


坚持 - 我收回了。如果strlen返回SIZE_MAX,则size可以为0.

else if((p = malloc(size))!= NULL)
memcpy(p,s,size);

return p;
}




顺便提一句,说到前缀,str *前缀和保留的

标识符,
#define strdup clc_strdup

合法吗?我对标准的解读说是,但我想在这里问。


Jeff写道:

在下面的函数中,可以size是0(零)?

char * clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char * p;

clc_assert_not_null(clc_strdup,s);

size = strlen(s)+ 1;
if(size == 0)
p = NULL;
else if((p = malloc(size))!= NULL)
memcpy(p,s,size);

返回p;
}




如果s是一个以空字符结尾的字符串,即使s [0] ==''\''',那么s

的strlen将为0当你添加一个,在这种情况下,大小不能为0.


如果s的长度超过size_t(在我的系统上,那是'

SIZE_MAX = UNIT32_MAX = 4294967295 [+ 1],然后大小将变为0.再次,作为

,你加1吨那个,我看不出大小如何为零。


-

============ ==

*不是学生*

==============


< blockquote> 2006-01-26,pemo< us *********** @ gmail.com>写道:

Jeff写道:

在下面的函数中,大小可以是0(零)吗?

char * clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char * p;

clc_assert_not_null(clc_strdup,s);

size = strlen( s)+ 1;
if(size == 0)
p = NULL;
else if((p = malloc(size))!= NULL)
memcpy(p ,s,size);

返回p;
}



如果s是以空字符结尾的字符串,即使s [0] == ''\''',然后s
的strlen将为0.当你添加一个,在这种情况下,大小不能为0.

如果s是这样的它溢出size_t的长度(在我的系统上是'SIZE_MAX = UNIT32_MAX = 4294967295 [+ 1],然后大小将变为0.再次,当你添加1时,我可以我们看不出大小如何为零。




除外,如果s _is_ SIZE_MAX的长度,然后随后添加1

将溢出。


假设我们有一个不现实的系统,其中SIZE_MAX为63. [清楚不是
ISO兼容,但我不想打出65535个字符]


然后,字符串

" ; abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ 0123456789"


的长度为63,33 + 1将换行为0.


In the function below, can size ever be 0 (zero)?

char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;

clc_assert_not_null(clc_strdup, s);

size = strlen(s) + 1;
if (size == 0)
p = NULL;
else if ((p = malloc(size)) != NULL)
memcpy(p, s, size);

return p;
}

解决方案

On 2006-01-26, Jeff <jo******@gmail.com> wrote:

In the function below, can size ever be 0 (zero)?
I''ve never heard of the "clc string lib" - where can i find it?
char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;

clc_assert_not_null(clc_strdup, s);
No idea how this would work in a way that needs the function pointer as
its first argument. Is it a macro that stringizes its first argument to
print an error?

size = strlen(s) + 1;
The result of this assignment cannot be zero.
if (size == 0)
p = NULL;
hold on - i take that back. size can be 0 if strlen returns SIZE_MAX.
else if ((p = malloc(size)) != NULL)
memcpy(p, s, size);

return p;
}



Incidentally, speaking of prefixes, the str* prefix, and reserved
identifiers, is something along the lines of

#define strdup clc_strdup

legal? My reading of the standard says it is, but thought i''d ask here.


Jeff wrote:

In the function below, can size ever be 0 (zero)?

char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;

clc_assert_not_null(clc_strdup, s);

size = strlen(s) + 1;
if (size == 0)
p = NULL;
else if ((p = malloc(size)) != NULL)
memcpy(p, s, size);

return p;
}



If s is a null terminated string, even if s[0] == ''\0'', then strlen of s
will be 0. As you add one to that, in this case, size cannot be 0.

if s is of such a length that it overflows a size_t (on my system that''s
SIZE_MAX = UNIT32_MAX = 4294967295 [+ 1], then size will go to 0. Again, as
you add 1 to that, I can''t see how size can ever be zero.

--
==============
*Not a pedant*
==============


On 2006-01-26, pemo <us***********@gmail.com> wrote:

Jeff wrote:

In the function below, can size ever be 0 (zero)?

char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;

clc_assert_not_null(clc_strdup, s);

size = strlen(s) + 1;
if (size == 0)
p = NULL;
else if ((p = malloc(size)) != NULL)
memcpy(p, s, size);

return p;
}



If s is a null terminated string, even if s[0] == ''\0'', then strlen of s
will be 0. As you add one to that, in this case, size cannot be 0.

if s is of such a length that it overflows a size_t (on my system that''s
SIZE_MAX = UNIT32_MAX = 4294967295 [+ 1], then size will go to 0. Again, as
you add 1 to that, I can''t see how size can ever be zero.



Except, if the length of s _is_ SIZE_MAX, then subsequently adding 1
will "overflow".

suppose we have an unrealistic system where SIZE_MAX is 63. [clearly not
ISO compliant, but i don''t want to type out 65535 characters]

then, the string
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ 0123456789"

has a length of 63, and 63+1 will wrap to 0.


这篇关于关于clc字符串lib的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆