0

I have a map which holds CStringArray as its value. The declaration is something like

std::map<int, CStringArray&> string_list;

The purpose of this map is to store strings which are having same index number.

So, The first time when I am inserting the string, the process is working fine. But when I am inserting the second element for the same key, i.e, updating the CStringArray which is already present against key, the app is getting crashed.

PF below code FYI..

void FillMyMap(const int key, const CString& str) {
    string_list::iterator list_itr = my_mapp.find(key); //my_map is a member
    if (list_itr != my_mapp.end()) {
        (list_itr->second).Add(str);     //crash occured
    } else {
        CStringArray str_arr;
        str_arr.Add(str);
        my_mapp.Insert(std::pair<int, CStringArray&>(key, str_arr));   //working fine
    }
}

The same code worked fine when I replace "CStringArray" with "Vector of CStrings". Can you please explain me why this crash occurred? Thanks in advance.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 7
    `std::map string_list;` Huh?? Since when is it possible to store references in a map? – πάντα ῥεῖ Oct 22 '16 at 05:00
  • And since you're using `std::map`, what's wrong with using `std::vector` instead of `CStringArray`? – PaulMcKenzie Oct 22 '16 at 05:01
  • 1
    Think a little: You have the variable `str_arr` which is local inside the `else` only, and you store a reference to it. What happens when the `else` part is over and `str_arr` goes out of scope and is destructed? What will the reference be referencing? – Some programmer dude Oct 22 '16 at 05:02
  • Thank you @Someprogrammerdude. So, changing (list_itr->second).Add(str); to "CStringArray* arr = &(list_itr->second); arr->add(str); will this work ? – Bharadwaj Gali Oct 22 '16 at 05:07
  • Why not just use value semantics instead of pointers or references? A vector has copy semantics, unlike the CStringArray you are using. I bet the reason you're doing all sorts of pointer and reference gymnastics is because CStringArray disallows copying. Instead, just use `std::vector` and everything will just work. – PaulMcKenzie Oct 22 '16 at 05:10
  • No that's no different really, storing a pointer to something that no longer exists. Store a *copy*. – Some programmer dude Oct 22 '16 at 05:10
  • @PaulMcKenzie, First I declared the map to store just CStringArray object only. But then, It gave compiler error because CStringArray has some problem with object copying (may be some member inside it is protected or private) So I changed the syntax to to take referecne. – Bharadwaj Gali Oct 22 '16 at 05:13
  • @BharadwajGali So basically I was right. Drop using CStringArray and just use `std::vector`. You gain nothing from CStringArray. – PaulMcKenzie Oct 22 '16 at 05:14
  • @PaulMcKenzie yes. But then, my question should be something like, what is the proper way to insert CStringArray inside a map. – Bharadwaj Gali Oct 22 '16 at 05:15
  • @BharadwajGali _"But then, my question should be something like, what is the proper way to insert CStringArray inside a map."_ Probably yes. You seem to barking up the wrong trees. – πάντα ῥεῖ Oct 22 '16 at 05:38
  • You can't put reference like this `std::map`, you are thinking about MFC containers. If you had `std::map` then it would work in VS2015, but not in VS2010. – Barmak Shemirani Oct 22 '16 at 05:42
  • 1
    You probably need something a lot more fundamental. Get a few books from [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329) (as you don't appear to have a firm grip on the concepts *copy* and *reference*). – IInspectable Oct 22 '16 at 16:43

0 Answers0