补充C频繁答案 [英] supplementary C frequent answers

查看:52
本文介绍了补充C频繁答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几年里,特别是最近,我已经建立了一些股票。补充C FAQ的答案。如果

有人想查看和评论它们,我现在就把它们放在我的网页上,
http://benpfaff.org/writings/clc


- -

char a [] =" \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b []

= { 0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},* p =

b,x,i = 24; for(; p + =!* p; * p / = 4)switch (x = * p& 3)case 0:{return 0; for(p - ; i - ; i - )case

2:{i ++; if(1)break; else默认值:continue; if(0)case 1:putchar(a [i& 15]); break;}}}

解决方案

" Ben普法夫" < bl*@cs.stanford.edu>在消息中写道

news:87 ************ @ pfaff.stanford.edu ...

在过去的几年里,特别是最近,我已经建立了一些股票。补充C FAQ的答案。如果有人想要评论和评论它们,我现在就把它们放在我的网页上,
http://benpfaff.org/writings/clc




来自该网站:

"将strncpy()用于大缓冲区可能效率非常低。 strncpy()

总是

写入目标缓冲区中的每个字节,这会浪费很多时间

时间

如果目标缓冲区比源字符串长得多。


这不是QoI问题吗?根据标准,


" strncpy函数从数组中复制不超过n个字符(

跟随空字符的字符不被复制)由s2指向

由s1指向的数组。如果在

重叠的对象之间进行复制,则行为未定义。


请注意部分内容。

也来自您的网站,


"如果源字符串长度超过目标缓冲区的大小,则

然后strncpy()不会写一个终止空值。所以调用strncpy()必须



然后在

目的地的末尾显式写一个空终止符
在大多数情况下,
缓冲区。


没什么更简单的:


strncpy(dest,src,n)[n-1 ] = 0;


IMNSHO,strncpy()比strcpy()更可取。


最后,非主题指导消息"在

特定线程的上下文中是一个很好的标题。作为一般指导,例如,在你的网站上,它很可能会被忽视(我从不写偏离主题的帖子,为什么我应该浪费

我的时间阅读它?)。

像comp.lang.c topicality guidance这样的东西。可能会更好。只是我的

£0.02。


Peter


Peter Pichler写道:
< blockquote class =post_quotes>" Ben Pfaff" < bl*@cs.stanford.edu>在消息中写道
新闻:87 ************ @ pfaff.stanford.edu ...

在过去的几年里,特别是最近,我已经建立了一些股票。补充C FAQ的答案。如果有人想要评论和评论它们,我现在就把它们放在我的网页上,
http://benpfaff.org/writings/clc
从该网站:
使用strncpy()成为一个大的缓冲可能非常低效。 strncpy()总是写入目标缓冲区中的每个字节
,如果目标缓冲区比源字符串长得多,这会浪费很多时间。

这不是QoI问题吗?




No.

根据标准,

" strncpy函数从s2指向的数组复制不超过n个字符(
跟随空字符的字符不被复制)到s1指向的数组。如果在重叠的对象之间进行复制,则行为是不确定的。

请注意该部分是否为parenteses。


请注意下一句:如果s2指向的数组是一个短于n个字符的

字符串,则追加空字符

s1指向的数组中的副本,直到所有的n个字符都已写入



也来自您的网站,

"如果源字符串长于目标缓冲区的大小,则
然后strncpy()不会写入终止空值。所以在strncpy()
之后必须跟着在大多数情况下在
目标缓冲区的末尾显式写一个空终止符。

没有什么比这更简单:

strncpy(dest,src,n)[n-1] = 0;


但是很容易忘记这样做,并且它假定dest至少为
字节长,这可能不是。

IMNSHO,strncpy()比strcpy()更可取。


这就像说用螺丝刀比锤子更合理。这取决于你想要做什么的
。 strncpy'的工作是复制字符串的一部分,

而strcpy'的工作是复制整个字符串。如果您的目标是复制整个字符串,那么使用strncpy

是愚蠢的。

最后,偏离主题的指导消息在
特定线程的上下文中是一个很好的标题。作为一般指导,例如,在你的网站上,它很可能会被忽视(我从不写偏离主题的帖子,为什么我要浪费时间阅读它?。)
类似于 comp.lang.c topicality guidance可能会更好。只是我的
£0.02。




更好,它建议为你的

问题寻找最佳新闻组的指导对我来说(相当冗长,但也许更友好)

可能性。


-

Richard Heathfield: bi **** @ eton.powernet.co.uk

" Usenet is a奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


Peter Pichler写道:


Ben Pfaff < bl*@cs.stanford.edu>在消息中写道
新闻:87 ************ @ pfaff.stanford.edu ...

在过去的几年里,特别是最近,我已经建立了一些股票。补充C FAQ的答案。如果有人想要评论和评论它们,我现在就把它们放在我的网页上,
http://benpfaff.org/writings/clc



