memcpy作为宏观合法吗? [英] memcpy as macro legit?
问题描述
我刚刚注意到,执行以下操作可能会失败,因为
它可以在评估之前覆盖u->尺寸。
memcpy(u,buf,u-> size);
这是一个宏是否合法,而不是一个函数,这将是一个函数,它将是b / b
不是clobber参数?
只是让我感到惊讶。
Mike
Michael B Allen< mb ***** @ ioplex.com>写道:我只是注意到做类似下面的事情可能会失败,因为它可以在评估之前覆盖u->尺寸。
你怎么知道u->尺寸在评估前会被覆盖?
memcpy(u,buf,u-> size);
我猜'你'和''buf''是结构的指针 - 但为什么你会用
然后用u->尺寸代替sizeof * u的大小或sizeof * buf?通常
我会假设没有必要在结构中存储结构的大小
。或者你是否在结构中使用了一些肮脏的技巧,例如零 -
长度数组?
这是一个宏是否合法,而不是一个函数
不破坏参数?
据我所知,在标准的memcpy()中应该是一个
函数。你确定'u'和''buf''指向永远没有
重叠吗?
问候,Jens
-
\ Jens Thoms Toerring ___ Je ****** *****@physik.fu-berlin.de
\ __________________________ http://www.toerring.de
2004年11月19日星期五04:27:17 -0500,Michael B Allen写道:
我刚刚注意到,执行以下操作可能会失败,因为它可以在评估之前覆盖u->尺寸。
memcpy(u,buf,u-> size);
这是一个宏是否合法,而不是一个不会破坏参数的函数?
任何标准库函数都可以在相关的
标准头文件中有一个宏定义。然而,宏必须仍然保留
函数的语义,并且作为一个函数,它不能修改它的参数。
劳伦斯
" Michael B Allen" < MB ***** @ ioplex.com>在消息中写道
news:pa ******************************** @ ioplex.com ...我刚刚注意到,执行以下操作可能会失败,因为它可以在评估之前覆盖u->尺寸。
memcpy(u,buf,u-> size);
这是一个宏是否合法,而不是一个不会破坏参数的函数?
只是让我感到惊讶。
这种行为确实让我大吃一惊。
怎么可能
评估u->大小以至少检查它是非零之前,在你内覆盖一个字节?
鉴于最后一个参数可能是一个序数常量
或一个昂贵的函数调用,甚至不需要一个宏需要
来临时存储大小值它?
你所描述的行为绝对不合法。
据我了解,C99标准要求
memcpy表现得像一个函数(不像宏) -
即使平台显然被允许对待它作为一个
内在函数,并实现特定的优化。
hth,Ivan
-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格
I just noticed that doing something like the following may fail because
it can overwrite u->size before it''s evaluated.
memcpy(u, buf, u->size);
Is it legit that this is a macro as opposed to a function that would
not clobber the parameter?
Just surprised me a little is all.
Mike
Michael B Allen <mb*****@ioplex.com> wrote:I just noticed that doing something like the following may fail because
it can overwrite u->size before it''s evaluated.
How do you know that u->size gets overwritten before evaluation?
memcpy(u, buf, u->size);
I guess ''u'' and ''buf'' are pointers to structures - but why would you
then use u->size instead of "sizeof *u" or "sizeof *buf"? Usually
I would assume that there''s no need to store the size of a structure
within the structure. Or are you using some dirty tricks like zero-
length arrays in the structure?
Is it legit that this is a macro as opposed to a function that would
not clobber the parameter?
As far as I can see in the standard memcpy() is supposed to be a
function. Are you sure that what ''u'' and ''buf'' point to never
overlaps?
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
On Fri, 19 Nov 2004 04:27:17 -0500, Michael B Allen wrote:
I just noticed that doing something like the following may fail because
it can overwrite u->size before it''s evaluated.
memcpy(u, buf, u->size);
Is it legit that this is a macro as opposed to a function that would
not clobber the parameter?
Any standard library function can have a macro definition in the relevant
standard header. However the macro must still preserve the semantic of the
function, and as a function it cannot modify its arguments.
Lawrence
"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa********************************@ioplex.com ...I just noticed that doing something like the following may fail because
it can overwrite u->size before it''s evaluated.
memcpy(u, buf, u->size);
Is it legit that this is a macro as opposed to a function that would
not clobber the parameter?
Just surprised me a little is all.
This behavior would surprise me a lot indeed.
How could a single byte be overwritten within ''u'' before
having evaluated u->size to at least check that it is non-zero?
Given that the last parameter could be an ordinal constant
or an expensive function call, wouldn''t even a macro need
to store the size value in a temporary before using it?
The behavior you are describing definitely isn''t legit.
And as far as I understand, the C99 standard requires
memcpy to behave like a function (not like a macro) --
even though platforms obviously are allowed to treat it as an
intrinsic function, and to implement specific optimizations.
hth, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
这篇关于memcpy作为宏观合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!