Re: Length of C++ arrays allocated by operator new[]
Yannick Tremblay <yatremblay@bel1lin202.(none)> wrote:
Implement a generic container using void *. That way, your code size
will be a few % points smaller.
That's actually how generic containers are implemented in most other
OO languages (and even some non-OO ones). Of course it's much safer in
those because the language hides the details behind the safer syntax.
The problem is that yes, the executable size decreases a bit, but at
the cost of significantly increasing memory consumption (usually by
significantly more than the reduction in binary size). That's because
all the objects have to be allocated dynamically, adding memory
management overhead, and handled through references/pointers (which
are inexistent when dealing with objects by value, as in the case
of a std::vector having objects as elements).
(As a side-effect of this the data container becomes polymorphic,
which is sometimes nice, but in practice this is actually needed much
less frequently than containers with uniform elements.)
When people complain about the executable size growing, they seldom
seem to offer a viable alternative that wouldn't trade executable size
with an order of magnitude or two of increased memory usage.