Search code examples
c++pointerscastingpool

Understanding pointer cast


I am reading this interesting article. http://www.codeproject.com/Articles/746630/O-Object-Pool-in-Cplusplus

I can't understand this line _firstDeleted = *((T **)_firstDeleted);

_firstDeleted already has type T*. Can anyone explain the purpose of that statement?


Solution

  • When an object is destroyed, its first sizeof(T*) bytes are overwritten with the address of the next free object.
    (That is, *T is actually no longer storing a T but a T*, if you see what I mean. The cast performs this reinterpretation. It is formally rather undefined.)

    This has the effect of the deleted objects forming a linked list of available memory blocks.

    Reusing the object memory for this list means that you don't need a separate list of free blocks.