News for hackers. Stuff that matters.
Re:Zeroing Memory is Hard: VC++ 2015 arrays by brucedawson on Monday July 18, 2016 @02:45AM (#12113542)
But reserve() doesn't work if you are going to fread() into the buffer or otherwise fully initialize it. std::vector<char> basically requires you to double-initialize the buffer if you pass it to a function that just needs a raw pointer to fill, which can be an issue in some high-performance contexts.

I hit this in a game server where a few percent of CPU time (that was enough to count as low-hanging-fruit) was being spent on memset inside of resize().

Re:Zeroing Memory is Hard: VC++ 2015 arrays by someguydave on Monday July 18, 2016 @08:34AM (#12114658)
Yes I am in proecisely this situation. It made me realize how political the c++ std library authorship is.
Re:Zeroing Memory is Hard: VC++ 2015 arrays by honkhonkpants on Monday July 18, 2016 @03:22AM (#12113632)
What was the solution? Using a custom allocator?
Re:Zeroing Memory is Hard: VC++ 2015 arrays by deadmutex on Monday July 18, 2016 @05:29AM (#12114012)
I am not OP, but you could get around it by having a default constructor that is empty. This might've not been possible if you need a different default constructor that initializes values.
Re:Zeroing Memory is Hard: VC++ 2015 arrays by brucedawson on Monday July 18, 2016 @11:26AM (#12115611)
In my case the fix was to not allocate so much space. The vector was always being resized to 64 KiB. After considering lots of other options I decided that the most pragmatic fix was just a smaller constant to the vector constructor. I don't remember why the other options weren't practical.
Re:Zeroing Memory is Hard: VC++ 2015 arrays by someguydave on Monday July 18, 2016 @08:31AM (#12114647)
Yes you could use a custom allocator except that changes the type of the std::vector - making a large ripple in our code base.

I have not solved it yet. The best solution - don't use std::vector for anything critical.

Re:Zeroing Memory is Hard: VC++ 2015 arrays by andreyv on Monday July 18, 2016 @02:01PM (#12116899)
A simple new int[] will work. It doesn't initialize the array (unlike the new int[]() version).

You can use it with std::unique_ptr<> to simplify memory management.

Re:Zeroing Memory is Hard: VC++ 2015 arrays by SeanBoocock on Monday July 18, 2016 @08:30PM (#12118790)
This is true but at least with the current standard you will want to define a custom deleter with your unique_ptr to implement "delete [] data" vs the default behavior of "delete data".
Re:Zeroing Memory is Hard: VC++ 2015 arrays by gpderetta on Monday July 18, 2016 @06:59AM (#12114317)
Using a custom allocator is the standard-blessed solution for this problem, although it can be inconvenient.