2

Consider an example:

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

How can I know the size of arr?

Marcin
  • 12,245
  • 9
  • 42
  • 49
user204930
  • 47
  • 1
  • 4
  • 21
    It says so right between the `[` and `]`. ;) – jalf Nov 06 '09 at 15:03
  • 7
    http://www.research.att.com/~bs/bs_faq2.html#void-main – Sinan Ünür Nov 06 '09 at 15:03
  • 2
    To amplify on what Sinan said, lose the `void main()`. It's nonstandard and serves no useful purpose, other than marking people who use it as newbies or worse. If you got it from a book, that book is untrustworthy, so you should get another book (at least for reference). Good books on C and C++ use `int main()`. – David Thornley Nov 06 '09 at 16:56
  • 1
    See: http://stackoverflow.com/questions/197839/is-there-any-way-to-determine-the-size-of-a-c-array-programmatically-and-if-n – Shog9 Nov 07 '09 at 04:09

6 Answers6

20

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
  • 60,987
  • 18
  • 112
  • 174
  • cn i knw vector....!!! I want to know how many elements are there.... i.e.,10!! By using code hw can i get... – user204930 Nov 06 '09 at 15:05
  • 7
    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 Nov 06 '09 at 15:06
  • 2
    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. – Raphaël Saint-Pierre Nov 06 '09 at 15:07
  • 3
    You simply can't. That's one of the problems with arrays in C and C++ -- they don't know their size. – Fred Larson Nov 06 '09 at 15:08
  • 3
    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 Nov 06 '09 at 15:44
7

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
  • 8,175
  • 7
  • 56
  • 72
  • 1
    @Patrick - Technically the type of the "size" variable in your vector example should be "std::vector::size_type", not "int". size_type is generally unsigned. – Void - Othman Nov 06 '09 at 18:50
0

Size of your array is 10.

Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
0

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
  • 12,800
  • 7
  • 74
  • 110
0

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.

timB33
  • 1,977
  • 16
  • 33
  • 2
    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 Nov 06 '09 at 16:53
  • 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 Nov 06 '09 at 16:56
-4

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
  • 12,502
  • 16
  • 58
  • 70
  • Uh ... "size_of()"? Ints holding pionters? This is a very confusing answer. – unwind Nov 06 '09 at 15:07
  • 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 Nov 06 '09 at 15:09