Re: byte buffer implementation
On Saturday, August 3, 2013 2:50:02 AM UTC-5, fmatthew5876 wrote:
In practice the signed vs unsigned char issue hardly matters
unless you're treating the chars themselves as 8 bit ints.
This is the impression I've had also.
I have no qualms with C style arrays especially for a temporary buffer
for doing IO. There's nothing faster than allocating a fixed buffer
on the stack.
std::array is just like a struct containing a C array
except that it always initializes its members. This is a slight
but not really problematic inefficiency if you're just
about to write to the buffer.
I don't think std::array initializes its members:
#include <array>
#include <vector>
#include <iostream>
int main()
{
int oldschool[200];
std::array<int, 200> a;
std::vector<int> v(200);
std::cout << "oldschool[134] = " << oldschool[134] << std::endl;
std::cout << "a[134] = " << a[134] << std::endl;
std::cout << "v[134] = " << v[134] << std::endl;
return 1;
}
oldschool[134] = -887447128
a[134] = -41345424
v[134] = 0
You can use a unique_ptr with operator new [] to have a heap allocated
fixed size array. Use unique_ptr, not a raw pointer so that it is cleaned up
automatically.
std::vector is flexibly growing array that you can append and
insert into forever. If you need this append behavior then use a vector.
If you don't then use one of the other options.
I have a place where I'm thinking about switching from vector
to array, but am not sure about heap/stack potential problems.
The buffers could be large - over a megabyte.
Brian
Ebenezer Enterprises
http://webEbenezer.net
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]