C: Make sure the item returned from the array is correct

Let's say we have this piece of C code:

 int x[] = {1, 2, 3, 4, 5}; printf("%d", *(x + 1)); //prints 2 printf("%d", *(x + 500)); //prints 7209065 (...?) 

As you can see from the second call, it still returns something ... but it's garbage.

So, I ask, how do you handle this case in C? those. how do you know if the returned element is really an element that exists in the array or just garbage?

+1
c arrays
source share
11 answers

Add another variable that contains the length of the array, and whenever you want to access the array, make sure you are within the array.

+9
source share

Simply put, you cannot. This is just a function / bug / design of the C and C ++ language.

+4
source share

Array boundaries are not checked at runtime. These are just chunks of memory indicating the type inside. This is a language feature and it reduces the overall overhead of your code. Whether you like it or not, this is what it is.

+2
source share

As everyone noted, you cannot do this because of the nature of C. However, there are tools that you can use to manage your code and perform further memory access checks (for use during the development cycle). Purify is one such tool, and I have found it priceless to identify such problems.

+2
source share

In general, thatโ€™s why โ€œsmarterโ€ data types were created โ€” things like STL C ++ arrays that can throw exceptions for index errors outside of borders.

+1
source share

C does not check the bounds of the array. The responsibility for ensuring that access is within the limits is compared with the length of the array ( sizeof(x)/sizeof(x[0]) ).

+1
source share

In practice, either an array with a 0-terminating end, where you have 0 at the end of the array, is limited to sequentially alternating it, or you pass in a variable containing the length of the array around the array and check that all of your array has access.

An array in C does not know, in fact, how long this takes, and therefore you cannot get any behavior that relies on it for free.

0
source share

That's why in most (more reliable) C libraries that use arrays, you see the size of the array passed as a parameter, for example, fread () fwrite ()

0
source share

To clarify the answers of Jared Par and Ed:

C arrays are a block of contiguous memory. When you say int x [] = {1, 2, 3, 4, 5}; C allocates enough memory for 5 integers. You are requesting the 500th element, so that you get memory that was not properly allocated for X. This is garbage, and it can change every time you run it.

I agree with Rob. Add a variable with the number of elements in X and just do a for loop through each.

If you need arrays that change length at runtime, there are more efficient ways to do this.

0
source share

If you are "lucky" enough, or your index deviation is large enough, then you will not get garbage - you will get a segmentation fault . but itโ€™s really good, because it helps you understand that you are doing something wrong. some tools tell you the exact line in which this happened.

Sometimes it depends on the input of the program, which means that segfault only occurs when some input has been inserted (for example, if your value "500" was received as input from the user). (this is somewhat more annoying to find and process)

The most problematic cases are those in which you get garbage and don't know.

In these cases, there really is no substitute for writing good code. But, like others, Dynamic and Static analysis tools can help a lot.

0
source share

To a large extent, it is a mistake to think that C is of type "array". The syntax you use is sugar around pointers. x [y] is really syntactic sugar for * x + sizeof (something) * y (where "something" is harder than I want, something is int ).

Remember that C is a mid-level language ... a kind of "portable assembly" and you will not have unreasonable expectations on the table.

0
source share

All Articles