Search | | Contact Us | Resource Center Login | Home|

What is the output of the following program?

    group xyz     ,a
      x           ,d03
      y           ,d08
      z           ,d03
    group xyz2,x
      array       ,[^size(xyz)/^size(x)] d ^size(x)

    y = 2
    array[y] = y
    writes(1, %string(array[y]))

a. "2"
b. "0"
c. "200002"
d. a subscript error

In this program, we first assign a 2 to y, then use y to index array, storing the value of y in that element. Thus, we have assigned the second element of array the value of 2. Finally, we attempt to output the value of an element of array, indexed again by y. So it would seem reasonable that we should get "2" (a).

However, if we look closely at the data division, we will notice that array resides within a group that overlays the group that contains y. (That's what the ",x" on the name of the second group indicates.) That means that y occupies some portion of array. But what portion?

Each element of array is sized the same as the variable x, or 3 bytes.
The number of elements is determined by dividing the size of the group containing x by the size of x. Since that group has an overall size of
14 (3 + 8 + 3), dividing it by 3 yields 4 elements. Thus, array does not completely overlap x, y, and z.

Because the elements of array are sized the same as x, the second element of array will begin at the same location as y. If it were also the same size, the second assignment statement would have no effect and the answer would still be "2" (a). However, since the element size is 3 and the size of y is 8, the second element of array only overlaps the first three digits of y. Therefore, the second assignment changes the value of y from 2 to 200002 (c).

Of course, the second element of array still contains only a 2. In the final statement, though, we're using y again to index the array. Now that y contains 200002, which is far greater than the number of elements in array, we get a subscript error. Therefore, (d) is the correct answer.

As confusing as this example may seem, the ability to create complex data structures has been one of the historical strengths of Synergy Language. Some other languages provide similar features (such as unions in C, for example), but it's hard to find a better combination of flexibility and brevity that works well for business applications. With great power comes great responsibility, though, so try to make your own use of these features clearer than the above example.

More information about News and Events

Contact Synergex to learn more about  News and Events