-1

I am using global declaration of map<string,string> type.

  1. If i execute the code, is the strings are created in dynamic memory?
  2. Is the map is created on the dynamic memory or static memory?

-

#include <iostream>
#include <map>
#include <string> 

std::map<std::string, std::string> mymap;

class myObject
{
public:
    myObject()
    {
        mymap["A"] = "AString";
        mymap["B"] = "BString";
        mymap["C"] = "CString";
    }
};

int main()
{
    myObject obj1;
    std::cout << mymap["B"] << std::endl;
    return 1;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
buddy
  • 1
  • 3

3 Answers3

0

There is a fixed size object representing the map itself, which you have made global (same kind of storage as static). That will have some form of pointers to the dynamic memory that contains the variable size contents of the map. That includes the strings in dynamic memory, but also other stuff (implementation dependent) in dynamic memory.

JSF
  • 5,281
  • 1
  • 13
  • 20
  • When talking about strings, one must take SSO into account. With VC++, the memory for the small strings in the OP's example would not be allocated dynamically. – Christian Hackl Jul 17 '15 at 17:02
  • The basic (fixed size) object of each string in the map is allocated dynamically, so SSO does not change the fact that the string is allocated dynamically, just the details of that allocation. – JSF Jul 17 '15 at 22:26
  • Of course, but from the string's POV, it is not dynamic. The pointer in the map points directly to the string data, not to another pointer, or so to speak. – Christian Hackl Jul 18 '15 at 06:57
0

A correct, complete answer to this question may be more complicated than one might think. I'll give you some hints:

  • The C++ standard leaves details of memory management to the implementation. You should really not care too much about this. Concentrate on the semantics of your application logic and leave memory management to the compiler.

  • Unlike raw arrays (or std::array, for that matter), std::map and std::string (actually, std::basic_string) are containers whose size can change during execution, so the classes typically (but not always, see below) cannot contain all elements themselves. They will instead contain a pointer to dynamically allocated memory where the actual contents are kept. The size of (literally the sizeof) the std::map and std::string objects themselves is independent of their current contents.

  • For dynamic allocation, std::map and std::string use std::allocator by default. std::allocator uses placement new to allocate memory (not new[]).

  • In theory, you could use a custom allocator and not the default one, performing any memory allocation in a completely different way, although this is rarely done.

  • std::string can be implemented using Small-String Optimisation (SSO), in which case there will be no dynamic allocation at all for small strings. Visual C++ uses this technique.

  • Copy-on-Write (COW) is another traditional way to implement strings based on the idea that identical strings can share the same dynamically allocated contents. In such a scenario, the pointers in multiple std::string objects would point to the same address. However, this technique has always been problematic in multi-threading scenarios and has been practically disallowed by C++11; I've just mentioned it for the sake of completeness.

  • Since C++11, the contents of std::string are formally required to be contiguous, i.e. stored one after the other in memory, such that you can take the address of the first element and treat the contents as an array.

Community
  • 1
  • 1
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
0

actually this map<string,string> need to be moved to the shared memory so wanted to know how the memory is allocated, if i wanted to move map with strings i need to follow different form of memory management.

if anyone knows how to achieve this then please let me.

buddy
  • 1
  • 3