使用malloc-ed char *的for循环中的strlen [英] strlen in a for loop with malloc-ed char*

查看:82
本文介绍了使用malloc-ed char *的for循环中的strlen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码有什么问题?有人告诉我,在for循环中有一个很糟糕的做法是

strlen,但我并不完全明白。请问任何人

用简单的英语解释我吗?


char * reverse(char * s)

{

int i;

char * r;

如果(!s)返回NULL; //错误

r = calloc( strlen(s)+ 1,sizeof(char));

如果(!r)返回NULL;

for(i = 0; i< strlen(s); i ++)

*(r + i)= *(s + strlen(s)-1-i);

返回r;

}


-

它不对。它甚至都没有错

解决方案

2004年10月19日星期二18:53:37 +0000,apropo写道:
< blockquote class =post_quotes>这段代码有什么问题?有人告诉我,有一个不好的做法,
strlen在for循环,但我不完全明白。请问有人用简单的英语解释我吗?
你多次计算strlen(),这真的不需要char * reverse(char * s)
{i /;
char * r;
size_t len; if(!s)返回NULL; // ERROR
len = strlen(s);

r = calloc(len + 1,sizeof(char)); if(!r)返回NULL;
for(i = 0; i< len; i ++)

*(r + i)= *(s + len-1-i);返回r;
}




apropo写道:

有什么问题这段代码?有人告诉我,有一个不好的做法,
strlen在for循环,但我不完全明白。请问有人用简单的英语解释我吗?

char * reverse(char * s)
{
int i;
char * r;
if(!s)返回NULL; //错误
r = calloc(strlen(s)+ 1,sizeof(char));
if(!r)返回NULL;
for(i = 0; i< strlen(s); i ++)
*(r + i)= *(s + strlen(s)-1-i);
返回r;
}




在循环中计算一些不变的东西是浪费的。

s的长度是多少。这个函数没有改变,所以你可能会考虑将它作为一个const char *来反映它。无论如何,

更常见的是:


size_t len = strlen(s);


And然后在你使用strlen的地方使用len。有可能

优化器可能会为你解决这个问题,但为什么要求

麻烦?


代码不是' 't"错误',只是在不需要的地方烧掉CPU。

calloc也有点浪费,因为你正在把你想要的内存归零了。填写只是为了得到nul

终止,你可以只有r [len] =''\'''; mallocing后。

无论如何。


-David




apropo < FL *********** @ yahoo.com>写了


for(i = 0; i< strlen(s); i ++)
*(r + i)= *(s + strlen(s)-1- i);



想象一下,字符串s很大。然后记住计算机将如何实现strlen()(如果你不确定的话,自己编写函数)。


你熟悉Big O表示法吗?对于算法?这告诉您在给定大小的输入中需要多少

操作。例如,乘以
重复加法是O(N);你需要添加数字,因为较小的

很大。要使用你在小学教过的算法是O(log

N),因为你需要的操作数量增加了

数字。添加算法对于最多十个左右的数字是可以的,但对于

大数学,小学的方式要优越得多。


同样,你的代码是对于相当短的字符串可以,但对于长字符串,你可以大大降低速度。你能解决它的大O符号吗?


what is wrong with this code? someone told me there is a BAD practice with
that strlen in the for loop, but i don''t get it exactly. Could anyone
explain me in plain english,please?

char *reverse(char *s)
{
int i;
char *r;
if(!s) return NULL;//ERROR
r=calloc(strlen(s)+1,sizeof(char));
if(!r) return NULL;
for(i=0;i<strlen(s);i++)
*(r+i) = *(s+strlen(s)-1-i);
return r;
}

--
it''s not right. it''s not even wrong

解决方案

On Tue, 19 Oct 2004 18:53:37 +0000, apropo wrote:

what is wrong with this code? someone told me there is a BAD practice with
that strlen in the for loop, but i don''t get it exactly. Could anyone
explain me in plain english,please? You calculate the strlen() many times, which really isn''t needed char *reverse(char *s)
{
int i;
char *r; size_t len; if(!s) return NULL;//ERROR len = strlen(s);
r=calloc(len+1,sizeof(char)); if(!r) return NULL; for(i=0;i<len;i++)
*(r+i) = *(s+len-1-i); return r;
}




apropo wrote:

what is wrong with this code? someone told me there is a BAD practice with
that strlen in the for loop, but i don''t get it exactly. Could anyone
explain me in plain english,please?

char *reverse(char *s)
{
int i;
char *r;
if(!s) return NULL;//ERROR
r=calloc(strlen(s)+1,sizeof(char));
if(!r) return NULL;
for(i=0;i<strlen(s);i++)
*(r+i) = *(s+strlen(s)-1-i);
return r;
}



Computing something within a loop that is invarient is wasteful.
The length of "s" isn''t changed by this function, so you might
consider taking it in as a const char * to reflect that. Anyway,
more normal would be to do:

size_t len = strlen(s);

And then use len where you are using strlen(s). It is possible
that the optimizer might fix this for you, but why ask for
trouble?

The code isn''t "wrong", just burns CPU where it need not.
The calloc is also slightly wasteful, in that you are zeroing
out the memory you are about to fill in just to get the nul
termination, you could just have r[len] = ''\0''; after mallocing it.
Whatever.

-David



"apropo" <fl***********@yahoo.com> wrote


for(i=0;i<strlen(s);i++)
*(r+i) = *(s+strlen(s)-1-i);


Imagine that the string s is huge. Then remember how the computer will
implement strlen() (write the function yourself, if you are not sure).

Are you familiar with Big O notation for algorithms? This tells you how many
operations you need for a given size of input. For instance to multiply by
repeated adding is O(N); you need to add the numbers as often as the smaller
is large. To use the algorithm you were taught at primary school is O(log
N), since the numbers of operations you require goes up by the number of
digits. The adding algorithm is OK for numbers up to ten or so, but for
large numbers the primary school way is far superior.

Similarly, your code is OK for fairly short strings, but for long ones you
will slow things down considerably. Can you work out its Big O notation?


这篇关于使用malloc-ed char *的for循环中的strlen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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