Fundamental Concepts / Sequence Points
Subtitles of the Movie
Expressions have both direct effects and side effects. Take the simple statement that copies the value computed in the expression B into the location specified by A. Assigning a new value is a direct effect of the expression. For example, this expression copies a value of A sub I into X. No side effects. But look at this. This expression also copies the value of A sub I into X but it has a side effect. After it performs the copy, it adds one to I. This works out fine because of the sequence point. When this statement completes execution, both variables X and I have had their values changed. And changed in such a way that we can predict exactly what the new values will be. But that's not always the case. The results of this statement can not be predicted. It is possible that A sub I and B sub I are both calculated before the value of I is ever changed. Or it could be that the value of I is changed between the two operations. The precedence of operators is defined for C but the order in which the operations occur is not. The compiler is free to do it whichever way is best so this statement is ambiguous because of the side effects. That's where the sequence points come in. When the logic flow of your program comes to a sequence point, all operations are completed before it goes on. Any two operations performed on opposite sides of a sequence point are safe from the side effects of each other. The sequence points are semicolons or commas which in statements the operators logical or and logical and as well as the operators question mark and colon used in conditional expressions and finally any expressions used an as argument in a function call is finalized before the call is made. All of these are sequence points but you have to be careful. Because function calls are synchronization points, I is incremented immediately after the call to the function F and again immediately after the call to G. But the problem is because a plus sign is not a synchronization point, the C compiler is free to call the functions in whatever order suits it. So you can't predict what the value of the argument will be for either of the function calls. There is a rule of thumb you can follow and not have a problem. If any value is being modified inside this statement, don't use that same variable for anything else in that same statement. But you have to know what that means. For example, this statement is OK. And so is this one. The modification happens as the very last thing on the receiving end of the assignment operation. But this statement is not. That's because the location of the assignment is determined with a value of I and the value of I unknown at the time of the assignment. The increment of I may not happen until the synchronization point of the semicolon is reached. Simply put, the value of I is modified as a side effect but it's used more than once in the statement. This is another place that you have to be careful. Under normal circumstances this would be OK but if I happens to be a global variable and the function refers to its global location, it could get the wrong value. The function call is a sequence point but there is no promise that the value of I will be incremented before or after the function actually executes. Using a global this way is bad practice anyway but things like this can sneak up on you, especially when you are maintaining old code. Sometimes you want to call a function only for its side effects. You can do that and ignore the return value. This is a function call that passes no arguments and the return value, if one exists, is discarded. It has no effect on the running program except any possible side effects that it may produce. You can also have a null statement that is nothing other than a semicolon. Look at this. This loop is for the purpose of moving the pointer P forward until the letter J is found. There is nothing to be done in the body of the loop so it consists of a null statement; a semicolon. Here's another use of a null statement. It can be the holder of a label. This way a statement with a label on it can be moved, inserted or even deleted without affecting the rest of the program. The label can be the target of a jump statement. I'll have more about that later.
Tutorial Information
| Course: | Advanced C Programming |
| Author: | Arthur Griffith |
| SKU: | 33965 |
| ISBN: | 1-935320-24-6 |
| Release Date: | 2009-01-30 |
| Duration: | 5.5 hrs / 82 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 