title: C++ Build an array of data by repeating a vector many times
body: <p>I have a sequence of numbers that I need to repeat many times and get the repeated data as a pointer to the data along the size (actually element count). I need that data to pass it on to an API.</p>
  
  <p>What would be the most efficient way to allocate the memory, pass on the repeated data and then free it again. I currently have the sequence that needs to be repeated stored in a std::vector</p>
  
  <p>A couple of thoughts I had were along the lines of:</p>
  
  <pre><code>// Setup code
  unsigned int repeat = 30000;
  std::vector&lt;int&gt; datavector(5, 0); // assume this would contain arbitrary numbers that needed to be repeated
  
  // Idea 1:
  {
      unsigned int byte_size_step = datavector.size() * sizeof(int);
      unsigned int byte_full_size = byte_size_step * repeat;
      int *ptr = malloc(byte_full_size);
  
      for(unsigned int i=0; i&lt;repeat; i++)
      {
          memcpy(ptr+(i*byte_size_step), datavector.data(),  byte_size_step);
      }
      apiFunc(ptr); // apiFunc copies the data
      free(ptr)
  }
  
  // Idea 2:
  {
      std::vector datarepeated(datavector.size()*repeat);
      for(unsigned int i=0; i&lt;repeat; i++)
      {
          datarepeated.insert(datarepeated.begin()+(i*size_step), datavector.begin(), datavector.end());
      }
      apiFunc(datarepeated.data());
  }
  </code></pre>
  
  <p>Though I felt that there would be a function or easy-to-go method laying around to quickly repeat the sequence in memory. I'm probably missing something. I personally don't know if something like this could benefit from a multithreaded solution.</p>
  
  <p>Any tips to do this (most) efficiently are welcome.</p>