3

I am took two arrays and then merged those two arrays to a newly created third array and it worked but when I output the size of the array, I was getting the size as '1'. I don't understand why the size of that array was '1' even though there are 5 elements in it.

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int arr1[] = { 1,2,3 };
    int arr2[] = { 9,4 };
    int size1 = sizeof(arr1) / sizeof(int);
    int size2 = sizeof(arr2) / sizeof(int);

    int *arr = new int[size1 + size2];

    //merging the two arrays by transferinng the elements into the third array
    for (int i = 0; i < size1; i++)
    {
        arr[i] = arr1[i];
    }
    for (int i = size1; i < (size1 + size2); i++)
    {
        arr[i] = arr2[i - size1];
    }

    //sorting the array
    sort(arr, arr + (size1 + size2));
    cout << endl;

    //finding the size of newly merged array
    int mergeSize = sizeof(arr) / sizeof(int);
    cout << "The size of the array is " << mergeSize << endl;   //why am I getting the size of the array as '1'
    return 0;
}
philip
  • 33
  • 4
  • `arr` is an `int` pointer, so `sizeof(arr)` gets you the size of an `int` pointer. If you want to know the size of the allocate memory, that's `size1 + size2` times the size of `int`. – Blaze Jul 12 '19 at 06:26
  • 1
    Possible duplicate of [Using sizeof with a dynamically allocated array](https://stackoverflow.com/questions/2731500/using-sizeof-with-a-dynamically-allocated-array) – Alan Birtles Jul 12 '19 at 06:30

2 Answers2

2

sizeof(arr) gives you the size of the pointer arr, which does not depend on the number of elements you allocated for it.

Avoid the problem by using std::array. It doesn't have the overhead of std::vector and it's easier to use than C-style arrays.

int main()
{
    array<int, 3> arr1 = { 1, 2, 3 };
    array<int, 2> arr2 = { 9, 4 };

    array<int, arr1.size() + arr2.size()> arr;

    //merging the two arrays by transferinng the elements into the third array
    for (int i = 0; i < arr1.size(); i++)
    {
        arr[i] = arr1[i];
    }
    for (int i = 0; i < arr2.size(); i++)
    {
        arr[i + arr1.size()] = arr2[i];
    }

    //sorting the array
    sort(arr.begin(), arr .end());
    cout << endl;

    //finding the size of newly merged array
    int mergeSize = arr.size();
    cout << "The size of the array is " << mergeSize << endl;   //why am I getting the size of the array as '1'
    return 0;
}
Sid S
  • 6,037
  • 2
  • 18
  • 24
  • I haven't tested this directly; but I'm not so sure that this line of code: `array arr;` will compile. When instantiating an instance of `std::array` and setting its second template argument; doesn't it require the fact that the second parameter must be both constant and known at compile time as opposed to run time? – Francis Cugler Jul 12 '19 at 07:22
  • 1
    @FrancisCugler, the sizes _are_ constant and known at compile time. Why wouldn't they be, when the `array` template requires a constant and known at compile time size parameter ? – Sid S Jul 12 '19 at 08:00
  • I wasn't 100% sure; and I haven't tested it... You may be right, that's why I asked just for some clarity! – Francis Cugler Jul 12 '19 at 08:02
  • 1
    I was able to test it; and yes it does consider: `std::array.size()` to be known at compile time and constant. – Francis Cugler Jul 12 '19 at 08:07
1

arr is not an array, it's a pointer, Using sizeof on a pointer gives the size of the pointer not the size of the dynamic array it's pointing at. The sizeof a pointer is usually 4 or 8, depending on whether you have a 32 bit or 64 bit system.

You can avoid these problems by using vectors instead of arrays. Vectors have a size method which always gives the actual size. Arrays are quite poor in C++.

john
  • 85,011
  • 4
  • 57
  • 81
  • Why should he use a `vector` ? What's wrong with `std::array` ? – Sid S Jul 12 '19 at 06:32
  • `std::array` is fine, although it wouldn't work for the task above. – john Jul 12 '19 at 06:33
  • Why wouldn't it ? – Sid S Jul 12 '19 at 06:33
  • Because `std:::array arr;` does not compile. And reworking the code so that it does compile is too much for me to be bothered with. But if you want to give an answer that shows how to use `std::array` for this task then be my guest. – john Jul 12 '19 at 06:35