0

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • What is length of array in real code? – huseyin tugrul buyukisik Apr 30 '22 at 20:43
  • `memcpy` doesn't avoid looping through an array. It is implemented with a loop. Generally speaking, if you need to compare array elements, there's a loop somewhere.And it doesn't matter whether the data is on the heap or in the stack. Arrays are arrays. – Pete Becker Apr 30 '22 at 21:12
  • @PeteBecker I actually do not care how it is implemented. I just do not want to write a loop each time I have to deal with basic operations on arrays. If there are loops inside a built-in routine, let them be, at least I do not have to reinvent a wheel each time – Kaiyakha Apr 30 '22 at 22:46

1 Answers1

2

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

See also Compare fast two memory regions.

codeling
  • 11,056
  • 4
  • 42
  • 71