STL: Do people still use char[] buffers?

S

syncman

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
 
R

Ron Natalie

syncman said:
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...
 
T

Thomas Matthews

syncman said:
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
 
H

Howard

syncman said:
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
 
R

Rob Williscroft

syncman wrote in
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fstream Buffers 26
Can't solve problems! please Help 0
C Containers Library vs STL 14
How to manage static buffers 2
Casting int from char pointer 13
STL Vector Access 1
String Buffers? 5
vector<char> -> file advice 7

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top