title: Preventing Core Dump from initializing string with null pointer
body: <p>In my code, I have a constructor that initializes <code>std::string</code> variables from a structure containing <code>char *</code> pointers.  I'm getting Aborts with Visual Studio 2008 in the constructor, if a structure field is null.  </p>
  
  <pre><code>class Ingredient
  {
    public:
      struct Static_Table
      {
         char const * const title;
         char const * const category;
      };
      Ingredient(const Static_Table&amp; entry)
         : m_title(entry.title),
           m_category(entry.category)
      { ; }
    private:
      std::string m_title;
      std::string m_variety;
  };
  </code></pre>
  
  <p>If I have an instance of the <code>Static_Table</code> where a member is NULL, the <code>Ingredient</code> constructor <em>blows up</em>:  </p>
  
  <pre><code>Ingredient::Static_Table test_data =
  {
    /* Title */ "Pepperoni",
    /* Category */ 0, // Oops, forgot to put a string literal here.
  };
  Ingredient pepperoni(test_data); // This will cause Abort / Stack Dump
  </code></pre>
  
  <p>The basis of the issue is that behavior is undefined when the <code>char</code> pointer is null <em>(from <a href="http://en.cppreference.com/w/cpp/string/basic_string/basic_string" rel="nofollow">basic_string definition on CppReference.com</a></em>:  </p>
  
  <blockquote>
    <p>Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s. The length of the string is determined by the first null character. The behavior is undefined if s does not point at an array of at least Traits::length(s)+1 elements of CharT.</p>
  </blockquote>
  
  <p>This can be verified by the simple example:  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  
  int main(void)
  {
      char const * const pointer = NULL;
      std::string test_string(pointer);
      std::cout &lt;&lt; "Pointer: " &lt;&lt; pointer &lt;&lt; "\n";
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <h2>My Questions:</h2>
  
  <ol>
  <li>Is there any method to prevent the abort / crash when using
  <em>initialization list</em> for the constructor?</li>
  <li>Why can't <code>std::string</code> simply construct empty as if no parameters
  were given?</li>
  </ol>
  
  <h2>Tool Information:</h2>
  
  <p>Using Visual Studio 2008 on Windows Vista.<br>
  Simple example compiled with G++ 3.4.4 using Cygwin.  </p>