title: Change in string initialization induces UB
body: <p>Edit: Please refer to this abbreviated version of the question:</p>
  <hr />
  <p>Why does the definition</p>
  <pre><code>char word[2];
  word[0]='a';
  word[1]='b';
  </code></pre>
  <p>introduce UB?</p>
  <hr />
  <p>Summary: I have written code which renders the alphabetic portion of a string. When applied to a single string, it seems to work without issue. When used element-wise on a string vector using a for loop, however, it suffers from UB. There is an (apparent) workaround: define the output string using an initialization constructor, i.e.:</p>
  <pre><code>std::string output(2,'A');
  </code></pre>
  <p>instead of using the familiar:</p>
  <pre><code>char output[2];
  </code></pre>
  <p>Below is a (rather) simplified reproducible example wherein the outputs of two similar functions are compared. return_alpha uses the former initialization and return_alpha2 the latter. distribute_ra applies either return_alpha or return_alpha2 depending on modal input.</p>
  <pre><code>#include &lt;Rcpp.h&gt;
  using namespace Rcpp;
  
  std::string return_alpha(std::string s){
      int count=0;
      for (int i = 0; i &lt; s.length(); i++){
          if (std::isalpha(s[i])){
              count++;
          }
      }
      //char output[count];
      std::string output(count,'A');
      int idx=0;
      for (int i = 0; i &lt; s.length(); i++){
          if (std::isalpha(s[i])){
              output[idx]=s[i];
              idx++;
          }
      }
      return output;
  }
  
  std::string return_alpha2(std::string s, int mode){
      int count=0;
      for (int i = 0; i &lt; s.length(); i++){
          if (std::isalpha(s[i])){
              count++;
          }
      }
  
      char output[count+1];
      //std::string output(count,'A');
      int idx=0;
      for (int i = 0; i &lt; s.length(); i++){
          if (std::isalpha(s[i])){
              output[idx]=s[i];
              idx++;
          }
      }
      output[count]='\0';
      return output;
  }
  
  // [[Rcpp::export]]
  std::vector&lt;std::string&gt; distribute_ra(std::vector&lt;std::string&gt; s2,int s2len, int mode){
      std::vector&lt;std::string&gt; output(s2len);
      if (mode==0){
          for (int i = 0; i &lt; s2len; i++) {
              
              output[i] = return_alpha(s2[i]);
          }
      }else{
          for (int i = 0; i &lt; s2len; i++) {
              
              output[i] = return_alpha2(s2[i]);
          }
      }
      
      return output;
  }
  
  /*** R
  distribute_ra(c(&quot;bobby10&quot;,&quot;bob&quot;,&quot;bob&quot;),3,0)
  distribute_ra(c(&quot;bobby10&quot;,&quot;bob&quot;,&quot;bob&quot;),3,1)
  */
  </code></pre>
  <p>Evaluating the last bit yields:</p>
  <pre><code>&gt; Rcpp::sourceCpp('playground.cpp')
  
  &gt; distribute_ra(c(&quot;bobby10&quot;,&quot;bob1&quot;,&quot;bob&quot;),3,0)
  [1] &quot;bobby&quot; &quot;bob&quot;   &quot;bob&quot;  
  
  &gt; distribute_ra(c(&quot;bobby10&quot;,&quot;bob1&quot;,&quot;bob&quot;),3,1)
  [1] &quot;bobby&quot; &quot;bobb&quot; &quot;bobb&quot;
  </code></pre>
  <p>The expected output is</p>
  <pre><code>&gt; distribute_ra(c(&quot;bobby10&quot;,&quot;bob1&quot;,&quot;bob&quot;),3,0)
  [1] &quot;bobby&quot; &quot;bob&quot;   &quot;bob&quot;  
  </code></pre>