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 4 1787
"syncman" <hs***@hotmail.com> wrote in message news:cc**************************@posting.google.c 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...
"syncman" <hs***@hotmail.com> wrote in message
news:cc**************************@posting.google.c 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
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
syncman wrote in news:cc**************************@posting.google.c 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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: grahamo |
last post by:
Hi,
I am trying to use STL copy routine to stream the my user defined type
to std out. I am using a standard ostream_iterator object in
conjunction with my type (called employee) which has
...
|
by: YinTat |
last post by:
Hi,
I learned C++ recently and I made a string class.
A code example is this:
class CString
{
public:
inline CString(const char *rhs)
{
m_size = strlen(rhs);
|
by: rokia |
last post by:
in a project, I use many,many stl such as stack,list,vctor etc.
somewhere the vector's size is more than 2K. is this a efficient way?
|
by: Joe |
last post by:
I have a: vector<string> which contains a few dozen elements.
I want to find the index of the element containing a certain string.
for example:
vector<string> strings;...
|
by: Paul |
last post by:
Hi,
Global operator new and delete are overloaded and I am using stl map
to store pointers, but this code crashes, can some one shed some
light???
Compiler: MS VC++ 6.0
STL: Shipped with...
|
by: Christian Christmann |
last post by:
Hi,
I'd like to store structs on an STL stack.
Here is a piece of my code:
#inclue <stack>
...
struct storeInfo {
|
by: blangela |
last post by:
Someone has asked me what the relationship is, if any, between iostream
and the STL. My suspicion is that iostream uses some of the
functionality provided by the STL, but I have no actual kowledge...
|
by: belier |
last post by:
Hello.
I'm making an application that works with buffers. I have to call some C++ functions with data pointers as arguments. I'm trying to not use unsafe code.
One of the functions have two...
|
by: madhav001 |
last post by:
Hi,
I am biginner in C++ and I tried to combine two memory buffers of TIFF image data to one so that to save it as one image. ie: left half image + right half image = a full image. That was not...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |