Test 1:
TargetArray[0]: 10
TargetArray[24]: 10
SentinelOne[0]: 0
SentinelTwo[0]: 0
SentinelOne[1]: 0
SentinelTwo[1]: 0
SentinelOne[2]: 0
SentinelTwo[2]: 0
Assigning...
Test 2:
TargetArray[0]: 20
TargetArray[24]: 20
TargetArray[25]: 20
SentinelOne[0]: 20
SentinelTwo[0]: 0
SentinelOne[1]: 0
SentinelTwo[1]: 0
SentinelOne[2]: 0
SentinelTwo[2]: 0
Lines 8 and 10 declare two arrays of three longintegers that act as sentinels
around TargetArray. These sentinel arrays are initialized with the value 0 on
lines 12–16. Because these are declared before and after TargetArray, there is a good
chance that they will be placed in memory just before and just after it. If memory is writ-
ten to beyond the end of TargetArray, it is the sentinels that are likely to be changed
rather than some unknown area of data. Some compilers count down in memory; others
count up. For this reason, the sentinels are placed both before and after TargetArray.
Lines 20–30 confirm the sentinel values are okay by printing them as well as the first
and last elements of TargetArray. On line 34,TargetArray’s members are all reas-
signed from the initial value of 10 to the new value of 20. Line 34, however, counts to
TargetArrayoffset 25, which doesn’t exist in TargetArray.
Lines 37–39 print TargetArray’s values again as a second test to see what the values are.
Note that on line 39 TargetArray[25]is perfectly happy to print the value 20. However,
when SentinelOneand SentinelTwoare printed,SentinelOne[0]reveals that its value
has changed. This is because the memory that is 25 elements after TargetArray[0]is the
same memory that is at SentinelOne[0]. When the nonexistent TargetArray[25]was
accessed, what was actually accessed was SentinelOne[0].
OUTPUT
412 Day 13
ANALYSIS