
On Aug 11, 2013, at 12:39 PM, Rob Stewart
On Aug 11, 2013, at 9:50 AM, Edward Diener
wrote: On 8/10/2013 8:59 PM, Rob Stewart wrote:
On Aug 10, 2013, at 8:25 AM, Edward Diener
wrote: std::vector<TYPE> is the C++ way of specifying arrays.
If you do that, don't use resize() and size(). Use reserve() and capacity(), and don't forget to push_back() one element to make &v[0] valid.
I do not follow this. Especially the bit about pushing back an element. Vectors can be empty as I am sure you know and there are still valid.
Accessing an element of an empty vector is undefined behavior. Calling resize() to reserve space also causes elements to be initialized, which is often not needed when calling an API that indicates the number of bytes written.
What I often do is the following:
std::vector<char> v; v.reserve(1024); v.push_back(0); read(&v[0], v.capacity());
That can be simplified by a wrapper class.
Rob -- Could you have meant "resize()" instead of "reserve()" here? -- Marshall Marshall Clow Idio Software mailto:mclow.lists@gmail.com A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki