0

Is there a way to take two pieces of heap allocated memory, and put them together efficiently? Would this be more efficient than the following?

for( unsigned int i = 0; i < LENGTH_0; ++i )
    AddToArray( buffer, array0[ i ] );
for( unsigned int i = 0; i < LENGTH_1; ++i )
    AddToArray( buffer, array1[ i ] );
  • What are your comparing? – R Sahu Jul 01 '14 at 18:09
  • 2
    C++ have many nice [algorithms](http://en.cppreference.com/w/cpp/algorithm), for example to [copy data](http://en.cppreference.com/w/cpp/algorithm/copy). With a [standard container](http://en.cppreference.com/w/cpp/container/vector) it's very easy to [append at the end](http://en.cppreference.com/w/cpp/iterator/back_inserter). – Some programmer dude Jul 01 '14 at 18:13
  • 4
    `realloc` and then a `memcpy`, perhaps, but that is really for C – Ben Jul 01 '14 at 18:14
  • 2
    This would be so much easier and safer with [a container class](http://en.cppreference.com/w/cpp/container) instead of an array. – Fred Larson Jul 01 '14 at 18:17

2 Answers2

2

For copying memory byte by byte, you can't go wrong with memcpy. That's going to be the fastest way to move memory.

Note that there are several caveats, however. For one, you have to manually ensure that your destination memory is big enough. You have to manually compute sizes of objects (with the sizeof operator). It won't work well with some objects (shared_ptr comes to mind). And it's pretty gross looking in the middle of some otherwise elegant C++11.

Your way works too and should be nearly as fast.

You should strongly consider C++'s copy algorithm (or one of its siblings), and use vectors to resize on the fly. You get to use iterators, which are much nicer. Again, it should be nearly as fast as memcpy, with the added benefit that it is far, far safer than moving bytes around: shared_ptr and its ilk will work as expected.

George Hilliard
  • 15,402
  • 9
  • 58
  • 96
  • 2
    "It won't work well with some objects" -- That's an understatement. It will cause undefined behavior with non-[POD](http://stackoverflow.com/q/146452/10077) class types. – Fred Larson Jul 01 '14 at 18:42
  • It is *technically* correct. ;P But you're right; many data structures will break. – George Hilliard Jul 01 '14 at 18:51
1

I'd do something like this until proven to slow:

vector<decltype(*array0)> vec;
vec.reserve(LENGTH_0 + LENGTH_1);
vec.insert(vec.end(),array0,array0 + LENGTH_0);
vec.insert(vec.end(),array1,array1 + LENGTH_1);

Depending on the data stored in array1 and array0 that might be as fast or even faster than calling a function for every single data.

TNA
  • 2,595
  • 1
  • 14
  • 19