
Keith B. answered 09/22/19
Software Engineer and Math Geek
This question has no easy answer, as there isn't anything that tracks this information if you don't implement it yourself. A very common error (and easy to do!) is buffer overrun, where you go past the end of an array. Keep in mind that all an error is a section of contiguous memory set aside for you to use, and if you want to walk past the end, there's not much stopping you.
To implement, you can 1) Keep track externally 2) Embed a marker/flag value within.
To keep track externally, you need to keep a variable that has the current number of elements; this can be checked against how many elements you declared the array can hold as well.
Embedding a value within an array acts as a flag; for example using -1 in an array of integers, or '\0' in an array of characters (ie, a string!).