title: How to store a persistent pointer to char* FROM std::string object?
body: <p>I have the following code:</p>
  <pre><code>                ResultItemAttributeClass theAttribute;
                  char* tmpObtainedValueName = (char*)obtainedValue.Name();
                  theAttribute.AttributeName = (char*)malloc(strlen(tmpObtainedValueName)*sizeof(char));
                  strcpy(theAttribute.AttributeName,tmpObtainedValueName);
                  string tmpObtainedValueType0 = obtainedValue.aDesc-&gt;TypeName();
                  char* tmpObtainedValueType = (char*) tmpObtainedValueType0.c_str();
                  theAttribute.AttributeType = (char*)malloc(strlen(tmpObtainedValueType) * sizeof(char));
                  strcpy(theAttribute.AttributeType, tmpObtainedValueType);
                  string tmpAttributeValue0= obtainedValue.asStr();
                  char* tmpAttributeValue = (char*) tmpAttributeValue0.c_str();
                  theAttribute.AttributeValue = (char*)malloc(strlen(tmpAttributeValue) * sizeof(char));
                  strcpy(theAttribute.AttributeValue, tmpAttributeValue);
  </code></pre>
  <p>ResultItemAttributeClass is declared as:</p>
  <pre><code>struct DLLDIR ResultItemAttributeClass {
      public:
        char* AttributeName;
        char* AttributeType;
        char* AttributeValue;
  };
  </code></pre>
  <p>DLLDIR is a macro for export to DLL, yes, I am creating DLL , and std::string has questionable marshalling.
  In the code above I am filling instance of this structure, and storing it in some array, which is contained inside another structure (it's complex, I know)
  But when I try to call free on it:</p>
  <pre><code>free(inPntr-&gt;allItems[i].allAttributes[j].AttributeName);
  </code></pre>
  <p>I get a HEAP CORRUPTION messsage:</p>
  <pre><code>HEAP CORRUPTION DETECTED: after Normal block (#2606781) at 0x0000021EBB4FD060.
  CRT detected that the application wrote to memory after end of heap buffer.
  </code></pre>
  <p>I have looked at similar questions about HEAP CORRUPTION :</p>
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/23375466/heap-corruption-detected-when-using-free">&quot;Heap corruption detected&quot; when using free()</a> - author writes char beyond array boundary, but I am not modifying the value</p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/14413091/heap-corruption-detected-when-i-try-to-freevoid-key">HEAP CORRUPTION DETECTED when i try to free(void * key)</a> - here is similar issue, but I do not see how it solves my question. Author appends '\0' to end of string too.</p>
  </li>
  </ul>
  <p>I tried accessing AttributeName , AttributeValue, AttributeType before calling Free, and it works.</p>
  <pre><code>LOG_F(INFO, &quot;| %s %s %s&quot;, inPntr-&gt;allItems[i].allAttributes[j].AttributeName, inPntr-&gt;allItems[i].allAttributes[j].AttributeType, inPntr-&gt;allItems[i].allAttributes[j].AttributeValue);
  </code></pre>
  <p>So, how do I keep the pointer to char* from std::string , so it does not get mingled and calling free on it does not cause crash . . . ?</p>