1

Memory deallocation is more problematic, because at deallocation time there's a piece of information missing—all we have is a pointer to deallocate, and we don't know to what Chunk that pointer belongs. We can walk through chunks_ and check whether the given pointer falls in between pData_ and pData_ + blockSize_ * numBlocks_.

from Modern C++ design 2001, Page 76

If my understanding is right, the algorithm described for checking whether a given pointer points at an element in a given array involves comparing of two pointers from different arrays. If the comparison operator is >= or <= (highly possible?), then this will cause undefined behaviour.

But the author didn't explicitly specify this procedure or the operator, which will leave this problem to readers. (Or the author has made a mistake? He can at least give a hint.)

One solution is to use !=, which will make deallocation 100 times slower. Or may be, under certain circumstance, certain undefined behaviour can be tolerated. Then under what circumstance, the undefined behaviour mentioned above can be happily tolerated?

felix
  • 2,213
  • 7
  • 16
  • This question has been edited. It's no longer a duplicate. Please reopen it. – felix Nov 20 '16 at 19:37
  • 1
    The unspecified (not undefined) behavior was included for oddball architectures, such as segmented pointers on the x86 in 16-bit mode. You are unlikely to run into trouble in practice. – Mark Ransom Nov 21 '16 at 15:52

0 Answers0