Reputation: 5386
I'm writing code for a router (aka gateway), and as I'm receiving and sending packets I need to use a type of container that can support the logic of a router. When receiving a packet I want to place it in the end of the dynamic container (here from and on known as DC). When taking the packet out of the DC for processing I want to take it from the front of the DC.
Any suggestion on which one to use?
I've heard that a vector would be a good idea but I'm not quite sure if they are dynamic..
EDIT: The type of element that it should contain is a raw packet of type "unsigned char *". How would I write the code for the DC to contain such a type?
Upvotes: 0
Views: 405
Reputation: 28892
For a router, you probably should use a fixed size custom container (probably based around std::array or a C array). You can then introduce some logic to allow it to be used as a circular buffer. The fixed size is extremely important because you need to deal with the scenario where packets are coming in faster than you can send them off. When you reach your size limit, you then flow off.
With dynamically re-sizable containers, your may end up running out of memory or introducing unacceptable amounts of latency into the system.
Upvotes: 2
Reputation: 126787
std::deque<unsigned char *>
is the obvious choice here, since it supports efficient FIFO semantics (use push_back
and pop_front
, or push_front
and pop_back
, the performance should be the same).
In my experience the std::queue
(which is a container adapter normally built over std::deque
) is not worth the effort, it only restricts the interface without adding anything useful.
Upvotes: 3
Reputation: 51840
You can use std::queue
. You insert elements at the end using push()
and remove elements from the front using pop()
. front()
returns the front element.
To store unsigned char*
elements, you'd declare a queue like this:
std::queue<unsigned char*> packetQueue;
Upvotes: 0