2

Consider an example:

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

How can I know the size of arr?

4
  • 21
    It says so right between the [ and ]. ;) Commented Nov 6, 2009 at 15:03
  • 7
    research.att.com/~bs/bs_faq2.html#void-main Commented Nov 6, 2009 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(). Commented Nov 6, 2009 at 16:56
  • 1
    See: stackoverflow.com/questions/197839/… Commented Nov 7, 2009 at 4:09

6 Answers 6

20

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

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

5 Comments

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.
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.
You simply can't. That's one of the problems with arrays in C and C++ -- they don't know their size.
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.
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();
}

1 Comment

@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.
0

Size of your array is 10.

Comments

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).

Comments

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.

2 Comments

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<>.
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.
-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.

2 Comments

Uh ... "size_of()"? Ints holding pionters? This is a very confusing answer.
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

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.