468,491 Members | 2,024 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

STL buffer

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?

Oct 18 '06 #1
8 5601
On 2006-10-18, mi******@gmail.com <mi******@gmail.comwrote:
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.

--
Salvatore Iovene
http://www.iovene.com
Oct 18 '06 #2
mi******@gmail.com wrote:
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));
Oct 18 '06 #3
On 18 Oct 2006 11:44:09 -0700 in comp.lang.c++, mi******@gmail.com
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.

Oct 18 '06 #4
2. You should probably use a std::vector<unsigned char>, though, since
you may not receive string data.
You might want std::deque, since you'll probably want to pop data out
of the buffer too.

Michael

Oct 18 '06 #5
Salvatore Iovene wrote:
On 2006-10-18, mi******@gmail.com <mi******@gmail.comwrote:
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.
subclass == derive from?

can you derive from the std containers?

--
Nick Keighley

Oct 19 '06 #6

Nick Keighley schrieb:
Salvatore Iovene wrote:
On 2006-10-18, mi******@gmail.com <mi******@gmail.comwrote:
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.

subclass == derive from?

can you derive from the std containers?
You can. But you don't want to.

ralpe

Oct 19 '06 #7
Salvatore Iovene wrote:
On 2006-10-18, mi******@gmail.com <mi******@gmail.comwrote:
>>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.
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
Oct 20 '06 #8
mi******@gmail.com wrote:
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
Oct 20 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by inkapyrite | last post: by
4 posts views Thread by ma740988 | last post: by
2 posts views Thread by William Stacey | last post: by
2 posts views Thread by Bill Sun | last post: by
28 posts views Thread by bwaichu | last post: by
4 posts views Thread by aki | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.