1

arr1,arr2 are two arrays of pointers, I have to merge these into arr3. When the program gets arr3[1] (when k=1) the program is closes , I don't get why.

Please help.

class Node {
public:
    Node* left;
    T data;
    Node* right;
    int height;
};
    void mergeArrays(Node<T>** arr1,Node<T>** arr2,int len1,int len2){
        int p=0,q=0,k=0;
        Node<T>** arr3 = new Node<T>*[len1+len2];
        
        while ( p < len1 && q < len2) {
                if ((arr1[p])->data< (arr2[q])->data) {
                    (arr3[k++])->data = (arr1[p++])->data;
    
                } else {
                    
                    (arr3[k++])->data = (arr2[q++])->data;    
                }  
            
            }
       
            while ( p < len1) {
                (arr3[k++])->data = (arr1[p++])->data;
            }
            while ( q < len2) {
         
                (arr3[k++])->data = (arr2[q++])->data; 
            }
    
            
            
    }
3
  • 1
    You're not copying pointers; you are copying data from dereferenced members to other dereferenced members of objects that don't exist. And I have no idea what you hoped to accomplish with cout << arr3[c] << " ";, since all that does is drop a pointer into cout unless you have some secret operator overload buried the the code you're not showing us (but should be, as part of a proper minimal reproducible example). Commented Apr 30, 2022 at 8:00
  • 1
    Just to add to @WhozCraig's comment, if you intend to implement a shallow-copy, please copy the pointers instead of copying the data data-member. Otherwise, if you intend to implement a deep-copy, then do a separate heap-allocation for each element of arr3 using new using a suitable constructor that takes data as an argument. Commented Apr 30, 2022 at 8:07
  • 2
    What about using std::merge? It is quite flexible, less bug-prone, easier to maintain and possibly faster (not to mention there is now a parallel implementation available). Commented Apr 30, 2022 at 10:27

1 Answer 1

1

You're not copying pointers. You're copying values from one data member of some object to another data member of some object. But the problem is, there is no "there" there to copy to . You allocate a bed of pointers for arr3, but there are no objects behind those pointers so this:

(arr3[k++])->data = (arr1[p++])->data;

Invokes undefined behavior.

I'm fairly certain this is what you were shooting for:

template<class T>
Node<T>** mergeArrays(Node<T> **arr1, Node<T> **arr2, int len1, int len2)
{
    int p = 0, q = 0, k = 0;
    Node<T> **arr3 = new Node<T> *[len1 + len2];

    while (p < len1 && q < len2)
    {
        if ((arr1[p])->data < (arr2[q])->data)
        {
            arr3[k++] = arr1[p++];
        }
        else
        {
            arr3[k++] = arr2[q++];
        }
    }

    while (p < len1)
    {
        arr3[k++] = arr1[p++];
    }
    while (q < len2)
    {
        arr3[k++] = arr2[q++];
    }

    return arr3;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.