   Using std::vector like a raw array - Is this good practise?
  Hello,I need a raw array for basic socket operations (recv / send). I could just use an unsigned char array, but I need my sequence to be resizable so I figured I'd just use std::vector. But I'm not completely sure whether that is even an intended use for std::vector because AFAIK it's real purpose is to act as a container class to store elements. Do you think it's okay to use std::vector< unsigned char > instead of just a plain array?   
  I don't see anything wrong with it. It performs that function perfectly and even includes the [] operator so you can use it as a dynamically-sized array.Just, if you are using a lot of data be careful not to let it overallocate through using push_back too much (it will allocate more than you need). Allocate what you need and it's as simple as that.Also consider if std::string is more suitable for your task, if your uchars store text data. If not, go ahead with the vector.   ;  Yeah, I intend to do one initial allocation to reserve as much space as I deem reasonable so push_back() won't have to reallocate all the time. But I still want the sequence to be resizable just in case I ever receive a message that is much larger than the norm.I'm dealing mostly with binary data so I suppose I'll stick with vector over string. Thanks for your input! =)   ;  Yes, std::vector is designed to be completely compatible with functions expecting raw arrays.  It is intended to be used for exactly this purpose (as well as others).  std::string on the other hand is not designed to be compatible as a destination buffer, so std::vector is really the better choice.  This thread expands slightly on the reasons for the latter.Enigma   ;  Thanks for your reply. Enigma, in the other thread you stated that ...Quote:Original post by Enigma... Writing into a std::vector that is appropriately sized is perfectly safe and not uncommon.  It must be resized though, not just reserved. (Emphasize mine)Why is that? Wouldn't a call to reserve() see to the allocation of contiguous memory that you can safely write to? Of course the vector wouldn't be aware of any elements not reflected in its count member, but are there are reasons why you say it is important to resize not just reserve?;  Reservation is an optimization to help the vector efficiently deal with memory. For all other intents and purposes, you should assume the reserved elements do not exist.   ;Quote:Original post by Red AntWouldn't a call to reserve() see to the allocation of contiguous memory that you can safely write to? Of course the vector wouldn't be aware of any elements not reflected in its count memberIsn't that reason enough?   ;  Yeah sorry, I kinda realized the idiocy of my question a few minutes after I had posted it. =0   ;  Reserved memory is not initialised.std::vector<char> v;v.reserve(100);memset( &*v.begin(), 1, 100 );v.resize(100);std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, " " ) );Will most likely show you 100 zeros, not 100 ones.   