STL buffer

Discussion in 'C++' started by mike7411@gmail.com, Oct 18, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. On 2006-10-18, <> 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?


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

    --
    Salvatore Iovene
    http://www.iovene.com
    Salvatore Iovene, Oct 18, 2006
    #2
    1. Advertising

  3. red floyd Guest

    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));
    red floyd, Oct 18, 2006
    #3
  4. David Harmon Guest

    On 18 Oct 2006 11:44:09 -0700 in comp.lang.c++,
    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.
    David Harmon, Oct 18, 2006
    #4
  5. Michael Guest

    > 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
    Michael, Oct 18, 2006
    #5
  6. Salvatore Iovene wrote:
    > On 2006-10-18, <> 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?

    >
    > 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
    Nick Keighley, Oct 19, 2006
    #6
  7. ralph Guest

    Nick Keighley schrieb:

    > Salvatore Iovene wrote:
    > > On 2006-10-18, <> 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?

    > >
    > > 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
    ralph, Oct 19, 2006
    #7
  8. Salvatore Iovene wrote:

    > On 2006-10-18, <> 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?

    >
    >
    > 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
    Thomas Matthews, Oct 20, 2006
    #8
  9. 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
    Thomas Matthews, Oct 20, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,353
    John Harrison
    Jun 21, 2004
  2. Tomasz Grobelny
    Replies:
    4
    Views:
    2,199
    Mike Wahler
    Jan 1, 2005
  3. Replies:
    2
    Views:
    592
    sergejusz
    Mar 26, 2007
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    403
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    264
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page