STL: Do people still use char[] buffers?

Discussion in 'C++' started by syncman, Jan 20, 2004.

  1. syncman

    syncman Guest

    I want to do a lot of data manipulation and passing. The buffer I
    have always used before STL was a simple char buf[SIZE] .

    I can pass its address easily.
    void f (char *buf)

    I can assign to its middle with a moving pointer.
    myStruct *p = (myStruct *) buf; // Cast
    *p++ = myValue;

    etc

    Should I be using a vector<char> or something? I know that it would
    provide better type safety (no cast), and would catch overruns. Also,
    it would grow as needed, but I don't need that here because I know the
    size beforehand. Is it slower? Any other considerations?

    Can you say that there is NO reason why anyone should use char[]
    instead of vector<char>?

    Thanks
     
    syncman, Jan 20, 2004
    #1
    1. Advertising

  2. syncman

    Ron Natalie Guest

    Re: Do people still use char[] buffers?

    "syncman" <> wrote in message news:...
    > I want to do a lot of data manipulation and passing. The buffer I
    > have always used before STL was a simple char buf[SIZE] .
    >
    > I can pass its address easily.
    > void f (char *buf)
    >
    > I can assign to its middle with a moving pointer.
    > myStruct *p = (myStruct *) buf; // Cast
    > *p++ = myValue;
    >

    You can't portably do that STL or not.
    You're going to have to tell us what you're really trying to do. Why
    do you have the struct in a char array? Who allocated it, how did
    the value get set, etc...
     
    Ron Natalie, Jan 20, 2004
    #2
    1. Advertising

  3. syncman wrote:

    > I want to do a lot of data manipulation and passing. The buffer I
    > have always used before STL was a simple char buf[SIZE] .
    >
    > I can pass its address easily.
    > void f (char *buf)
    >
    > I can assign to its middle with a moving pointer.
    > myStruct *p = (myStruct *) buf; // Cast
    > *p++ = myValue;
    >
    > etc
    >
    > Should I be using a vector<char> or something? I know that it would
    > provide better type safety (no cast), and would catch overruns. Also,
    > it would grow as needed, but I don't need that here because I know the
    > size beforehand. Is it slower? Any other considerations?
    >
    > Can you say that there is NO reason why anyone should use char[]
    > instead of vector<char>?
    >
    > Thanks


    One of the reasons of not using an array of characters is buffer
    overflow or overrun. There is a rule that states no matter the
    size you allocate, it is not enough.

    When the number of characters is dynamic, use either a std::string
    or a vector. Those data types handle dynamic allocation. If
    the quantity will always be fixed or less than a maximum size,
    go ahead and use an array.

    As for the difference in passing parameters of type vector,
    string or array, there is no difference in the cost. One should
    pass pointers or references to parameters of these types.

    --
    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.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jan 20, 2004
    #3
  4. syncman

    Howard Guest

    Re: Do people still use char[] buffers?

    "syncman" <> wrote in message
    news:...
    > I want to do a lot of data manipulation and passing. The buffer I
    > have always used before STL was a simple char buf[SIZE] .
    >
    > I can pass its address easily.
    > void f (char *buf)
    >
    > I can assign to its middle with a moving pointer.
    > myStruct *p = (myStruct *) buf; // Cast
    > *p++ = myValue;
    >
    > etc
    >
    > Should I be using a vector<char> or something? I know that it would
    > provide better type safety (no cast), and would catch overruns. Also,
    > it would grow as needed, but I don't need that here because I know the
    > size beforehand. Is it slower? Any other considerations?
    >
    > Can you say that there is NO reason why anyone should use char[]
    > instead of vector<char>?
    >
    > Thanks


    I use char arrays all the time. I use the string class a lot more now,
    where appropriate, because it's just plain better than plain char
    arrays...for strings, anyway. But I don't treat my classes/structs as char
    arrays, or vise versa. Is this just an example, or are you really doing
    that? Some kind of serialization you're doing perhaps? For just plain char
    data, the vector seems like overkill. Just use the string class instead.

    -Howard
     
    Howard, Jan 20, 2004
    #4
  5. syncman wrote in news::

    > I want to do a lot of data manipulation and passing. The buffer I
    > have always used before STL was a simple char buf[SIZE] .
    >
    > I can pass its address easily.
    > void f (char *buf)


    You could pass it safer as

    void f( char (&buf)[ SIZE ] )

    assuming SIZE is a compile-time const.

    >
    > I can assign to its middle with a moving pointer.
    > myStruct *p = (myStruct *) buf; // Cast
    > *p++ = myValue;
    >


    This requires that myStruct has the same alignment requirments
    as a char (i.e. no alignment requirments :), you'll get away
    with it many platforms but *not* all.

    > etc
    >
    > Should I be using a vector<char> or something? I know that it would
    > provide better type safety (no cast), and would catch overruns. Also,
    > it would grow as needed, but I don't need that here because I know the
    > size beforehand. Is it slower? Any other considerations?
    >
    > Can you say that there is NO reason why anyone should use char[]
    > instead of vector<char>?
    >


    You should be using an array or vector of myStruct. Use an array (*)
    if your size is a constant and you can resonably define this array
    as a local variable, otherwise use std::vector< myStruct >.

    (*) also checkout boost::array<> as an alternative to inbuilt
    array's this will give you an STL interface, so it can easily be
    changed to std::vector if the need arises.

    http://www.boost.org/libs/array/array.html


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jan 20, 2004
    #5
    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. Patrick Stinson

    lenght of char buffers in PyArg_ParseTuple

    Patrick Stinson, Jun 10, 2004, in forum: Python
    Replies:
    1
    Views:
    413
    Duncan Booth
    Jun 10, 2004
  2. Simon Wittber
    Replies:
    16
    Views:
    604
    Steve Holden
    Jan 14, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,124
    Ian Collins
    May 9, 2006
  4. Replies:
    1
    Views:
    742
    Roger Lindsjö
    Jun 11, 2008
  5. Lawand
    Replies:
    42
    Views:
    7,831
    sorin12345
    May 21, 2011
Loading...

Share This Page