title: Swap arrays of different sizes
body: <p>I needed to swap the contents of two <code>std::string</code> arrays of different sizes. Using <code>std::vector&lt;std::string&gt;</code> to use the <code>swap</code> vector method was the answer for other questions like this one, but using a vector was undesirable in my situation, since the swap is the only moment the arrays might have had their lengths changed - this function would only be called once, at initialization, other than that they'd be (very big) fixed length arrays. At the end I re-structured my code to avoid the swap in the first place, but I'm curious if it's feasible. I tried the following, where <code>n</code> is the length of <code>a</code> and <code>m</code> the length of <code>b</code>, but it segfaults.</p>
  
  <pre><code>void swap_strarr(std::string *a, std::string *b, int n, int m) {
      int i;
      std::string *tmp;
      tmp = new std::string[n];
      for (i = 0; i &lt; n; i++) {
          tmp[i] = a[i];
      }
      delete []a;
      a = new std::string[m];
      for (i = 0; i &lt; m; i++) {
          a[i] = b[i];
      }
      delete []b;
      b = new std::string[n];
      for (i = 0; i &lt; n; i++) {
          b[i] = tmp[i];
      }
      delete []tmp;
  }
  </code></pre>
  
  <p>I first tried without the <code>delete</code>s on <code>a</code> and <code>b</code>. I also tried using <code>std::string**</code>, which shouldn't even work - and it didn't.</p>
  
  <p>So... is it possible? Or, had I done the swap, should I just use <code>std::vector</code> even though they're fixed size arrays (apart from the swap) and a few million elements long?</p>