title: How do I pass a std::string as a constructor argument and store the C string it holds in a void pointer?
body: <p>I have a constructor that accepts a <code>const</code> reference to a <code>std::string</code> object and is supposed to store the <code>std::string</code>'s internal C string in a <code>void</code> pointer member variable. However, my program segfaults when the <code>void</code> pointer is used, and <code>gdb</code> says that the pointer is pointing to address <code>0x01</code>. Here is my source:</p>
  
  <p>Foo.h:</p>
  
  <pre><code>class Foo {
      public:
          Foo(const std::string&amp; str);
          void* getData();
  
      private:
          Foo(void* newData);
          void* data;
  };
  </code></pre>
  
  <p>Foo.cpp:</p>
  
  <pre><code>Foo::Foo(const std::string&amp; str)
      : Foo(str.c_str())
  {
      //nothing
  }
  
  void* Foo::getData() {
      return data;
  }
  
  Foo::Foo(void* newData)
      : data(newData)
  {
      //nothing
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>int func(void* data);
  
  int main() {
      Foo f("bar");
  
      func(f.getData()); //segfault here
  
      return 0;
  }
  
  int func(void* data) {
      std::string str = (char*)data;
      std::cout &lt;&lt; str &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>