1

I want to know upto what index my array is filled. I know one method in which I will maintain a temporary variable inside the loop and will keep it updating which will at last determine the size.

I want to know that beside this method is their any other way to do this task? Better be O(1)(if possible) or anything better than O(n).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
chota bheem
  • 371
  • 3
  • 8
  • 20
  • I assume you mean "filled contiguously from the zeroth" index? You could initialize the array elements to a sentry value and then start at zero and count how many are filled. Why is keeping track in a temp variable not good enough? What are you actually trying to accomplish? Technically there are many, many, many ways to do it, though many are not efficient. – i_am_jorf May 02 '15 at 04:53
  • 1
    @i_am_jorf That will be a O(n) solution.Correct me if i am wrong – chota bheem May 02 '15 at 04:55
  • 1
    Yeah, so? You didn't specify how fast you want it. Your question is overly vague. – i_am_jorf May 02 '15 at 04:55
  • 1
    I should add that what you are suggesting is only possible with string arrays where they always have a null byte at the end to indicate the end of the string. For other arrays you need to make sure you keep track of the size or atleast keep track of the position to which it is filled, manually. This is your O(1) solution – smac89 May 02 '15 at 04:56

2 Answers2

2

There is no generic way to do that as all elements of an array always contain a value.

Couple common ways to handle that:

  • keep track of "valid" elements yourself as you suggested in the post.
  • have sentinel element that marks "missing" value and check each element for it - first element with such value will mark "end of filled array". For reference types you can use null, for other types sometimes there is specific value that rarely used and can be treated as "missing" - i.e. max value of integer types.

The second approach is the way C-style strings are implemented - it is array of characters up to 0 character - so you can always compute length of the string even if it is stored in longer array of chars.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I think for now i will choose the naive way that is first one and after getting good in fundamentals i will try to implement the second way myself.Thank you :) and sorry for my bad english – chota bheem May 02 '15 at 05:03
  • 2
    you might add this to answer : http://en.cppreference.com/w/cpp/container/array/size – Abhinav Gauniyal May 02 '15 at 05:13
0

will this do?

size_t size_of_array = sizeof(array)/sizeof(array[0]);

something like that , and do correct the syntax :)

hg_git
  • 2,884
  • 6
  • 24
  • 43
  • That tells you how big the array is, not how many elements have been "filled". – i_am_jorf May 02 '15 at 04:53
  • @hg_git I think it will tell the total size of array i.e empty+filled – chota bheem May 02 '15 at 04:54
  • arrays are represented with pointers, so when you do `sizeof(array)` you are getting the size of the pointer – smac89 May 02 '15 at 04:54
  • 1
    @chotabheem , oh okay. You mean till which index values are filled and not which are empty? – hg_git May 02 '15 at 04:55
  • 2
    @chotabheem , then sorry we can't do much. every array is filled with something ( either by you , or some garbage values). To track values you manually filled , you'll have to explicitly introduce another variable and increment it with each insertion. – hg_git May 02 '15 at 04:57
  • @Smac89, I think you're mistaken about how sizeof() works with arrays. That is a standard way of determining the size of an array. See: http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – i_am_jorf May 02 '15 at 05:02
  • @hg_git, what about [this](https://ideone.com/9Y0O09). The only reason your version worked is because you declared a static array, i.e. the size will never change so the compiler can infer that, but when it comes to dynamically allocated arrays which is almost always the case, `sizeof` will tell you nothing about the size – smac89 May 02 '15 at 05:04
  • 1
    @Smac89 correct , will fail on pointer types. use templates instead. – hg_git May 02 '15 at 05:06