English 中文(简体)
C++中的这种不明确的行为吗?
原标题:Is this undefined behaviour in C++?

我很想知道,如果下文所述,接触X的机会是没有界定的行为,还是:

int f(int *x)
{
    *x = 1;
    return 1;
}

int x = 0;
if (f(&x) && x == 1) {
    // something
}
最佳回答

It s not undefined behavior as operator && is a sequence point

问题回答

它的定义是明确的。

Reference - C++03 Standard:

第5节:表述,第4段:

除已指出的[例如关于和(或)“特别规则”和“propp”/strong”外,对单个运营商和个别表述的次量进行评价的次序以及产生副作用的命令是未具体说明

While in,

Section 1.9.18

在评估以下表述时:

a && b
a || b
a ? b : c
a , b

using the built-in meaning of the operators in these expressions, there is a sequence point after the evaluation of the first expression (12).

该定义了。 C/C++确实进行了zy评价,并且规定,先计算和检查左侧的表述。 如果情况确实如此,那是正确的。

无,因为<代码>&&界定了在车前必须计算灯的订单。

There is a defined order also on ||, ?: and ,. There is not on other operands.

In the comparable:

int x = 0;
if (f(&x) & x == 1) {
    // something
}

然后是没有定义的。 在此,将按顺序计算电灯和电灯。 这种不附带条件的形式是合乎逻辑的,不太常见,因为短期削减通常至少有利于业绩,而且往往对纠正至关重要。

它不是没有定义的行为。 理由取决于两个事实,即: <>m> 和> 均足以确定行为

  • A function call and termination is a sequence point
  • The && operator is a sequence point

以下行为的定义太大:

int f(int *x) {
    *x = 1;
    return 1;
}

int x = 0;
if (f(&x) & (x == 1)) {
    // something
}

但是,你不知道<代码>x = 1? 但是,这对于界定这一法典的行为来说并不重要。

它不是没有定义的,而是应当汇编,因为你重新试图指定一个点到X(&x)。

<代码>&&将从左边评价到右边(如果左边评价假,评价将停止)。

Edit:随着这一改动,它应当汇编,但仍需加以界定(因为如果你使用点名或参考书,这确实很重要)。

It will pass the address of the local variable x in the caller block as a parameter to f (pointer to int). f will then set the parameter (which is a temporary variable on the stack) to address 1 (this causes no problem) and return 1. Since 1 is true, the if () will move on to evaluate x == 1 which is false, because x in the main block is still 0.

如果没有执行,那部分人将无法执行。

http://www.ohchr.org。

有了你的新版本,尸体将被处决,因为在f(a)返回后,X在警棍中为1。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?