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;
}
4
  • 2
    Isn't the size known at the time of allocation? Commented Aug 30, 2012 at 12:21
  • 1
    @FlopCoder dynamic allocation can be made at runtime as you already know and that's the case here i guess Commented Aug 30, 2012 at 12:22
  • 2
    Of course, but you have to provide the size to do that! Can I allocate an unspecified amount of memory? Commented Aug 30, 2012 at 12:23
  • About the edit: Please don't ask multiple questions in one post. And the problem is: you're referring to two locations of memory you may not be permitted to write on. Commented Aug 30, 2012 at 12:43

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@RobertKilar pass the size as parameter. This isn't uncommon.
Beside passing(it is obvious and unfortunately...)? sizeof will never work?
@RobertKilar with your code, no. Perhaps this might help - stackoverflow.com/questions/7966964/what-does-this-c-code-mean
@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.
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.

4 Comments

The second parameter could be the one-past-the-end pointer, that is even more sensible, in C++
yes indeed, that resembles the C++ iterator pattern more. Could you please provide an example ? For the moment, I have found cplusplus.com/reference/iostream/istream/getline that uses the pattern I mentionned.
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)
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.