T
Tobe
Hi,
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..
--
Tobe
=========================
#include <map>
#include <string>
#include <stdio.h>
class Entry;
typedef std::map<std::string, Entry> EntryMap;
class Entry
{
public:
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;
}
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..
--
Tobe
=========================
#include <map>
#include <string>
#include <stdio.h>
class Entry;
typedef std::map<std::string, Entry> EntryMap;
class Entry
{
public:
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;
}