Re: A few questions on C++
D. Susman wrote:
1) Should one prefer short / long to int in favour of better
compability? Does the sizeof(int) still vary dramatically between
Dramatically? How much is dramatically? It's still between INT_MAX
and INT_MIN, IIRC. If you need specific sizes, use 'intNN_t' types
soon to be available in a compiler near you (if they aren't yet).
'int' is always "the natural platform size" for numbers, as far as
integer arithmetic is concerned. In most cases it's the fastest way
to perform integer arithmetic (but don't take my word for it, measure
the performance instead of guessing).
2)Should one check a pointer for NULL before deleting it? Although
deleting a NULL pointer is said to be safe in C++, I see a lot of code
doing the check before deletion. Is this because they still preserve
the C attitude?
No need to check. 'delete NULL' is a NOP. No comment on C attitude.
AFAIK 'free(0)' is also a NOP.
3) When is a struct favourable over a class?
When you need compatibility with C.
4) Should one favor '++iterator' over 'iterator++'? I did some
performance tests myself and did not see a big gap between their
execution times ( by using containers with size 10000000 which contain
It depends. If you don't need the "previous value", use pre-increment.
If you need the previous value _and_ have the iterator increment itself
at the same time, use post-increment.
You did the right thing by measuring the performance. I did the same
thing in our application and came up with _some cases_ where it was
quite apparent that if all else is the same (no need to use the value)
switching to pre-increment would improve speed. You need to ask
yourself: even if the improvement is small, but everything else is the
same, why *not improve* the performance?
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask