Re:Zeroing Memory is Hard: VC++ 2015 arrays
by someguydave on Monday July 18, 2016 @01:34AM
(#12113379)
Far worse is trying to grow a std::vector with a std::vector.resize() and subsequently trying to opt out of array intialization.
[ Parent
| on HN ]
Re:Zeroing Memory is Hard: VC++ 2015 arrays
by lorenzhs on Monday July 18, 2016 @11:22AM
(#12115591)
Most STL containers with the default allocator will always initialise memory (some, like std::array, don't iirc). That's a feature. If you don't want it, then you'll have to use your own allocator or container. Both bring changes throughout the codebase, unfortunately.
> Both bring changes throughout the codebase, unfortunately.
That's where auto often helps. Lets you change types as long as you don't break the interface.
Then you might need std::vector::reserve() instead.
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.
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".