On 12/27/2011 11:27 PM, XeCycle wrote:
> "Asger Joergensen"<> writes:
>
>> Hi
>
> Hello,
>
>> I'm not that used to the stl so I ask to be on the safe side:
>>
>> typedef std::vector<char> TMembuf;
>>
>> TMembuf buf(1000);
>> char* p =&buf[0];
>>
>> I have tested it and it seem to work fine just like:
>>
>> char* p = new char[1000];
>>
>> and without the delete[];
>>
>> But is there any downside to doing like this ?
>
> This is not recommended, of course. You didn't even use any of
> the vector facilities, in this case. Vectors provide the method
> reserve().
WHAT is not recommended? And why the hell not?
And what use would Asger have for 'reserve()'? std::vector has a proper
constructor that allocates the buffer and makes it ready to use.
> However if you want a buffer, there's circular buffer in boost
> library.
Asger needed something analogous to an array allocated by 'new[]'. A
standard vector is just about what the library can offer. Boost does
not exist on every platform, while std::vector does. There is no sense
in using a non-portable library when the Standard library suffices.
V
--
I do not respond to top-posted replies, please don't ask
|