How to get size of a dynamic allocated array of ints in C++?
Example:(/this will always give 4 as 4 * 8 = 32bits it's size of int)
bool tabSum(int* t, int& p, int& np){
cout<<"\n\n";
cout<<sizeof(*t)<<endl;
}
How to get size of a dynamic allocated array of ints in C++?
Example:(/this will always give 4 as 4 * 8 = 32bits it's size of int)
bool tabSum(int* t, int& p, int& np){
cout<<"\n\n";
cout<<sizeof(*t)<<endl;
}
You cannot do this. As already mentioned by other uses keep the length in an extra variable or maybe write your own malloc/realloc class that may be a little helper and track those for you.
It is not possible.
Every sensible function using a pointer as argument and expecting it to be an array will ALWAYS take a second parameter representing the number of elements.
Another approach, used in the STL, is to use a pointer one step past the last item in the array as the end boundary. STL Iterators abstract this approach with the use of the begin() and end() methods. This approach also eliminates the need for a separate numeric data type such as size_t.
A notable exception to this rule are string functions expecting NULL terminated strings, although that specific approach is prone to security issues and makes use of std::string classes the preferred approach.
You can't, arrays decay to pointers when passed as parameters. And
sizeof
is a compile-time operator.I suggest you use
std::vector
instead.