Category: 

What Is a Sequence Point?

Article Details
  • Written By: Eugene P.
  • Edited By: Angela B.
  • Last Modified Date: 05 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The U.S. Coast Guard led the evacuation of more than 500,000 people from Lower Manhattan on 11 September 2001.  more...

September 27 ,  1940 :  The World War II Axis powers formed with the signing of the Tripartite Pact.  more...

A sequence point in computer programming is a moment that occurs during program execution when the value of a variable has been completely calculated, with no changes pending from previous operations and no computations in a future expression being performed yet. There are a number of places where sequence points exist — primarily defined in the C language standard — such as before the execution of the code inside a function, at the end of control expressions in statements such as "for" and "if", and at the end of any complete expression, such as a simple line of C code. Some reasons for defining a sequence point are to avoid situations that are ambiguous, result in undefined behavior or could confuse the compiler and generate code that is unpredictable. In many cases, programmers do not explicitly worry about a sequence point, although, in the creation of a compiler, the concept is very important to ensuring code is executed correctly.

An example of a sequence point in the C programming language is in the statement A = A + B;. In this expression, the semicolon is the sequence point; when the expression is completed, the value of A will be evaluated and no residual calculations will be performed on it until the next expression begins. The equal sign is not a sequence point, because the value of A might be modified by the compiler in any order throughout the expression.

Ad

The main rule of a sequence point is that no variable will be accessed more than once between points for any purpose other than to calculate a change in its value. A violation of this rule is best expressed when assigning a value to an array. If there is a variable A and an array called I, then grammatically in C it is possible to write the expression I[ A ] = A++. Here, the variable is accessed more than once for a purpose other than evaluating its current value; namely, it is used as an index into the array I. This means the compiler might increment A before it is used as an index or after it is used, creating unpredictable behavior that cannot be relied on in the program.

A sequence point basically can be seen as a way to ensure that statements can and will be consistently evaluated and executed by a compiler. This also allows a compiler to employ optimization strategies, because the defined behavior is predictable. Within the C language standard, there are three main instances of sequence points, namely when a function begins execution, at the point of logical operators and commas, and at the end of a complete expression that ends with a semicolon, as most C statements do.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email