的memcpy /的memmove [英] memcpy/memmove

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

问题描述

(如果这是常见问题解答或在K& R2中,我没有找到它)


哪些参数(如果有的话)可能是0或NULL? IOW,以下哪个

语句可以保证产生明确定义的行为?


char src [10];

char dst [10];


memcpy(dst,src,1);

memcpy(NULL,src,1);

memcpy(dst,src,0);

memcpy(NULL,src,0);

memcpy(dst,NULL,1);

memcpy(NULL,NULL,1);

memcpy(dst,NULL,0);

memcpy(NULL,NULL,0);


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

(if this is a FAQ or in K&R2, I didn''t find it)

What parameters (if any) may be 0 or NULL? IOW, which of the following
statements are guaranteed to produce well-defined behavior?

char src[10];
char dst[10];

memcpy( dst, src, 1 );
memcpy( NULL, src, 1 );
memcpy( dst, src, 0 );
memcpy( NULL, src, 0 );
memcpy( dst, NULL, 1 );
memcpy( NULL, NULL, 1 );
memcpy( dst, NULL, 0 );
memcpy( NULL, NULL, 0 );

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
(如果这是FAQ或K& R2,我没找到它。

什么参数(如果有的话)可能是0或NULL? IOW,以下哪些陈述可以保证产生明确的行为?

char src [10];
char dst [10];

memcpy(dst,src,1);
memcpy(NULL,src,1);
memcpy(dst,src,0);
memcpy(NULL,src,0);
memcpy(dst,NULL,1);
memcpy(NULL,NULL,1);
memcpy(dst,NULL,0);
memcpy(NULL,NULL,0 );
(if this is a FAQ or in K&R2, I didn''t find it)

What parameters (if any) may be 0 or NULL? IOW, which of the following
statements are guaranteed to produce well-defined behavior?

char src[10];
char dst[10];

memcpy( dst, src, 1 );
memcpy( NULL, src, 1 );
memcpy( dst, src, 0 );
memcpy( NULL, src, 0 );
memcpy( dst, NULL, 1 );
memcpy( NULL, NULL, 1 );
memcpy( dst, NULL, 0 );
memcpy( NULL, NULL, 0 );




根据我的研究(我建议你搜索新闻组),

任何参数都可以为NULL。但是,如果目的地或

来源为NULL或数量为空,则没有任何反应。

-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍/>



According to my research (I suggest you search the newsgroups),
any argument can be NULL. However, if the destination or
source are NULL or the quantity is null, nothing happens.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


" Christopher本森-马尼卡" <在*** @ nospam.cyberspace.org>写道:
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote:
什么参数(如果有的话)可能是0还是NULL? IOW,以下哪些陈述可以保证产生明确的行为?

char src [10];
char dst [10];


定义的行为:一个字节从src复制到dest。

memcpy(dst,src,0);


定义的行为:没有复制字符。

memcpy(NULL,src,1);
memcpy(NULL,src,0);
memcpy(dst,NULL,1);
memcpy(NULL,NULL,1);
memcpy(dst,NULL,0);
memcpy(NULL,NULL,0 );
What parameters (if any) may be 0 or NULL? IOW, which of the
following statements are guaranteed to produce well-defined
behavior?

char src[10];
char dst[10];

memcpy( dst, src, 1 );
Defined behaviour: One byte is copied from src to dest.
memcpy( dst, src, 0 );
Defined behaviour: no characters are copied.
memcpy( NULL, src, 1 );
memcpy( NULL, src, 0 );
memcpy( dst, NULL, 1 );
memcpy( NULL, NULL, 1 );
memcpy( dst, NULL, 0 );
memcpy( NULL, NULL, 0 );




所有六个都有未定义的行为:两个指针都必须有效

指针,即使要复制的字符数为零。


C99 7.21.1#2"声明为size_t n的参数指定

函数数组的长度,n可以具有值

调用该函数时为零。除非明确说明

否则在此

子条款中的特定函数的描述中,此类调用上的指针参数仍应具有

有效值,如7.1.4所述。在这样的调用中,找到一个字符的

函数没有发生,比较两个字符序列的

函数返回零,

和复制字符的函数复制零个字符。


-

Simon。



All six have undefined behaviour: both pointers must be valid
pointers even if the number of characters to copy is zero.

C99 7.21.1#2 "Where an argument declared as size_t n specifies
the length of the array for a function, n can have the value
zero on a call to that function. Unless explicitly stated
otherwise in the description of a particular function in this
subclause, pointer arguments on such a call shall still have
valid values, as described in 7.1.4. On such a call, a
function that locates a character finds no occurrence, a
function that compares two character sequences returns zero,
and a function that copies characters copies zero characters."

--
Simon.


Simon Biber< ne ** @ ralminnospam.cc>这样说:
Simon Biber <ne**@ralminnospam.cc> spoke thus:
所有六个都有未定义的行为:即使要复制的字符数为零,两个指针也必须是有效的指针。
C99 7.21.1#2(修剪)
All six have undefined behaviour: both pointers must be valid
pointers even if the number of characters to copy is zero. C99 7.21.1#2 (trimmed)




谢谢。现在,如果只有K& R2,我的另一本C书,或者我友好的男人页面那么多...... b $ b说了多少...


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Thank you. Now, if only K&R2, my other C book, or my friendly man page had
said as much...

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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