-1

I'm exploring a c++ library and cant figure out why its examples are written as they are. In the example they do the following

 std::vector<unsigned int> vec(count);
 someFunc(&vec[0]);  

while the fucion is define as

void someFunc(unsigned int* a);

why are the examples passing a reference to the first element of the vector and not the whole vector. Inst this code the same ?

 someFunc(&vec);  

Edit: I should have provided more contex. I simplified the case, but this has proven a bad descision. The function is supposed to simplify a mesh, the vec is a list of all the indicies of the mesh. Presumably somewhere in the code that pointer is used to iterate over the rest of the indicies ?

Uri Popov
  • 2,127
  • 2
  • 25
  • 43
  • 8
    `&vec[0]` is a pointer to the first element. `&vec` is a pointer to a `std::vector`. Those are two completely different types of pointers. There are no references here – 463035818_is_not_an_ai Sep 07 '22 at 11:56
  • You may want to consult one of these https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Sep 07 '22 at 11:57
  • why the function takes a pointer to a single element rather than a reference to the whole vector is not clear from the provided context. – 463035818_is_not_an_ai Sep 07 '22 at 11:58
  • Presumably `someFunc` accepts a pointer to a single `unsigned int` so that it can potentially modify it. That being the case why pass a reference/pointer to the whole vector? – G.M. Sep 07 '22 at 11:58
  • 1
    The vector is not passed by reference. The ampersand, "&", has three different meanings. In a type, it means "reference"; as a unary operator, it means "address of" (this is the case here); as a binary operator, it means "bitwise and". – molbdnilo Sep 07 '22 at 11:59
  • 1
    `someFunc(vec.data())` might be clearer. – Jarod42 Sep 07 '22 at 12:00
  • 1
    to iterate the vector the function would also need the size. A pointer does not carry information on the size of the array – 463035818_is_not_an_ai Sep 07 '22 at 12:01
  • Like an array a pointer to the first element of a vector is all you need to point to the entire array that the vector abstracts. You do need a size parameter to know where the end of the array is. – NathanOliver Sep 07 '22 at 12:01

2 Answers2

0

The reason they have written their example like they have is that someFunc() takes in a unsigned int* type (a pointer to an unsigned int). The reason they call it as:

someFunc(&vec[0]);

and not

someFunc(&vec);

is because a std::vector is not like a C-style array (say unsigned int arr[]) where arr can be boiled down a unsigned int* to the first element in the array but is rather a class/struct that acts as a resource handle to some dynamic memory they you access std::vector interface. Thus, like any class in C++, calling foo(&A) (where A is some class instance/object) would return a pointer to the object itself [1].

In their example they have accessed the first element of the vec like a C-style array (using the overloaded operator[]) which is a reference. They then use the C-style unary-prefix ampersand operator (with the signature &A) to access the address of the value obtained through the index operator.

There example might be clearer if they used std::vector::data() which returns a pointer to the starting address of the memory owned by the vector or using std::addressof() (although the latter can be verbose).

someFunc(vec.data());

/// or ...

someFunc(std::addressof(vec[0]));

[1] Note: This may not be the case if the class/struct in question has overloaded the & operator to behave differently. std::vector does not in this case.

Links

oraqlle
  • 186
  • 2
  • 12
  • *"calling `foo(&A)` (where A is some class instance/object) would return a pointer to the object itself."* - unless said class overrides the address-of operator :P – UnholySheep Sep 07 '22 at 15:44
  • @UnholySheep indeed :). I'll make another note – oraqlle Sep 07 '22 at 16:09
0

It is likely that the library function someFunc was written with a C-style array as an argument, i.e. you just pass a pointer to the first element (not a reference). As the pointer is typed, the addresses of the next elements can be computed by the function. But their number must be known elsehow.