-2

This is the function from where I am returning the array pointer.

#include<bits/stdc++.h>
using namespace std;

int *mergeArray(int a[], int b[], int as, int bs){
    int* temp=new int[as+bs];
    int i{0}, j{0}, k{0};
    while(i<as&&j<bs){
        if(a[i]<b[j]) temp[k++]=a[i++];
        else temp[k++]=b[j++];
    }
    while(i<as) temp[k++]=a[i++];
    while(j<bs) temp[k++]=b[j++];
    
    return temp;
}

This is my main function. I am merging the sorted array by calling the mergeArray function. I want to find the length of the main array after merging. When I am trying to find the array size but I am getting 8 as output but the value should be 14. When I am using sizeof am getting size of int data type divide by first data of merged array i.e ((size of int data type)/arr[0]);

int main(){
    int arr1[]={3, 6, 8, 10, 15};
    int arr2[]={1, 5, 12};
    int arr3[]={4, 8, 15, 16};
    int arr4[]={2, 6};
    int s1=sizeof(arr1)/sizeof(arr1[0]);
    int s2=sizeof(arr2)/sizeof(arr2[0]);
    int s3=sizeof(arr3)/sizeof(arr3[0]);
    int s4=sizeof(arr4)/sizeof(arr4[0]);
    int range[min(s1,min(s2, min(s3, s4)))*4];
    int *arr= mergeArray(arr1, (mergeArray(arr2, (mergeArray(arr3, arr4, s3, s4)), s2, s3+s4)), s1, s2+s3+s4);
    cout<<sizeof(arr)/sizeof(arr[0])<<" ";
}
datenwolf
  • 159,371
  • 13
  • 185
  • 298
RAVI SHAH
  • 11
  • 3
  • 2
    Please don't abuse bold font and don't tag C++ questions with the C tag. – Evg Oct 16 '21 at 10:20
  • 1
    You can't. The pointer doesn't contain this information. But you know `s1+s2+s3+s4`. You could use a `std::vector` instead. Why do you even use C arrays in C++? – Thomas Sablik Oct 16 '21 at 10:24
  • 2
    Note: `sizeof(arr)/sizeof(arr[0])` is the size of a _pointer_ divided by the size of an `int`. `sizeof(arr1)/sizeof(arr1[0]);` is the size of an _array_ divided by the size of an `int`. Arrays are not pointers. Pointers are not arrays. – chux - Reinstate Monica Oct 16 '21 at 10:26
  • 2
    If you're trying to learn C++ from coding websites, don't. Prefer [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) written by professionals. – Evg Oct 16 '21 at 10:38

1 Answers1

1

If working with naked pointers, then you'll have to explicitly pass around the size of the objects they're pointing to.

However since you're using C++, the actual answer to your problem is to use std::vector instead of naked pointer array. Using std::vector your original code turns into

#include <vector>
#include <iostream>

template<class T>
std::vector<T> mergeArray(
     std::vector<T> const a,
     std::vector<T> const b )
{
    std::vector<T> r(a.size() + b.size());

    auto a_it = a.begin();
    auto b_it = b.begin();
    auto r_it = r.begin();

    while( a_it != a.end()
        && b_it != b.end()
        && r_it != r.end()
    ){
        *r_it++ = *a_it < *b_it ? *a_it++ : *b_it++;
    }

    while( a_it != a.end()
        && r_it != r.end()
    ){ *r_it++ = *a_it++; }


    while( b_it != b.end()
        && r_it != r.end()
    ){ *r_it++ = *b_it++; }

    return r;
}

int main(){
    std::vector<int> arr1={3, 6, 8, 10, 15};
    std::vector<int> arr2={1, 5, 12};
    std::vector<int> arr3={4, 8, 15, 16};
    std::vector<int> arr4={2, 6};
    auto arr = mergeArray(
        arr1,
        mergeArray(
            arr2,
            mergeArray(arr3, arr4)
        )
    );

    std::cout << arr.size() << std::endl;

    return 0;
}
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • `++a_it; ++b_it;` after dereferencing exactly one of those two iterators. – aschepler Oct 16 '21 at 10:42
  • If we use namespace std then we don't have to use std::. Y u are using this? Is there any specific reason? – RAVI SHAH Oct 16 '21 at 11:26
  • In the question, I have been restricted to use the array @aschepler – RAVI SHAH Oct 16 '21 at 11:42
  • 3
    @RAVISHAH [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Oct 16 '21 at 11:43
  • @RAVISHAH to add what Evg referred to: This is also the main reason why I myself rarely make use of namespaces and instead resort to prefixes; i.e. instead of `namespace foo { class Bar {}; }` I prefer `class fooBar {};`. Especially in projects which will also be used and added to by less experienced and novice programmers (I'm working at a university and students do contribute to our codebases): It enforces things to be explicit. – datenwolf Oct 16 '21 at 13:15