WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Guru · question 94 of 100

Explain the concept of intrusive containers in C++ and their advantages and disadvantages compared to non-intrusive containers.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

Intrusive containers are a type of container in C++ where the container nodes or elements are directly embedded within the data structure they are intended to represent. This is in contrast to non-intrusive containers where the nodes are maintained as separate objects and the data they contain is stored elsewhere.

The primary advantage of intrusive containers is that they reduce the overhead associated with allocating and deallocating memory for nodes, as well as with copying and moving the contained objects. This can result in significant performance improvements in cases where many small objects need to be stored and accessed frequently.

Intrusive containers can also improve memory locality, as the nodes and data are stored in the same memory block, which can help reduce cache misses and improve performance.

On the other hand, intrusive containers can be more difficult to implement and use than non-intrusive containers, as they require modification of the contained objects themselves to include the necessary node information. This can make the code more complex and harder to maintain.

Here is an example of an intrusive singly linked list implementation in C++:

    template<typename T>
    class IntrusiveList
    {
        public:
        struct Node
        {
            Node* next = nullptr;
            T* value = nullptr;
        };
        
        void push_front(T& value)
        {
            Node* node = new Node{head_, &value};
            head_ = node;
        }
        
        void pop_front()
        {
            if (head_)
            {
                Node* node = head_;
                head_ = node->next;
                delete node;
            }
        }
        
        class Iterator
        {
            public:
            Iterator(Node* node) : node_(node) {}
            
            T& operator*() { return *node_->value; }
            Iterator& operator++() { node_ = node_->next; return *this; }
            bool operator!=(const Iterator& other) { return node_ != other.node_; }
            
            private:
            Node* node_;
        };
        
        Iterator begin() { return Iterator{head_}; }
        Iterator end() { return Iterator{nullptr}; }
        
        private:
        Node* head_ = nullptr;
    };

In this implementation, the Node struct is embedded within the contained objects themselves, allowing the list to be intrusive. This reduces the overhead of allocating and deallocating memory for nodes, and avoids the need to copy or move objects when adding them to the list. However, it also requires that the contained objects be modified to include the necessary Node information.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics