Search code examples
c++overflowdeque

c++ Handling queue overflow


I am creating a thread which puts data into the message queue which is defined as follows:

std::deque<struct MessageDetails> MsgQueue;

struct MessageDetails{
    char msg[256];
    uint64_t Signature;
    int32_t Id;
    int32_t Mask;
    bool Valid;
};

Ideally the other module should pop the data from this queue and process it. But for error handling in my code , In case the other module is not processing any data (or not processing too quickly) then the message queue size keeps on increasing and may be at some point of time lead to overflow.

I wanted know when will it overflow(what is the allocated size?) and how can I handle that? Is there any error that I can capture?


Solution

  • You can try adding a try catch block while inserting into dequeu:

    try {
        MsgQueue.insert(xxx)
    } catch (std::bad_alloc & e) {
        //Add logic to handle queue overflow
    }
    

    also you can do compare

    MsQueue.max_size() and MsQueue.size() 
    

    and if there is no enough capacity try

    MsQueue.resize