来自该网站:
" ;使用strncpy()进入大缓冲区可能效率很低。 strncpy()总是写入
目标缓冲区中的每个字节,如果目标缓冲区比源字符串长得多,则会浪费大量时间。

这不是QoI问题吗?




如果你把一个零长度的字符串源strncpy到

a百字节长度的字符串目的地,

目标字符串的101个字节将被覆盖。


如果你将零长度字符串源strcpy到

a百字节长度的字符串目标,

目标字符串的1个字节将被覆盖。


-

pete


Over the last couple of years, and especially recently, I''ve
built up a few "stock" answers that supplement the C FAQ. If
anyone wants to review and comment on them, I''ve just now put
them up on my webpage, at
http://benpfaff.org/writings/clc

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

解决方案

"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...

Over the last couple of years, and especially recently, I''ve
built up a few "stock" answers that supplement the C FAQ. If
anyone wants to review and comment on them, I''ve just now put
them up on my webpage, at
http://benpfaff.org/writings/clc



From that website:
"Using strncpy() into a large buffer can be very inefficient. strncpy()
always
writes to every byte in the destination buffer, which can waste a lot of
time
if the destination buffer is much longer than the source string."

Isn''t this a QoI issue? According to the Standard,

"The strncpy function copies not more than n characters (characters that
follow a null character are not copied) from the array pointed to by s2 to
the array pointed to by s1. If copying takes place between objects that
overlap, the behavior is undefined."

Please note the part in parenteses.

Also from your website,

"If the source string is longer than the size of the destination buffer,
then strncpy() doesn''t write a terminating null. So a call to strncpy() must
be
followed by explicitly writing a null terminator at the end of the
destination
buffer in most cases."

Nothing simpler:

strncpy(dest, src, n)[n-1] = 0;

IMNSHO, strncpy() is far more advisable than strcpy().

Lastly, "off-topic guidance message" is a good title in a context of a
specific thread. As a general guidance, as e.g. on your website, it will
most likely go unnoticed ("I never write off-topic posts, why should I waste
my time reading it?").
Something like "comp.lang.c topicality guidance" might be better. Just my
£0.02.

Peter


Peter Pichler wrote:

"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...

Over the last couple of years, and especially recently, I''ve
built up a few "stock" answers that supplement the C FAQ. If
anyone wants to review and comment on them, I''ve just now put
them up on my webpage, at
http://benpfaff.org/writings/clc
From that website:
"Using strncpy() into a large buffer can be very
inefficient. strncpy() always writes to every byte
in the destination buffer, which can waste a lot of
time if the destination buffer is much longer than
the source string."

Isn''t this a QoI issue?



No.
According to the Standard,

"The strncpy function copies not more than n characters (characters that
follow a null character are not copied) from the array pointed to by s2 to
the array pointed to by s1. If copying takes place between objects that
overlap, the behavior is undefined."

Please note the part in parenteses.
Please note the very next sentence: "If the array pointed to by s2 is a
string that is shorter than n characters, null characters are appended to
the copy in the array pointed to by s1, until n characters in all have been
written."
Also from your website,

"If the source string is longer than the size of the destination buffer,
then strncpy() doesn''t write a terminating null. So a call to strncpy()
must be followed by explicitly writing a null terminator at the end of the
destination buffer in most cases."

Nothing simpler:

strncpy(dest, src, n)[n-1] = 0;
Easy to forget to do that, though, and it assumes that dest is at least n
bytes long, which it might not be.
IMNSHO, strncpy() is far more advisable than strcpy().
That''s like saying a screwdriver is more advisable than a hammer. It depends
on what you''re trying to do. strncpy''s job is to copy part of a string,
whereas strcpy''s job is to copy a whole string. It is silly to use strncpy
if your objective is to copy the whole string.
Lastly, "off-topic guidance message" is a good title in a context of a
specific thread. As a general guidance, as e.g. on your website, it will
most likely go unnoticed ("I never write off-topic posts, why should I
waste my time reading it?").
Something like "comp.lang.c topicality guidance" might be better. Just my
£0.02.



Better, and it suggested "Guidance on finding the best newsgroup for your
question" to me as a (rather verbose but otherwise perhaps more friendly)
possibility.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Peter Pichler wrote:


"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...

Over the last couple of years, and especially recently, I''ve
built up a few "stock" answers that supplement the C FAQ. If
anyone wants to review and comment on them, I''ve just now put
them up on my webpage, at
http://benpfaff.org/writings/clc



From that website:
"Using strncpy() into a large buffer can be
very inefficient. strncpy() always writes to every byte in the
destination buffer, which can waste a lot of time
if the destination buffer is much longer than the source string."

Isn''t this a QoI issue?



If you strncpy a zero length string source to
a hundred byte length string destination,
101 bytes of the destination string will be overwritten.

If you strcpy a zero length string source to
a hundred byte length string destination,
1 byte of the destination string will be overwritten.

--
pete


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

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