title: Using a protobuf map of protobuf objects in C++
body: <p>I cannot seem to find any explicit mention of how protobuf maps that use non-scalar values can be manipulated in C++.
  For example consider this proto file:</p>
  <pre><code>syntax = &quot;proto3&quot;
  message X{
    uint32 name = 1;
  }
  
  message Y{
    map&lt;string, X&gt; values = 1;
  }
  </code></pre>
  <p>Notice that X is non-scalar. How would I go about inserting something into this map in C++? Do I need to dynamically allocate a X object or that is not necessary? For example are both pieces of code below correct? For the dynamically allocated one, would I need to explicitly deallocate the pointer after the insertion in the map? If yes what is the proper way to deallocate the pointer after copying the data in the map?</p>
  <p>code 1:</p>
  <pre><code>Y y;
  X * x = new X();
  x-&gt;set_name(123);
  auto map = y.mutable_values();
  (*map)[&quot;key value&quot;] = *x;
  </code></pre>
  <p>code 2:</p>
  <pre><code>Y y;
  X x;
  x.set_name(123);
  auto map = y.mutable_values();
  (*map)[&quot;key value&quot;] = x;
  </code></pre>