0

I am learning about pointers, and how they can be used to send array(size is not assigned) to objects. Ultimately i m trying to learn how arrays can be added and multipled using operator overloading instead of using "vector" in c++.

I defined a class named vector as follows,

class vector
{
private:
    int len;
    int *pvec;
}
public:
    vector(int array[])
    {
        len = sizeof(array)/sizeof(*array);
        cout<<len<<endl;
        pvec = new int[len+1];
        for(int i = 0;i<len;i++)
         {
            pvec[i] = *(array + i);
         }
    }
    void display()
    {
        cout<<"[";
        for(int i = 0;i<len;i++)
        {
            cout<<pvec[i];
            if(i < len-1)
            {
                cout<<",";
            }
        }
        cout<<endl;
    }
};

and i declared an object of class vector as follows

int vec1[] = {1,2,3,4};
vector v(vec1);

when i try to display the elements in the vector using the member function as

v.display();

the output is as follows

2
[1,2]

but as you can see the length of the vector is 4, but in the output it is displayed as 2. If i try to display the length of the array in main as

cout<<sizeof(vec1)/sizeof(*vec1)<<endl;

the length is displayed as 4, but if i pass the same array to the object it is displayed as 2.

If any one could point out a mistake in this program, i would be grateful.

the_parzival
  • 354
  • 3
  • 19
  • 2
    Please use search before asking – Ivan Aksamentov - Drop Apr 25 '16 at 16:36
  • That was not my question, i knew that "sizeof" cannot be used for pointers. I came to know that the arrays are passed as pointers to objects. – the_parzival Apr 25 '16 at 16:40
  • `arrays are passed as pointers` Not really. It's more complicated. Read the answers in deduplicated thread, not its header, it's all explained there. There are many many more duplicate questions and answers about it. Bonus: try to search for "array to pointer decay". Enjoy. – Ivan Aksamentov - Drop Apr 25 '16 at 16:43

2 Answers2

2

You cannot compute the size of an array once it is passed to a function as a pointer.

However, you can use couple of overloads of the constructor to help you with what you need.

In the first one, expect a pointer to an array and a size.

vector(int array[], int size) : len(size)
{
   cout<<len<<endl;
   pvec = new int[len+1];
   for(int i = 0;i<len;i++)
   {
      pvec[i] = *(array + i);
   }
}

In the second one, use a template with the size of the array as the template parameter. Make it a delegating constructor that calls the first one to do the work.

template <int N> vector(int (&array)[N]) : vector(array, N) {}

With this you can use:

// Use the first constructor
int* array1 = new int[20];
vector v1(array1, 20);

// Use the second constructor
int array2[10] = {0};
vector v2(array2);

See it working at http://ideone.com/RJN1gd.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

There's no way, you should pass the length as an additional function's parameter.

Passing an array as a parameter is almost like passing a pointer to its first element, you loose every information about its size.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • So you are saying that the array is passed as a pointer to the object. I read that the 'sizeof' doesn't work with pointers. – the_parzival Apr 25 '16 at 16:36
  • sizeof() returns the size, in bytes, of the variable type used as the argument. – Stephen Apr 25 '16 at 16:37
  • @Kalyan Well, `sizeof()` works well with pointers, but yields the size of the pointer variable, not the _underlying array_. – πάντα ῥεῖ Apr 25 '16 at 16:38
  • Yeah exactly, when we use it to know the size of array by `int len = sizeof(array)/sizeof(*array);` the total size of array is 16 and when divided by 4 gives 4. – the_parzival Apr 25 '16 at 16:42
  • @Kalyan Arrays decay to pointers when passed. If you want to maintain peripheral info, consider using `std::vector` or `std::array`. – erip Apr 25 '16 at 16:45