Reassigning references (to std::map in this case)...

Discussion in 'C++' started by Tobe, Sep 6, 2007.

  1. Tobe

    Tobe Guest


    Here's an example of something that feels like it should be OK but
    does in fact produce a segfault on every compiler I've tried (VC2005, g
    ++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked // KABOOM is the
    one that segfaults. If I change the references to pointers (and make
    the appropriate dereferences) everything works just fine so is this
    some finer point of references I'm not grasping or an issue with the
    STL ?

    Answers gratefully received..



    #include <map>
    #include <string>
    #include <stdio.h>

    class Entry;
    typedef std::map<std::string, Entry> EntryMap;

    class Entry
    std::string iValue;
    EntryMap iEntries;
    Entry *iParentEntry;

    Entry() { iParentEntry = NULL; }

    int main(int argc, char* argv[])
    EntryMap entries;

    entries["1"].iEntries["2"].iEntries["3"].iValue = "three";

    EntryMap &entriesRef1 = entries["1"].iEntries;
    EntryMap &entriesRef2 = entries["1"].iEntries["2"].iEntries;

    printf("%s\n", entriesRef2["3"].iValue.c_str());

    entriesRef1 = entriesRef2; // KABOOM
    printf("%s\n", entriesRef1["3"].iValue.c_str());

    return 0;
    Tobe, Sep 6, 2007
    1. Advertisements

  2. NEVER put anything beside your signature after the "-- " (the signature
    separator). Now I have to manually drag your source code in here...
    So, 'entriesRef2' is a reference to an entry in a member of the
    'entriesRef1's referred object.
    What happens? You start overriding 'entries["1"].iEntries' value by
    means of assigning to a reference to it. And at the same time you
    probably want to continue using it (since the right-hand side still
    refers to it). It's not going to work. 'entriesRef2' becomes invalid
    as soon as the assignment to 'entriesRef1' begins because assignment
    cleans out the contents of 'entries["1"].iEntries' thus deleting what
    is behind the 'entriesRef2' along with it.

    Victor Bazarov, Sep 6, 2007
    1. Advertisements

  3. Tobe

    Default User Guest

    Actually, he didn't have "-- ", but "--". I assume your newsreader is
    not strict on .sig separators.

    Default User, Sep 6, 2007
  4. Tobe

    James Kanze Guest

    Or yours truncates trailing whitespace in some cases. I see
    "-- " with Google news (and I'd be surprised if even Google adds
    a trailing white space).
    James Kanze, Sep 7, 2007
  5. * James Kanze:
    The original message had only "--".

    From earlier discussion of this, it seems that when posting, Google
    Groups strips the space at the end of "-- ", because stripping it will
    cause most harm and confusion, and from your comment it seems that when
    reading, Google Groups adds a space, because that will cause most harm
    and confusion. If Google Groups had done nothing, signature delimiters
    would work perfectly. They're /actively/ messing them up.

    And of course there is no obvious way to report problems with Google
    Groups -- you're led on a wild goose tour leading nowhere.


    - Alf

    CC: Oh, I would CC to Google Groups technical support or customer
    service or whatever, if any address was available (of course it isn't).
    Alf P. Steinbach, Sep 7, 2007
  6. EntryMap &entriesRef1 = entries["1"].iEntries;
    Your map entriesRef1 contains the map referenced by entriesRef2. When
    you assign entriesRef2 to entriesRef1, entriesRef1 will be emptied
    first, which will delete entriesRef2, because it is inside
    entriesRef1. Then you will try to assign this dangling reference
    entriesRef2 into entriesRef1, which fails.
    mike.polyakov, Sep 7, 2007
  7. Tobe

    James Kanze Guest

    Maybe. There are so many intermediaries involved that I'm no
    longer sure who's doing what. But it's true that back in the
    old days, before Google and IE, things seemed to work better.

    FWIW: making trailing spaces significant is a major error in the
    specification, since some more exotic systems don't have any
    means of maintaining them. (The existance of such systems is
    why C and C++ don't guarantee them in text mode.)
    I reported a problem once to them, and got a response. I don't
    remember where I sent it, but I do know that it took some effort
    to find the address, and if I remember correctly, you need a
    Google account to access it.
    James Kanze, Sep 8, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.