STL: Do people still use char[] buffers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • syncman

    STL: Do people still use char[] buffers?

    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
  • Ron Natalie

    #2
    Re: Do people still use char[] buffers?


    "syncman" <hsjs3@hotmail. com> wrote in message news:cc9cc476.0 401201137.6e8c2 64b@posting.goo gle.com...[color=blue]
    > 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;
    >[/color]
    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...


    Comment

    • Howard

      #3
      Re: Do people still use char[] buffers?


      "syncman" <hsjs3@hotmail. com> wrote in message
      news:cc9cc476.0 401201137.6e8c2 64b@posting.goo gle.com...[color=blue]
      > 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[/color]

      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





      Comment

      • Thomas Matthews

        #4
        Re: STL: Do people still use char[] buffers?

        syncman wrote:
        [color=blue]
        > 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[/color]

        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:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Rob Williscroft

          #5
          Re: STL: Do people still use char[] buffers?

          syncman wrote in news:cc9cc476.0 401201137.6e8c2 64b@posting.goo gle.com:
          [color=blue]
          > 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)[/color]

          You could pass it safer as

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

          assuming SIZE is a compile-time const.
          [color=blue]
          >
          > I can assign to its middle with a moving pointer.
          > myStruct *p = (myStruct *) buf; // Cast
          > *p++ = myValue;
          >[/color]

          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.
          [color=blue]
          > 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>?
          >[/color]

          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.




          Rob.
          --

          Comment

          Working...