STL buffer

M

mike7411

I'm trying to create a program that will buffer data received in UDP
packets.

I'd like to use an STL object that will allow me to add on new data
like this:

buffer+=newdata;

I don't think I can use a string because I don't think it allows 0
characters.

Any ideas on what to use?
 
S

Salvatore Iovene

I'm trying to create a program that will buffer data received in UDP
packets.

I'd like to use an STL object that will allow me to add on new data
like this:

buffer+=newdata;

I don't think I can use a string because I don't think it allows 0
characters.

Any ideas on what to use?

You could subclass Vector or a List, and then overload the += operator
for that purpose.
Just an idea.
 
R

red floyd

I'm trying to create a program that will buffer data received in UDP
packets.

I'd like to use an STL object that will allow me to add on new data
like this:

buffer+=newdata;

I don't think I can use a string because I don't think it allows 0
characters.

Any ideas on what to use?

1. Yes, std::string allows 0 characters.

2. You should probably use a std::vector<unsigned char>, though, since
you may not receive string data.

3. std::copy(newdata.begin(),
newdata.end(),
std::back_inserter(buffer));
 
D

David Harmon

On 18 Oct 2006 11:44:09 -0700 in comp.lang.c++, (e-mail address removed)
wrote,
I don't think I can use a string because I don't think it allows 0
characters.

std::string can contain '\0' characters, no problem.
 
N

Nick Keighley

Salvatore said:
You could subclass Vector or a List, and then overload the += operator
for that purpose.
Just an idea.

subclass == derive from?

can you derive from the std containers?
 
T

Thomas Matthews

Salvatore said:
You could subclass Vector or a List, and then overload the += operator
for that purpose.
Just an idea.

In general, you don't want to use a linked list for receiving
packet data. Way too slow.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
T

Thomas Matthews

I'm trying to create a program that will buffer data received in UDP
packets.

I'd like to use an STL object that will allow me to add on new data
like this:

buffer+=newdata;

I don't think I can use a string because I don't think it allows 0
characters.

Any ideas on what to use?

If you use std::vector, pre-allocate the size for the average
packet. You don't want std::vector to reallocate while you
are receiving data.

Also, search the web for "double buffering". A minimum of
two buffers used for send and receiving.

Many embedded systems pre-allocate a space for the data
packets. These are arrays of unsigned chars. The goal
is to haul the data in as fast as possible, then analyze
it. An array has the minimal access times and least
overhead.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top