Re: multiset segfault

From: Joaquín Mª López Muñoz (joaquin_at_tid.es)
Date: 06/17/04


Date: Thu, 17 Jun 2004 18:19:15 +0200


"Arthur J. O'Dwyer" ha escrito:
[...]

> > > Meanwhile, I'm still wondering what's wrong with my use of
> > > 'multiset'...
> >
> > Ummm... The problem lies in that you're storing pointers to a
> > *vector* inside myStrHash. But these pointers are *not* stable,
>
> Oh, bother! See, I knew it was something simple... :(
>
> > the elements inside myVector will be relocated when its size
> > grows; in your code this happens at about the second insertion.
> > You have two solutions:
> >
> > 1. Use an std::list insiead of an std::vector. std::list references
> > are stable, i.e elements won't be relocated over time.
>
> Aha. This would be the easiest solution, then. Thanks.
> For future reference, are there any other STL containers for
> which the elements' addresses are guaranteed to be stable?
> And I seem to recall that in a std::vector, the elements are
> guaranteed to be in contiguous memory --- but this is not true
> of std::list --- is it true of any other containers?

std::vector is the only container guaranteeing that elements
are in contiguous memory.

>
>
> I've been relying mostly on the Dinkum C++ library reference
> page and on Google for my STL information. Do you have any
> suggestions of a better STL reference for the casual C++ user?
> [Read: one that might manage to warn me about dumb mistakes like
> that one.]
>

Well,

* C++ FAQ Lite (http://www.parashift.com/c++-faq-lite/) contains
some sections on STL containers, and it is anyway worth reading the
whole contents even if not specifically dealing with STL.
* The reference at cppreference (http://www.cppreference.com/cpp_stl.html)
is a little more readable than Dinkumware's stuff.
* Thinking in C++ (http://www.bruceeckel.com/) is a C++ programming
guide downloadable for free. Vol II has some stuff on STL.

In general, if you google for "STL tutorial" you'll find many more
links. Beware to check the date of the docs you're reading: if it
is prior to 1998 (when the standard was published) then you'd rather
not use it, much info will be outdated.

HTH

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo



Relevant Pages

  • Re: Clean code vs. efficiency
    ... > instead of character arrays. ... The default STL ... In general, the STL containers are extremely fast, and might be faster than ... The STL allocators let you use pool allocators, but you can also that by ...
    (comp.lang.cpp)
  • Re: A solution to warning C4251 - class needs to have dll-interface...?
    ... Because the functions of the STL containers ... are completely defined by their header files. ... the static data for the entire program. ...
    (microsoft.public.vc.language)
  • Re: Cross platform C++ base library?
    ... This way, you should have both the cross-platform feature (because the class is built on top of STL), and the CString-like public interface: ... However, I learnt the basics of STL containers like vector and map, and now I'm glad to use these STL containers. ... If you really don't like STL, you could just use STL as the core to implement classes with an MFC-like interface (e.g. a David::CArray, ...
    (microsoft.public.vc.mfc)
  • Re: Clean code vs. efficiency
    ... > In general, the STL containers are extremely fast, and might be faster than ... this year given by a guy from the Xbox ATG. ... This speaker mentioned that in *every single case*, the STL (shipping ... much cleaner than those I wrote prior to that. ...
    (comp.lang.cpp)
  • Re: How to read Unicode(Big-Endian) text file(s) in Non-MFC
    ... Google is your friend here. ... The difference is that method atdoes a bounds-checking on index. ... So, using atis more secure, but more slow; ... There is a lot more, like using iterators (a very powerful STL concept, ...
    (microsoft.public.vc.language)