binary data buffers: vector<char> or std::string ?

binary data buffers: vector<char> or std::string ?

Post by Neil Butte » Wed, 25 Feb 2009 03:00:23



I would use std::vector, because it has a natural constructor for
creating buffers:

const int BUFSIZE = 1024; // or whatever
std::vector <char> buffer( BUFSIZE );


Neil Butterworth


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by vdk » Wed, 25 Feb 2009 16:29:41

> I would use std::vector, because it has a natural constructor for

Just be warned that there are compilers (MSVC.NET 2003 for one) that
do not have the std::vector<> template specialized for char (or
similar) thus the constructor call above will ultimately lead to a
loop initializing the vector's components one by one(!).


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
 
 

binary data buffers: vector<char> or std::string ?

Post by Bo Persso » Wed, 25 Feb 2009 22:20:33


This might of course be because there are compilers that are smart
enough to optimize this properly anyway, so that they don't need
specializations for POD types.


Bo Persson


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Neil Butte » Thu, 26 Feb 2009 04:26:46

On Feb 24, 7:01 pm, Francis Glassborow



How are you going to arrange that in a way that vector <unsigned char>
cannot?

Neil Butterworth


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Hakus » Thu, 26 Feb 2009 04:27:11


Why? And why not std::basic_string<unsigned char>?


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Francis Gl » Thu, 26 Feb 2009 22:38:57


A vector moves its contents when it expands.

--
Note that robinton.demon.co.uk addresses are no longer valid.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by vdk » Thu, 26 Feb 2009 22:49:46


That could be, I never checked the resulting (optimized) code. Still,
I'd wonder why then the std::basic_string<char> specialization uses
std::memset() in its constructor?

Vlado Klimovsky


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Jeff Schwa » Thu, 26 Feb 2009 23:38:30


Wouldn't the same be true of a hand-rolled buffer?

--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Neil Butte » Fri, 27 Feb 2009 02:17:44

On Feb 25, 7:38 pm, Francis Glassborow



Only if you actually expand it. My original suggestion was:

vector <char> buffer( BUFSIZE );

allocating a buffer not intended to be used with push_back() et al.
What's your alternative?

Neil Butterworth




--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Francis Gl » Sun, 01 Mar 2009 02:55:54


Perhaps we have a communication problem. I prefer a buffer to have fixed
location and that implies that it needs to be of fixed size (we could
use a deque though).

--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Francis Gl » Sun, 01 Mar 2009 02:56:08


char buffer[BUFSIZE]

Now you cannot use features that would change its size or location.

--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Neil Butte » Sun, 01 Mar 2009 07:29:34

On Feb 27, 11:56 pm, Francis Glassborow




You also cannot create a very large buffer without risking stack
overflow. What would you do if you needed a buffer to hold, say, a
large image?

Neil Butterworth


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Jeff Schwa » Sun, 01 Mar 2009 07:33:58


Sorry, then what's the problem with std::vector? I thought your point
was that if it grew, you would lose locality. (I suggested Boost or TR1
array elsethread, but I often use std::vector myself.)

--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Alexander » Sun, 01 Mar 2009 19:15:41


>> What should I prefer for binary data buffers, std::string or >> vecto< ?

scoped_array or shared_array from boost:: or std::tr1::


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

binary data buffers: vector<char> or std::string ?

Post by Francis Gl » Sun, 01 Mar 2009 19:19:05


Yes, but std::deque does not, elements stay where you put them and do
not suddenly get copied to somewhere entirely different.

(I suggested Boost or TR1

Yes tr1::array is fine but so is a raw array.

Perhaps we have different concepts of buffers.

--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]