Sequence point

Sequence point

A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.

Examples of ambiguity

Consider two functions f() and g(). In C and C++, the + operator is not a sequence point, and therefore in the expression f()+g() it is possible that either f() or g() will be executed first. The comma operator is a sequence point, and therefore in the code f(),g() the order of evaluation is defined (i.e., first f() is called, and then g() is called). The type and value of the whole expression are those of g(); the value of f() is discarded.

Sequence points also come into play when the same variable is modified more than once. An often-cited example is the expression i=i++, which both assigns i to itself and increments i; what is the final value of i? Language definitions might specify one of the possible behaviors or simply say the behavior is undefined. In C and C++, evaluating such an expression yields undefined behavior.

equence points in C and C++

In C [Annex C of the C99 specification lists the circumstances under which a sequence point may be assumed.] and C++ [The 1998 C++ standard lists sequence points for that language in section 1.9, paragraphs 16–18.] , 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.)

#Between evaluation of the left and right operands of the && (logical AND), || (logical OR), and comma 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 access q.
#Between the evaluation of the first operand of the ternary and the second or third operand. For example, in the expression a = (*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.
#At the end of a full expression. This category includes expression statements (such as the assignment a=b;), return statements, the controlling expressions of if, switch, while, or do-while statements, and all three expressions in a for statement.
#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 expression f(i++) + g(j++) + h(k++), f is called with a parameter of the original value of i, but i is incremented before entering the body of f. Similarly, j and k are updated before entering g and h respectively. However, it is not specified in which order f(), g(), h() are executed, nor in which order i, j, k are incremented. The values of j and k in the body of f are therefore undefined. [Clause 6.5#2 of the C99 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." Accessing the value of j inside f therefore invokes undefined behavior.] Note that a function call f(a,b,c) is not a use of the comma operator and the order of evaluation for a, b, and c is unspecified.
#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 [C++ standard, ISO 14882:2003, section 1.9, footnote 11.] .)
#At the end of an initializer; for example, after the evaluation of 5 in the declaration int a = 5;.

References

* [http://c-faq.com/expr/seqpoints.html Question 3.8] of the FAQ for comp.lang.c


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • point — I. Point. s. m. Picqueure qui se fait dans de l estoffe avec une aiguille enfilée de soye, de laine, de fil, &c. Il n y a qu un point ou deux à faire pour recoudre cela. faire un point d aiguille. En parlant d ouvrages de tapisserie à l aiguille …   Dictionnaire de l'Académie française

  • séquence — [ sekɑ̃s ] n. f. • 1170; bas lat. sequentia 1 ♦ Liturg. Chant rythmé qui prolonge le verset de l alléluia (à la messe) ou le trait (II, 5o). 2 ♦ (1534) À certains jeux, Série d au moins trois cartes de même couleur qui se suivent (⇒ 1. quarte,… …   Encyclopédie Universelle

  • Séquence du Gun Barrel de James Bond — La séquence du Gun Barrel est le dispositif de signature qui ouvre la plupart des films James Bond. Il représente une cible dans laquelle 007 marche de profil jusqu au centre de celle ci avant de se tourner vers l assassin, tirer et voir le sang… …   Wikipédia en Français

  • Point accepted mutation — (PAM), is a set of matrices used to score sequence alignments. The PAM matrices were introduced by Margaret Dayhoff in 1978 based on 1572 observed mutations in 71 families of closely related proteins.[1] Each matrix has the twenty standard amino… …   Wikipedia

  • Sequence stratigraphy — is a relatively new branch of geology that attempts to link relative sea level changes to sedimentary deposits. The essence of the method is mapping of strata based on identification of surfaces which represent time lines (e.g. subaerial… …   Wikipedia

  • Point-to-point construction — is the way most electronics circuits were constructed before the 1950s. Point to point construction is still used to construct prototype equipment with few or heavy electronic components.The crucial invention was soldering. In soldering, an alloy …   Wikipedia

  • Point-to-Point Protocol — Saltar a navegación, búsqueda Point to Point Protocol (PPP) Familia: Protocolos de enlace punto a punto Función: Transmisión de datagramas IP no estándar en líneas serie. Ubicación en la pila de protocolos …   Wikipedia Español

  • Séquence principale — Le diagramme de Hertzsprung Russell figure les étoiles. En abscisse, l indice de couleur (B V) ; en ordonnée, la magnitude absolue. La séquence principale se voit comme une bande diagonale marquée allant du haut à gauche au bas à droite. Ce… …   Wikipédia en Français

  • Sequence alignment — In bioinformatics, a sequence alignment is a way of arranging the sequences of DNA, RNA, or protein to identify regions of similarity that may be a consequence of functional, structural, or evolutionary relationships between the sequences.[1]… …   Wikipedia

  • Séquence d'ouverture des films de James Bond — Dans cette scène traditionnelle, James Bond marche dans le centre du canon sur un fond vide, puis tire sur le spectateur une fois l ensemble arrivé au centre de l écran. La séquence d ouverture des films de James Bond, aussi connue sous l… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”