-1

I want to get the size of an integer array in C++. If a method takes in an int* how do I get the size of that value?

int myMethod(int* values)
{
     values.size();

}

this give compilation errors

Robert
  • 4,168
  • 10
  • 44
  • 88

1 Answers1

5

You could get the size of of a pointer to an integer with sizeof(int*).

However, given a pointer value, there is no portable way (in C or C++) to get the dynamic -runtime- size of the pointed memory zone (in heap or elsewhere).

A general advice is to avoid raw pointers when possible: use smart pointers (from <memory> header) and standard C++ containers (e.g. std::vector from <vector>)

So if you declared std::vector<int> values; you could get the vector size with values.size();

Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509