Access element beyond the end of the array of C

I have been reading K&R’s book on C and found that the pointer algorithm in C allows access to an element beyond the end of the array. I know that C allows memory to do almost anything, but I just Do not understand, what is the purpose of this feature?
C does not allow access to memory beyond the end of the array. However, it allows the pointer to point to an element beyond the end of the array. Very important.

This will do:

char array[N];
char *p;
char *end;

for (p = array, end = array + N; p do_something(p);

(do *The end will be an error.)

This shows why this feature is useful: pointers to (non-existent) elements after the end of the array are very useful for comparisons (e.g. loops).

Technically speaking, this is everything that the C standard allows. However, in reality, the C implementation (compiler and runtime) does not check whether the memory beyond the end of the array is accessed, whether it is one element or more There must be a boundary check, which will slow down the execution speed of the program. The speed of the C type of program that is most suitable for (system programming, general library) is often more than that brought by security and security boundary checks.

This means that C may not be a good tool for general application programming.

I have been reading K&R’s book on C and found that the pointer algorithm in C allows access beyond the end of the array An element of. I know that C allows memory to be used for almost anything, but I just don’t understand, what is the purpose of this feature?

C does not allow access to memory beyond the end of the array. However, it allows pointers to point to an element beyond the end of the array. The distinction is important.

This will do:

char array[N];
char *p;
char *end;

for (p = array, end = array + N; p do_something(p);

(It will be an error to do *end.)

This shows why this feature is useful: pointers to (non-existent) elements after the end of the array are very useful for comparisons (such as loops).

Technically speaking, this is the C standard Everything that is allowed. However, in fact, the C implementation (compiler and runtime) does not check whether to access memory beyond the end of the array, whether it is one element or more. There must be bounds checking, which will slow down program execution Speed. The most suitable program (system programming, general library) C type of speed is often more than the security and security boundary check brings more.

This means that C may not be good for general application programming Tools.

Leave a Comment

Your email address will not be published.