Intrusive lists

后端 未结 5 945
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 23:53

I\'ve not been able to find too much information about them online. What are they and when are they typically used?

Thanks.

5条回答
  •  再見小時候
    2020-12-13 00:25

    Here is a brief description that is valid for lists as well:

    I. Intrusive containers.

    Object to be stored contains additional information to allow integration in container. Example:

    struct Node
    {
        Node* next;   // additional
        Node* prev;   // information 
        T data;
    } 
    

    1. Pros:

    • stores the objects themselves.

    • doesn't involve memory management.

    • iteration is faster.
    • better exception guarantees.
    • predictability in insertion and deletion of objects. (no additional (non-predictable) memory management is required.)
    • better memory locality.

    2. Cons:

    • contains additional data for container integration. (every store type must be adapted (modified) to the container requirements.)
    • caution with possible side effects when changing the contents of the stored object.(especially for associative containers.)
    • lifetime management of the inserted object, independently from the container.
    • object can be possibly disposed before erased from the container leading to iterator invalidation.
    • intrusive containers are NON-copyable and NON-assignable.

    II. Non-instrusive containers (C++ standard containers)

    Object doesn't "know" and contain details about the container in which is to be stored. Example:

    struct Node
    {
        T data;
    }

    1. Pros:

    • does not containe additional information regarding the container integration.
    • object's lifetime managed by the container. (less complex.)

    2. Cons:

    • store copies of values passed by the user. (inplace construction possible.)
    • an object can belong only to one container. (or the contaier should store pointers to objects.)
    • overhead on storing copies. (bookkeeping on each allocation.)
    • non-copyable or non-movable objects CAN'T be stored in non-intrusive containers.
    • can't store derived object and still maintain its original type. (slicing - looses polymorphism.)

提交回复
热议问题