OT: should I?

Started by
13 comments, last by Lev Povalahev 21 years, 10 months ago
So it basically works like realloc, doesn''t it.

Since I have my own container classes I''ll leave them as they are but next time I''ll need containter classes I''ll try STL. I''m a bit sceptical since I don''t like automatic memory management that I haven''t written myself, but I guess its just an old habit that comes from the days of procedural programming.

Anyway thanks for replies!

Regards,
-Lev
Advertisement
>> So it basically works like realloc, doesn''t it.

Yes.

>>I''m a bit sceptical since I don''t like automatic memory management that I haven''t written myself

First, many implementations do realloc every time you push. They allocate more elements than needed just in case (typically 8 at a time).

Furthermore, you can always use your own allocators with STL if you want to avoid using the built-in C++ ones. The second optional template arg for most STL structs is this allocator.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
The default allocator generally grows the capacity by a factor of 2 when size exceeds capacity to amortize the cost of allocation over several push_back calls (this is different from the default behavior of the java collections classes which increase size linearly). Again, however, its implementation specific. If you want some other form of behavior, you need to supply your own allocator.
I would suggest that everyone here needs to read about containers and how they work. If you use insert and some other methods on vectors, your vector will NOT allocate sequential elements in memory.

http://www.sgi.com/tech/stl/Vector.html

Its important to remember that vector is based on a random access container. Also memory may be allocated sequentially, but elements many not be sequential in memory - use insert and see what I mean.

If you dont use some methods then you may be able to access them sequentially, but it is NOT in the standard, so it should never be assumed that this will remain true in any circumstance on any platform. And if you are making classes that allow container inserts then obviously never access the memory like an array pointer.

I would also suggest that the ''top tip'' is very dangerous assumption and should properly describe the possible problems.

I think the reason people assume all this to be true, is that very few people obviously use the whole container feature set. Please remember that containers can do many many different things, and they are built for many possible implementations - hence the name, ''standard template library''.

I hope this helps.


I''ve been using STL for more than a year and I like it. It''s fast and easy to remember since most of the container functions are named the same. I''ve also used some exotic features but normally I don''t bother with them. There are some bugs in vc++6 stl that I found but nothing major. There are some tricks that speed up the stl like allocating space for vector before filling it, etc. I prepend std:: namespace to stl containers instead of opening namespace up by ''using namespace std''(I think). Go ahead and try stl and see how you like it. I''m using dinkum''s stl included with vc++6.

This topic is closed to new replies.

Advertisement