调用C和未定义/未指定的行为函数时顺序点 [英] Sequence points when calling functions in C and undefined/unspecified behaviour

查看:128
本文介绍了调用C和未定义/未指定的行为函数时顺序点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拖住我的序列点的理解用C - 只是想检查一番。在present,我相信,(1)是不确定的,而(2)仅仅是不确定的,在(2),有评估的参数为先按g ^ h (所以我们不会修改 I 序列点之间两次),但秩序的 F的参数评价仍然是不确定的。是我的理解是否正确?

I'm trying to pin down my understanding of sequence points in C -- just wanted to check something. At present, I believe that (1) is undefined whereas (2) is merely unspecified, on the basis that in (2), there are sequence points after evaluating the arguments for g and h (so we're not modifying i twice between sequence points), but the order of evaluation of the arguments of f is still unspecified. Is my understanding correct?

#include <stdio.h>

int g(int i) {
    return i;
}

int h(int i) {
    return i;
}

void f(int x, int y) {
    printf("%i", x + y);
}

int main() {
    int i = 23;
    f(++i, ++i); // (1)
    f(g(++i), h(++i)); // (2)
    return 0;
}

编辑:

看来关键点这里是编译器是否可以自由地执行这两个增量之前,无论是先按g ^ h 被称为 - 我从下面的答案的理解是,它是,但我倒是AP preciate确认是这样的话

It seems the key point here is whether the compiler is free to perform both the increments before either g or h is called -- my understanding from the answers below is that it is, although I'd appreciate confirmation that that's the case.

推荐答案

没有,每10 6.5.2.2存在的SUBEX pression参数评测之间没有顺序点,之前的实际调用。

No, per 6.5.2.2 10 there is no sequence point between the evaluation of subexpression arguments, just before the actual call.

看它的一种方法是,它是未指定的是否的这种行为是未定义;如果实现序列两个 ++我 SUBEX pressions任何调用先按g 或<$ C $前C> ^ h 则行为是不确定的,但如果 ++我 SUBEX pressions被尽可能晚地(立即评估之前调用 先按g 和 H分别设置),则行为是不确定的。然而,由于实施总是随意任何允许的不确定的行为之间作出选择,然后总的结果是不确定的。

One way of looking at it is that it is unspecified whether the behaviour is undefined; if the implementation sequences the two ++i subexpressions before any call to g or h then the behaviour is undefined, but if the ++i subexpressions are evaluated as late as possible (immediately before calling g and h respectively) then the behaviour is unspecified. However, because the implementation is always at liberty to choose between any allowed unspecified behaviour then the overall result is undefined.

这篇关于调用C和未定义/未指定的行为函数时顺序点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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