成语相关 [英] idiom related

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

问题描述




大多数地方我们看到这样的代码 - 比如复制一个字符串


for(i = 0;( dest [i] = src [i]); i ++);


为什么它们不能被整理成复制或复制


memcpy(dest,src,strlen(src))。这是否更有效?





int * x =(int *)& dest [0];

int * y =(int *)& src [0];


* x = * y;


会更有效吗?


谢谢

解决方案

问道:





大多数地方我们都看到这样的代码 - 比如复制一个字符串


for(i = 0;(dest [i] = src [i]); i ++);



这实际上是罕见的。如果你想复制一个字符串,请使用strcpy - 或者

memcpy。


为什么它们不能被整合为整数复制或memcopied



为什么要施放它们?


>

memcpy (dest,src,strlen(src))。这更有效吗?



它已经坏了。你的意思是:


memcpy(dest,src,strlen(src)+ 1);


和它的/更少/更高效比strcpy(dest,src);


但是如果你碰巧知道了字符串的长度而且没有必要

重新计算它复制操作的目的,然后:


memcpy(dest,src,len);


可能比以下更快:


strcpy(dest,src);


虽然C标准不保证这一点。


int * x =(int *)& dest [0];



如果没有为int正确对齐dest,则出现未定义的行为。


int * y =(int *)& src [0];


* x = * y;


会更高效吗?



不保证它会起作用(见上文)。在任何情况下,如果有问题的

字符串比sizeof(int)字节长?


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Richard Heathfield写道:
< blockquote class =post_quotes>
但是如果你碰巧知道了字符串的长度而且没有必要

为了复制操作的目的重新计算它,那么:


memcpy(dest,src,len);



如果`len`是字符串长度,这不会复制终止nul。


哎呀? br />

-

克里斯进一步下跌 Dollin

我们没时间找到我们想知道的一切。 /一个Cy钹的冲突/


Chris Dollin说:


Richard Heathfield写道:


>但是如果您已经知道字符串长度并且不必为了复制操作而重新计算它,那么:

memcpy(dest,src,len);



如果`len`是字符串长度,这不会复制终止nul。


哎呀?



显然,取决于你提到的情况。我不知道你,

但是当我记录存储字符串所需的字节数时,我的
包括1为null终止符。所以对我来说这不是一个哎呀,但对于

其他人可能是。


是的,它是第一个如果一切都好了你会检查的东西

梨形。毕竟,我们都会犯错误。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Hi

Most of the places we see code like this - for example copying a string

for (i = 0; (dest[i] = src[i]); i++);

why can''t they be casted as integers and copied or memcopied

memcpy(dest, src, strlen(src)). Is this more efficient ?

or

int *x = (int *) &dest[0];
int *y = (int *) & src[0];

*x = *y;

Would it be more efficient?

Thanks

解决方案

Ask said:

Hi

Most of the places we see code like this - for example copying a string

for (i = 0; (dest[i] = src[i]); i++);

That''s rare, actually. If you want to copy a string, use strcpy - or perhaps
memcpy.

why can''t they be casted as integers and copied or memcopied

Why cast them?

>
memcpy(dest, src, strlen(src)). Is this more efficient ?

It''s broken. You meant:

memcpy(dest, src, strlen(src) + 1);

and it''s /less/ efficient than strcpy(dest, src);

But if you happen to know the string length already and don''t have to
calculate it afresh for the purposes of the copy op, then:

memcpy(dest, src, len);

is likely to be faster than:

strcpy(dest, src);

although the C standard does not guarantee this.

int *x = (int *) &dest[0];

Undefined behaviour if dest is not properly aligned for an int.

int *y = (int *) & src[0];

*x = *y;

Would it be more efficient?

There''s no guarantee it would work (see above). And in any case, what if the
string in question is longer than sizeof(int) bytes?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Richard Heathfield wrote:

But if you happen to know the string length already and don''t have to
calculate it afresh for the purposes of the copy op, then:

memcpy(dest, src, len);

If `len` is the string length, this won''t copy the terminating nul.

Oops?

--
Chris "falling further in" Dollin
"We did not have time to find out everything we wanted to know." /A Clash of Cymbals/


Chris Dollin said:

Richard Heathfield wrote:

>But if you happen to know the string length already and don''t have to
calculate it afresh for the purposes of the copy op, then:

memcpy(dest, src, len);


If `len` is the string length, this won''t copy the terminating nul.

Oops?

Depends, obviously, on the condition you mention. I don''t know about you,
but when I record the number of bytes required for storing a string, I
include 1 for the null-terminator. So for me it isn''t an oops, but for
someone else it might be.

And yes, it''s one of the first things you''d check if it all went
pear-shaped. We all make mistakes, after all.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于成语相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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