tags:

views:

294

answers:

6

Consider an example:

void main()
{
    int *arr;
    arr=new int[10];
}

How can I know the size of arr?

+17  A: 

You have to keep track of it yourself. I'd recommend making life easier on yourself by using a vector or deque instead.

Fred Larson
cn i knw vector....!!!I want to know how many elements are there.... i.e.,10!!By using code hw can i get...
What part of "You have to keep track of it yourself" is not clear? Please at least pretend to make an effort to spell fully. Most browsers check spelling as you go these days.
Sinan Ünür
By "You have to keep track of it yourself", Fred means: "you can't know the size of the array". The language gives you no support for that.
RaphaelSP
You simply can't. That's one of the problems with arrays in C and C++ -- they don't know their size.
Fred Larson
I agree with everyone. Also, OP, vowels don't cost extra! We're not on twitter or text messaging here. please type out your words so we can figure out what you're talking about.
Brian Postow
A: 

Edit for clarity: the first part is an explanation why the editor change his original code from

int arr;

to

int* arr;

~~~~~

When you declare

int arr;

are you making arr an int. It will then hold the pointer to the new array you just created. But the size of arr is still size_of(int).
~~~~~

I'm guessing that's not what you're asking. Are you asking how can you know the size of the array? You have to keep track of it yourself.

#define ARR_SIZE 10

void main()
{
  int * arr;
  arr = new int[ARR_SIZE];
}

But as Fred said, it would most likely be better to use something else that keeps track for you.

David Oneill
Uh ... "size_of()"? Ints holding pionters? This is a very confusing answer.
unwind
size_of(var) returns the size of the type of the variable that is passed in. In this case, (int arr;) arr is an int, which has the size size_of(int). This is the case no matter what number has been saved into arr
David Oneill
+5  A: 

Two ways (please note that in the first arr is a ptr not an int):

int main()
{
    const int SIZE = 10;
    int* arr;
    arr = new int[SIZE];

    delete[] arr;
}

or better yet:

int main()
{
     std::vector<int> arr( 10 );
     std::size_t size = arr.size();
}
Patrick
@Patrick - Technically the type of the "size" variable in your vector example should be "std::vector<int>::size_type", not "int". size_type is generally unsigned.
Void
+1  A: 

Size of your array is 10.

Alexey Malistov
A: 

In deed you can overload new and track allocation size with some static class method. Try to check tools such as LeakTracer.

Although LeakTracer is not so right implemented tool it provides almost all you need and even beyond this. You simple need to add static method to get allocation size by pointer (not so hard to implement it, just modify 'delete' handler).

Roman Nikitchenko
A: 

you can use sizeof and divide by the size of whatever the array's storing to get the number of items? something like that

Stroustrupp would tell you to use a container so that you don't get leaks or buffer overruns when accessing it.

queBurro
You can't get the size of an allocated chunk of memory in a portable way, so that trick doesn't work. It does work for `int arr[10]; size_t arr_size = sizeof(arr) / sizeof(arr[0]`. And more people than Stroustrup will say to use a vector<>.
David Thornley
That doesn't work for dynamic allocations. On most common platforms, sizeof(arr) will always be 4 (32 bit build) or 8 (64 bit build) regardless of number of array members.
R Samuel Klatchko
@DT - cheers, that was the trick I was thinking of.
queBurro