Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL: Do people still use char[] buffers?

Reply
Thread Tools

STL: Do people still use char[] buffers?

 
 
syncman
Guest
Posts: n/a
 
      01-20-2004
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-20-2004

"syncman" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> 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...


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      01-20-2004
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

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-20-2004

"syncman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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





 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      01-20-2004
syncman wrote in news:(E-Mail Removed) om:

> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do people still use C instead of C++ ? Lawand C++ 42 05-21-2011 03:44 AM
People Helping People!!!!!!!!!!!! celeste96795@aol.com Java 1 06-11-2008 05:49 PM
Some people tell that at present, most web hosting servers supportall kinds of programming language, some people tell me that many web hostingserver don't support Java, What is the truth? Erwin Moller Java 3 05-07-2008 05:09 PM
Do people still use a shutter release cable in digital camera age? aniramca@gmail.com Digital Photography 30 07-24-2007 04:55 AM
people helping people......king kong, underworld 2, mi3, movie, dvd,music, games cam slade Computer Support 1 01-19-2006 06:07 AM



Advertisments