Search code examples
c++arrayscomparisonheap-memory

A shortcut to compare data on the heap in C++


Consider two arrays of data allocated on the heap:

int *A = new int[5];
int *B = new int[5];

// Some code which changes data pointed to by A and B

Now I want to compare these two arrays (not the pointers, but the data A and B point at). That is, I want to check out if the elements of the arrays are equal.

One might suggest elementwise comparison with a loop, but since there are functions such as memcpy which allow to avoid looping through data to copy it, it seems reasonable to assume there is a similar function which would allow to avoid looping through an array for elementwise comparison.

Does C++ have any shortcuts for this?


Solution

  • Since you're mentioning memcpy, there's also memcmp. This seems to be what you're after?

    See also Compare fast two memory regions.