Re: Style Question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Tim Roberts" <timr@xxxxxxxxx> wrote in message
news:r8d1m3p46ela7et3qtpakk3ac3br16ogje@xxxxxxx
I have an application where I need a queue of buffers whose size
changes relatively often. I was doing it with a "char *" with
separate Size and MaxSize members, but it seemed like a great
candidate for a std::vector<char>. After the size changes, I fill it
immediately, so the zero filling that vector::resize does is useless
to me, and it costs a fair amount.

You can reserve() instead, and push_back() or insert() new elements.

So, I derived a class from std::vector<char>, and overrode resize,
using knowledge of the VC++ STL implementation:

class ByteVector
: public std::vector<char>
{
...
void resize( size_t newSize, char init = -1 )
{
if( (init == -1) && (_Myfirst + newSize <= _Myend) )
_Mylast = _Myfirst + newSize;
else
std::vector<char>::resize( newSize, init );
}
};

This solves my problem neatly, but is this considered very bad form?
It's certainly true that this won't work with another STL
implementation. Would you fire a programmer that did this to you?

I would seriously consider it, yes. The code will definitely not pass my
code review and won't be submitted as long as I have any say in the
matter. At the very least, the author would have to prove to me that a)
avoiding zero-initialization causes a measurable performance
improvement, and b) there's no other way to avoid it (and I have my
doubts on both points).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: A few questions on std::string
    ... >c_strappends the terminating null char? ... >understand the STL implementation but I've oft been of the impression ... >string class that uses a vector underneath and provides the STL string ...
    (alt.comp.lang.learn.c-cpp)
  • Re: STL Queue question
    ... Yes, use strings, not chars. ... or check out the docs for your STL implementation. ... Joe Richards Microsoft MVP Windows Server Directory Services ... >> char msgtest; ...
    (microsoft.public.win32.programmer.kernel)