宏和副作用

编程入门 行业动态 更新时间:2024-10-19 18:35:34
本文介绍了宏和副作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

C宏是否与侧面效果相结合是不安全的,即使它们是干净的,也是干净的。例如可以 foo_macro(++ c); 执行++ c多次,即使foo_macro是干净的?我总是认为干净是干净的。宏(即宏中所有参数都是定义中括号括起来的)没有这个问题... 我想知道因为我有在参考文献中查找了putc(),并且它表示如果通过的话,它是不安全的。值有副作用,我假设 标准宏是干净的......

解决方案 >>>>" C" == copx< co ** @ gazeta.plwrites: c如果C与宏结合使用是不安全的,那么即使它们是b "清洁"例如可以 cfoo_macro(++ c); #define foo_macro(x)(x)*(x) b $ b哎呀。 Charlton - Charlton Wilbur cw*****@chromatico

copx写道:

如果C宏与侧面效果相结合是不安全的,即使 它们是干净的例如可以 foo_macro(++ c); 执行++ c多次,即使foo_macro是干净的?我总是认为干净是干净的。宏(即宏中所有参数都是定义中括号括起来的)没有这个问题... 我想知道因为我有在参考文献中查找了putc(),并且它表示如果通过的话,它是不安全的。值有副作用,我假设 标准宏是干净的...

你的引用是正确的,并且 ;清洁"宏可以多次评估他们的 参数。举个简单的例子,如果你有 #define foo_macro(x)((x)*(x)) 那么foo_macro'的参数是完全括号的,但是foo_macro(++ c) 仍将扩展为((++ c)*(++ c))。 对于大多数标准库函数,如果它们也是作为一个宏实现的,那么它必须是宏,那个宏只计算每个参数 一次。 putc是一个例外。

copx说:

C宏是不是真的不安全

如果写得正确并且使用不正确,则不会。

与副作用结合使用即使它们是"清洁"例如可以 foo_macro(++ c); 执行++ c多次,即使foo_macro是干净的?

这不是一个广泛用于描述宏的术语。 (翻译:这是我第一次看到它时的。)

我有 总是 认为干净宏(即宏中所有参数都是定义中用圆括号括起来的)没有这个 问题...

你认为错了。

我想知道因为我刚刚在参考文献中查找了putc(), 和它说,如果通过它是不安全的。值有副作用,

你的引用说错了。 putc函数,如果实现为宏, 可以多次评估流,但*不是*字符。并且 你很难把它称为putc(''\ n'',fp ++),我相信吗?

和我假设标准宏是干净的...

为什么? - Richard Heathfield Usenet是一个奇怪的地方 - dmr 29/7/1999 www.cpax.uk 电子邮件:rjh在上述域名中, - www。

Is it true that C macros are unsafe when combined with side effects even if they are "clean" e.g. could foo_macro(++c); execute "++c" multiple times even if foo_macro is "clean"? I have always thought that "clean" macros (i.e. macros where all parameters are surrounded by parentheses in the definition) do not have this problem... I am wondering because I have just looked up putc() in a reference, and it says that it is unsafe if the "passed" value has a side effect, and I assume standard macros are "clean"...

解决方案

>>>>"c" == copx <co**@gazeta.plwrites: cIs it true that C macros are unsafe when combined with side ceffects even if they are "clean" e.g. could cfoo_macro(++c); #define foo_macro(x) (x)*(x) Whoops. Charlton -- Charlton Wilbur cw*****@chromatico

copx wrote:

Is it true that C macros are unsafe when combined with side effects even if they are "clean" e.g. could foo_macro(++c); execute "++c" multiple times even if foo_macro is "clean"? I have always thought that "clean" macros (i.e. macros where all parameters are surrounded by parentheses in the definition) do not have this problem... I am wondering because I have just looked up putc() in a reference, and it says that it is unsafe if the "passed" value has a side effect, and I assume standard macros are "clean"...

Your reference is correct, and "clean" macros can evaluate their arguments multiple times. As a simple example, if you have #define foo_macro(x) ((x) * (x)) then foo_macro''s arguments are fully parenthesised, but foo_macro(++c) will still expand to ((++c) * (++c)). For most standard library functions, it is required that if they are also implemented as a macro, that that macro evaluates each argument exactly once. putc is an exception.

copx said:

Is it true that C macros are unsafe

Not if written correctly and used correctly.

when combined with side effects even if they are "clean" e.g. could foo_macro(++c); execute "++c" multiple times even if foo_macro is "clean"?

That is not a widely-used term for describing macros. (Translation: it''s the first time I have ever seen it.)

I have always thought that "clean" macros (i.e. macros where all parameters are surrounded by parentheses in the definition) do not have this problem...

You thought wrong.

I am wondering because I have just looked up putc() in a reference, and it says that it is unsafe if the "passed" value has a side effect,

Your reference says wrong. The putc function, if implemented as a macro, may evaluate the stream more than once, but *not* the character. And you''re hardly likely to call it as putc(''\n'', fp++), I trust?

and I assume standard macros are "clean"...

Why? -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 www.cpax.uk email: rjh at the above domain, - www.

更多推荐

宏和副作用

本文发布于:2023-11-14 00:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!