Resource-efficient programming (INF205) lab u38: 22nd September 2022

1. Terminology

The INF205 glossary document has been set up. Much of it is still under construction. We will periodically collect new suggested terms and definitions, still following the same principles as in no. 1 from lab u36.

  1. Please suggest one additional term, together with your own definition of that term.
  2. Is there anything you would like to add or comment on any of the existing headings from the INF205 glossary?

2. Memory leak

The "memory leak" code example does not only have a bug (memory leak), it is also a showcase of poor programming style in many ways.

  1. In your own view, what was done poorly? Is there anything about the programming style that makes bugs more likely? State your two main criticisms of the way in which the code was designed and/or written.
  2. Fix the code, or briefly describe how you would fix it.

Do not spend much time on the second part of this problem. If it takes too long, just give a brief description of what you were doing, and go on with problem no. 3.

3. Rearrange data using "struct"

Two spheres are here understood to collide if they overlap, which is the case if the distance between their centres is smaller than the sum of the radii of the two spheres. We are now looking at a program that reads diameters and coordinates of spheres; it then counts and outputs the number of collisions between pairs of spheres.

The sphere collision detection example code has a very low-level data structure, using multiple arrays: Data on the same sphere are spread over multiple arrays, and they have the same array index in all these arrays. In old-fashioned legacy codes from the times before object-oriented programming, you will often find the data arranged in such a way.

Make the code more logical: Use struct, defining a struct Sphere, to group all the data that belong to the same sphere into one Sphere object.

  1. Inspect the code (sphere-collisions-low-level.zip). How does it work, and what does it do?
  2. Design your new data structure, using a struct for spheres.
  3. Rewrite the code in line with your new, more logical and slightly higher-level data structure.