0
votes
#include <iostream>
using namespace std;

int main() 
{
   int *array1 = new int [5]();
   int *array2 = new int [7]();

   array1[2] = 3;// or anychange
   array2[2] = 3;// to both arrays

   if (array1==array2)
   {
    //if all values of the both arrays are equal
   }
   else
   {
    //if all values of the both arrays are not equal
   }
   return 0;
}

I have two dynamically allocated array using new (the size may or may not be same). Now I want to compare all elements of array (if size and elements are same, then true, if not either of these then false).

How to do in C++? (not interested using vector in my problem scenario)

4
That is impossible - you lost the size information of each array (although the compiler knows it at new[]/delete[] ).user2249683
It would be better to use vectors in this case. In your scenario, you need to store the array lenghts. Then you can iterate through arrays(e.g. with for loop). If you find an inequality, then put the iterator to lenght to finish loop(or use break), and a predeclared bool to false. After this check the bool.Lasoloz

4 Answers

0
votes

First off, I would like to encourage you to use std::vector for dynamically allocated arrays. They will free the allocated memory safely and automatically and you can always retrieve their size without extra manual book-keeping.

Once you have that, you can compare the two arrays in the following way:

#include <vector>

int main()
{
    std::vector<int> v1 = { 1, 2, 3 };
    std::vector<int> v2 = { 1, 2, 3, 4 };
    const bool theyAreEqual = v1 == v2;
}

Comparing two pointers as you did, only compares the addresses of the first elements and not the sizes and the contents of the dynamic arrays elementwise. That's one of the reasons, that it's much safer to use std::vector instead of C-style arrays.

0
votes

array1 == array2 compares pointers. They will never be equal. Furthermore, you can't know how many elements is in a dynamically allocated array, unless you're:

  1. having its size stored separately
  2. using sentinel value to determine its end - you choose a value (e.g. -1) to represent end of the array (like c-style strings usually use \0)

Then you'll be able to know how many elements to iterate over, comparing the elements of both arrays.

0
votes

Here is a way to resolve it, but I highly recommend vectors, in cases like this.

You need the length and a bool for checking. check is true for default and arrays should be allocated with length1 and length2.

//...
if (length1 != length2) check = false;
else for (int i = 0; i < length1; i++)
{
    if (array1[i] != array2[i])
    {
        check = false;
        break;
    }
}

if (check)
//...
0
votes

I followed up on Ralph comment, because I also wanted to see what std::equal did, and the == operator of std::vector does the right thing, and surprisingly simpler to use than the std::equal operator. If you use the latter, you will need to make sure to user begin()/end() for both arrays (It is a C++14 version of std::equal), or add v1.size() == v2.size() &&...

#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> v1 = { 1, 2, 3 };
    std::vector<int> v2 = { 1, 2, 3, 4 };
    std::vector<int> v3 = { 1, 2, 3 };
    const bool theyAreEqualv1v2 = v1 == v2;
    const bool theyAreEqualv1v3 = v1 == v3;

    const bool theyAreEqualStdv1v2 = std::equal(v1.begin(),v1.end(), v2.begin(),v2.end());
    const bool theyAreEqualStdv1v2bad = std::equal(v1.begin(),v1.end(), v2.begin());
    const bool theyAreEqualStdv1v3 = std::equal(v1.begin(),v1.end(), v3.begin(),v3.end());
    // std::equal according to http://en.cppreference.com/w/cpp/algorithm/equal actually
    // only compares the first range thus you would really need     begin()/end() for both arrays

    printf("equal v1v2: %d\n",theyAreEqualv1v2);
    printf("equal v1v3: %d\n",theyAreEqualv1v3);
    printf("std::equal v1v2: %d\n",theyAreEqualStdv1v2);
    printf("std::equal v1v2 bad: %d\n",theyAreEqualStdv1v2bad);
    printf("std::equal v1v3: %d\n",theyAreEqualStdv1v3);
    return 0;
}

clang++ -std=c++14 -stdlib=libc++  c.cpp 
output:
equal v1v2: 0
equal v1v3: 1
std::equal v1v2: 0
std::equal v1v2 bad: 1
std::equal v1v3: 1