0

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;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
Yoda
  • 17,363
  • 67
  • 204
  • 344

3 Answers3

7

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • If you know hwo to get a size of an array passed to function(maybe in other way, please tell) i do not want to use vector or any other collection. – Yoda Aug 30 '12 at 12:24
  • 1
    @RobertKilar pass the size as parameter. This isn't uncommon. – Luchian Grigore Aug 30 '12 at 12:25
  • Beside passing(it is obvious and unfortunately...)? sizeof will never work? – Yoda Aug 30 '12 at 12:26
  • @RobertKilar with your code, no. Perhaps this might help - http://stackoverflow.com/questions/7966964/what-does-this-c-code-mean – Luchian Grigore Aug 30 '12 at 12:27
  • @RobertKilar there is just no way to know the size of a dynamically allocated array, unless you keep track of it from the point where you allocated it. You can only do it for static sized arrays. – juanchopanza Aug 30 '12 at 12:28
4

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.

SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • The second parameter could be the one-past-the-end pointer, that is even more sensible, in C++ – Cubbi Aug 30 '12 at 13:15
  • yes indeed, that resembles the C++ iterator pattern more. Could you please provide an example ? For the moment, I have found http://www.cplusplus.com/reference/iostream/istream/getline/ that uses the pattern I mentionned. – SirDarius Aug 30 '12 at 13:22
  • if you're looking for the standard library examples, almost everything that can work with an array is already templated to take any two random access iterators (including two pointers) – Cubbi Aug 30 '12 at 13:58
  • yeah, when taking templates into account, it becomes obvious :) thanks ! Also, another advantage to this approach is that it eliminates the need for a separate numeric data type such as size_t. – SirDarius Aug 30 '12 at 14:22
0

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.

Stefan
  • 2,603
  • 2
  • 33
  • 62