0

I create an array of size int arr[50]; but I will insert value in it during compile time , like my solution will insert 10 values in it after performing some function (different amount of values can come) , Now in second part of my program I have to loop through the array like it should iterate <= total values of array like in int arr[50] my program save 10 values , it should iterate to it only 10 times but how I can get that there is only 10 values in that array.

arr[50]=sum;
for (int ut=0; ut<=arr[100].length();ut++)

Though i know ut<=arr[100].length() is wrong , but its just assumption , that function will work if I solve condition in this way.

Edit:

I know we can use vector , but I am just looking that type of thing using array.

Thanks for response

8
  • 6
    Use a vector. To avoid the overhead of resizing on push_back, you can call reserve on it first. Commented Jul 25, 2014 at 17:20
  • No, there's no way to check how many elements of the array have been initialized. Commented Jul 25, 2014 at 17:21
  • so we can only left with the option of vector ? Commented Jul 25, 2014 at 17:22
  • 1
    @Munieb The best option is a vector :D Commented Jul 25, 2014 at 17:22
  • 1
    I know we can use vector , but I am just looking that type of thing using array. So why the allergic reaction to using std::vector? Commented Jul 25, 2014 at 17:31

1 Answer 1

2

First of all, the array you show is not a "Dynamic Array". It's created on the stack; it's an automatic variable.

For your particular example, you could do something like this:

int arr[50];
// ... some code
int elem_count = sizeof(arr) / sizeof(arr[0]);

In that case, the sizeof(arr) part will return the total size of the array in bytes, and sizeof(arr[0]) would return the size of a single element in bytes.

However, C-style arrays come with their share of problems. I'm not saying never use them, but keep in mind that, for example, they adjust to pointers when passed as function arguments, and the sizeof solution above will give you an answer other than the one you are looking for, because it would return sizeof(int*).

As for actual dynamically allocated arrays (where all what you have is the pointer to that array), declared as follows:

int *arr = new int[50];
// ... do some stuff
delete [] arr;

then sizeof(arr) will also give you the size of an int* in bytes, which is not the size you are looking for.

So, as the comments suggested, if you are looking for a convenient random access container where you want to conveniently and cheaply keep track of the size, use a std::vector, or even a std::array.

UPDATE

To use a std::array to produce equivalent code to that in your question:

std::array<int, 50> arr;

and then use it like a normal array. Keep in mind that doing something like arr[100] will not do any bounds checking, but at least you can obtain the array's size with arr.size().

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

2 Comments

how to use std::array in this case ? can you just give a clue ?
I am just reading it arr.size() will return the size of array if arr size of 50 contain only 10 element in it ? yea ?

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.