Segmentation fault while trying to implement fixed size queue in C++

Bad*_*nda 2 c++ queue stl segmentation-fault

I wanted to implement a fixed size queue, I could have easily done with a struct having a queue as one of the data members and a member function that takes care of the push part of the queue, but rather wanted to try it out by inheriting queue like this.

template<typename T>
struct f_queue : public queue<T>{
    int n;
    T prev;

    f_queue(int n_):
        n(n_){}

    void push(T data){
        if(this->size() < this->n){
            this->push(data);
        }else{
            prev = this->front();
            this->pop();
            this->push(data);
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

This compiles just fine but for some reason it gives a segmentation fault and the gdb says

Program received signal SIGSEGV, Segmentation fault. 0x0000555555555862 in std::_Deque_iterator<int, int&, int*>::_S_buffer_size() ()

Not really sure what that is supposed to mean? When tried doing a backtrace the output comes out to be

#9913 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9914 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9915 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9916 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9917 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9918 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9919 0x0000555555554ec6 in f_queue<int>::push(int) ()
Run Code Online (Sandbox Code Playgroud)

doesn't stop goes infinitely. Need help with this.Thanks in advance.

Qui*_*mby 5

Your push function always recurses into itself:

void push(T data){
        if(this->size() < this->n){
            this->push(data);//HERE f_queue::push is called
        }else{
            prev = this->front();
            this->pop();
            this->push(data);//HERE f_queue::push is called
        }
    }
Run Code Online (Sandbox Code Playgroud)

You probably meant to call the base class's push:

void push(T data){
        if(this->size() < this->n){
            queue<T>::push(data);
        }else{
            prev = this->front();
            this->pop();
            queue<T>::push(data);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Note that STL containers are not meant to be publicly derived from. They do not have virtual destructors, so make sure you never delete through the base class pointer/reference.

EDIT

Non-virtual destructor means the following code is dangerous:

std::queue<T>* base = new f_queue<T>();
delete base;//Call std::queue<T>::~queue();
Run Code Online (Sandbox Code Playgroud)

There is no way how to force the call of the correct destructor. Making f_queue's destructor virtual will not help. But, as long as you never delete through a pointer to the base class, there is nothing wrong with deriving from any class.

One way to enforce that is to use private inheritance but I'm guessing you are using inheritance in the first place to retain most of the std::queue's API.