Answers/Solutions to Exercises in Chapter 6, Exercise 8

E8: We want to have a dynamic array of objects. We expect frequent repetitions. So we will keep a dynamic array with just one copy of each object and a dynamic array of pointers to the objects in the first array. See the diagram below.

pic1.jpg (37727 bytes)

Because both arrays are supposed to be dynamic, when needed, we extend them using realloc(). But sometimes it does not work. Why?

A8: When B is extended, all is fine. When B is reallocated, all is fine as well, as all the links to A are copied to the reallocated segment. When A is extended, all is fine as well, However, when A is reallocated, the links in B do not get updated accordingly, and hence they are nonsensical or dangling.

Back to Answers/Solutions Index                          Back to Answers/Solutions for Chapter 6 Index