Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Sequence point

From Wikipedia, the free encyclopedia
Concept in computer programming

InC andC++, asequence point defines any point in acomputer program'sexecution at which it is guaranteed that allside effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are a core concept for determining the validity of and, if valid, the possible results of expressions. Adding more sequence points is sometimes necessary to make an expression defined and to ensure a single valid order of evaluation.

Documentation forC11 andC++11 stopped using the term "sequence point" and now uses alternative terms:[1][2][3]

  1. An expression's evaluation can be "sequenced before" the evaluation of another expression. (Equivalently, the other expression's evaluation can be "sequenced after" that of the first.)
  2. The expressions' evaluation is "indeterminately sequenced", meaning that one is "sequenced before" the other, but which is unspecified.
  3. The expressions' evaluation is "unsequenced", meaning the operations in each expression may be interleaved.

The execution of unsequenced evaluations can overlap, leading to potentially catastrophicundefined behavior if they sharestate. This situation can arise inparallel computing, causingrace conditions, but undefined behavior can also result in single-threaded situations. For example,a[i] = i++; (wherea is an array andi is an integer) has undefined behavior.

Examples of ambiguity

[edit]

Consider twofunctionsf() andg(). In C and C++, the+ operator is not associated with a sequence point, and therefore in theexpressionf()+g() it is possible that eitherf() org() will be executed first. Thecomma operator introduces a sequence point, and therefore in the codef(),g() the order of evaluation is defined: firstf() is called, and theng() is called.

Sequence points also come into play when the same variable is modified more than once within a single expression. An often-cited example is theC expressioni=i++, which apparently both assignsi its previous value and incrementsi. The final value ofi is ambiguous, because, depending on the order of expression evaluation, the increment may occur before, after, or interleaved with the assignment. The definition of a particular language might specify one of the possible behaviors or simply say the behavior isundefined. In C and C++, evaluating such an expression yields undefined behavior.[4] Other languages, such asC#, define theprecedence of theassignment operator and theincrement operator in such a way that the result of the expressioni=i++ is guaranteed.

Behavior

[edit]

Up to C++03

[edit]

In C[5] and C++,[6] sequence points occur in the following places. (In C++,overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.)

  1. Between evaluation of the left and right operands of the&& (logical AND),|| (logical OR) (as part ofshort-circuit evaluation), andcomma operators. For example, in the expression*p++!=0&&*q++!=0, all side effects of the sub-expression*p++!=0 are completed before any attempt to accessq.
  2. Between the evaluation of the first operand of theternary conditional operator and its second or third operand. For example, in the expressiona=(*p++)?(*p++):0 there is a sequence point after the first*p++, meaning it has already been incremented by the time the second instance is executed.
  3. At the end of a full expression. This category includes expression statements (such as the assignmenta=b;),return statements, the controlling expressions ofif,switch,while, ordo-while statements, and each of the three expressions in afor statement.
  4. Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered. In the expressionf(i++)+g(j++)+h(k++),f is called with a parameter of the original value ofi, buti is incremented before entering the body off. Similarly,j andk are updated before enteringg andh respectively. However, it is not specified in which orderf(),g(),h() are executed, nor in which orderi,j,k are incremented. If the body off accesses the variablesj andk, it might find both, neither, or just one of them to have been incremented. (The function callf(a,b,c) isnot a use of the comma operator; the order of evaluation fora,b, andc is unspecified.)
  5. At a function return, after the return value is copied into the calling context. (This sequence point is only specified in the C++ standard; it is present only implicitly in C.[7])
  6. At the end of aninitializer; for example, after the evaluation of5 in the declarationinta=5;.
  7. Between each declarator in each declarator sequence; for example, between the two evaluations ofa++ inintx=a++,y=a++.[8] (This isnot an example of the comma operator.)
  8. After each conversion associated with an input/output format specifier. For example, in the expressionprintf("foo %n %d",&a,42), there is a sequence point after the%n is evaluated and before printing42.

C11 and C++11

[edit]
[icon]
This sectionneeds expansion. You can help byadding missing information.(April 2023)

C++11 brought diverse changes, including features that improve the language's performance, usability, and multithreading.[9] Partially because of the introduction of language support for threads, C11 and C++11 introduced new terminology for evaluation order. An operation may be “sequenced before” another, the two can be “indeterminately sequenced” (one must complete before the other), or the two can be “unsequenced” (the operations in each expression may be interleaved).

C++17

[edit]

C++17 restricted several aspects of evaluation order. Thenew expression will always perform the memory allocation before evaluating the constructor arguments. The operators<<,>>,.,.*,->*, and the subscript and function call operator are guaranteed to be evaluated left to right (whether they are overloaded or not). For example, the code

std::cout<<a()<<b()<<c();// parsed as (((std::cout << a()) << b()) << c());

is newly guaranteed to calla,b, andc in that order. The right-hand side of any assignment-like operator is evaluated before the left-hand side, so thatb() *= a(); is guaranteed to evaluatea first. Finally, although the order in which function parameters are evaluated remains implementation-defined, the compiler is no longer allowed to interleavesub-expressions across multiple parameters.[10]

See also

[edit]

References

[edit]
  1. ^"ISO/IEC 14882:2024".
  2. ^"A finer-grained alternative to sequence points (revised) (WG21/N2239 J16/07-0099)". Retrieved2012-07-05.
  3. ^"Order of evaluation". Retrieved2015-10-14.
  4. ^Clause 6.5#2 of theC99 specification: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."
  5. ^Annex C of theC99 specification lists the circumstances under which a sequence point may be assumed.
  6. ^The 1998 C++ standard lists sequence points for that language in section 1.9, paragraphs 16–18.
  7. ^C++ standard, ISO 14882:2003, section 1.9, footnote 11.
  8. ^C++ standard, ISO 14882:2003, section 8.3: "Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself."
  9. ^Abdulwahab, Saddam; Rashwan, Hatem; Cristiano, Julian; Chambon, Sylvie; Puig, Domenec (2019). "Effective 2D/3D Registration using Curvilinear Saliency Features and Multi-Class SVM".Proceedings of the 14th International Joint Conference on Computer Vision, Imaging and Computer Graphics Theory and Applications. SCITEPRESS - Science and Technology Publications. pp. 354–361.doi:10.5220/0007362603540361.ISBN 978-989-758-354-4.
  10. ^Dos Reis, Gabriel; Sutter, Herb; Caves, Jonathan (2016-06-23)."Refining Expression Evaluation Order for Idiomatic C++"(PDF).open-std.org. pp. 1–5. Retrieved28 April 2023.

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Sequence_point&oldid=1330250404"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp