Pointer arithmetic in c and array bounds

I was browsing a webpage in which there were some answers to frequently asked questions, I found this expression.

Similarly, if a has 10 elements and ip points to [3], you cannot calculate either ip + 10 or ip - 5 access (there is one special case: in this case, you can calculate, but not get access, a pointer to a nonexistent element just outside the array, which in this case is & a [10].

I was embarrassed by the statement

you cannot calculate ip + 10

I understand that accessing an element outside the borders is undefined, but calculation !!!.

I wrote the following snippet that calculates (let me know if this is what the website means by calculation), the pointer is out of bounds.

#include <stdio.h> int main() { int a[10], i; int *p; for (i = 0; i<10; i++) a[i] = i; p = &a[3]; printf("p = %p and p+10 = %p\n", p, p+10); return 0; } $ ./a.out p = 0xbfa53bbc and p+10 = 0xbfa53be4 

We see that p + 10 points to 10 elements (40 bytes) per p. So what does the statement made on the web page mean. I misinterpreted something.

Even in K & R (A.7.7) this statement is made:

The result of the + operator is the sum of the operands. A pointer to an object in the array and the value of any integral type .... sum is a pointer of the same type as the original pointer, and points to another object in the same array, the object properly compensates for the original. Thus, if P is a pointer to an object in the array, the expression P + 1 is a pointer to the next object in the array. If the pointer points of the sum are outside the bounds of the array, except for the first place outside the high result, the result is undefined.

Which means "undefined". Does this mean that the amount will be undefined, or will it only mean when we act out its undefined behavior. Is the operation undefined even when we are not looking for it and just compute a pointer to an element outside the bounds.

+6
c pointers
source share
3 answers

Undefined behavior means exactly that: absolutely anything can happen. It could succeed quietly, it could fail, it could cause your program to crash, it could lead to a blue screen on your OS, or it could erase your hard drive. Some of them are unlikely, but they are all acceptable behavior with respect to the C language standard.

In this particular case, yes, the C standard says that even calculating the address of a pointer outside the valid boundaries of an array without dereferencing it is undefined behavior. The reason is that this suggests that there are some secret systems in which such a calculation can lead to some kind of error. For example, you may have an array at the very end of the address memory, and building a pointer outside it will overflow in a special address register that generates a trap or error. Standard C wants to allow this behavior to be as portable as possible.

In reality, however, you will find that building such an invalid address without dereferencing has well-defined behavior on the vast majority of systems that you will encounter in common use. Creating an invalid memory address will not have any side effects unless you try to dereference it. But, of course, it’s better to avoid creating these invalid addresses so that your code works perfectly even on those secret systems.

+9
source share

The text of the webpage is confused, but technically correct. The C99 language specification (section 6.5.6) discusses additive expressions, including pointer arithmetic. Subclause 8 states that computing a pointer beyond the end of an array should not lead to overflow, but beyond that, the behavior is undefined.

In a more practical sense, C compilers will usually let you get away from it, but what you do with the resulting value is up to you. If you try to play the resulting pointer to a value, as indicated by K & R, the behavior is undefined.

Undefined, in programming terms, means "Don't do this." Basically, this means that the specification defining how the language works does not determine the appropriate behavior in this situation. As a result, theoretically, anything can happen. As a rule, everything that happens is your segfault error in your program, but many programmers like to joke about other possible results due to undefined behavior, for example, deleting all your files.

+4
source share

The behavior will be undefined in the following case

 int a[3]; (a + 10) ; // this is UB too as you are computing &a[10] *(a+10) = 10; // Ewwww!!!! 
+2
source share

All Articles