Re: memory leak in the code?
On Jan 14, 6:20 am, Jerry Coffin <jcof...@taeus.com> wrote:
In article <7a6ef05b-eff9-4f21-bf22-
86e0b9618...@j78g2000hsd.googlegroups.com>, grizl...@yandex.ru says...
[ ... ]
3. If requested concept of array is less than concept of
vector<>, then vector<> can produce poor code, because
not-used properties of more complex concept of vector<> can
produce not-used data and code. "will do produce" or "will
do not produce" depends only from concrete implementation of
concrete vector<> for concrete target arch. In order to
detect, you need refer to the concrete implementation of of
concrete vector<> for concrete target arch. (Any reference
to implementation violates encapsulation - bad desing way).
This is more or less the case: vector can do things an array
cannot do on its own. The most obvious is that a vector can
expand to hold more items as you insert them, which an array
cannot. If you're really never using that capability, your
program can include code that's never used.
The cost of this dead code is relatively low though -- it's
stored on disk, but if it's never used, it may never even be
paged into physical memory.
It's less than that: the functions which implement it are
templates, which aren't instantiated unless they are used.
To support that, std::vector also uses dynamically allocated
memory, which means all access to the memory is via a pointer.
Depending on the processor and such, this _may_ be marginally
slower than direct access to the memory (though, in all
honesty, it's been quite a while since I saw a processor for
which this was likely to be an issue).
On a Sparc (and I suspect most RISC machines), accessing through
the pointer is actually faster. (Of course, all of the
compilers I know convert the array code to use pointers
internally, so typically, there's no difference.)
For situations where these constitute real problems, TR1 and
C++ 0x both include an array class that acts more like a
built-in array -- it's size is set at creation time, and
remains constant until it's destroyed.
More importantly, it actually acts like a real data type; it
doesn't convert to a ponter (loosing the size) unless you want
it too.
One of the reasons people don't like C style arrays is that they
don't implement the concept of array very well. Conceptually,
an array is a data type, and in C/C++, data has value semantics.
Conceptually, an array has a length, and in C/C++, as soon as
the implicit conversion to a pointer has occured, the
abstraction is broken. Conceptually, std::vector<T> is a closer
approximation of an array than is T[].
[...]
At least with the compilers I've tested so far, std::sort is
consistently at least twice as fast as qsort. std::sort on an array is
sometimes faster than std::sort on a vector -- but sometimes it's
slower. I'm pretty sure you'd have to do the test extremely carefully
(and often) to be sure whether there's a statistically significant
difference between the two. Even if there is a difference, I'm quite
certain it's too small to care about most of the time.
If there is a difference, then it will depend on the compiler
and the architecture. One may be faster with one compiler or
architecture, and slower with another.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34