the position after the last element of an array


Book told us:
C guarantees that, given an array, a pointer to any array element, or to the position after the last element, is a valid pointer.

Is this means the position after the last element and the pointer that points to it can be used for some purpose? Can you show me an example?

I will be very appreciated for any help.


  • Well, not exactly. What it means is that you do not have [b]"array bounds checking"[/b] in C. That is to say, you can go on accessing the locations even beyond the logical upper limit of your array, and C won't complain.

    But [b]this is not[/b] a [b]secure[/b], and for that matter [b]not at all a good programming practice[/b]. A programmer should always include a check to prevent code that tries to access elements beyond array limits. Object oriented programming languages like Java inherently include this "array bound checking" and will not allow you to execute a code that tries to access elements beyond your allocated array.

    Why this is not secure is because you may end up accessing memory locations that are not "legally" allocated to your program. This can cause [b]serious security issues[/b]. Being able to pry the code of other applications is not in the best interest of many. A particular attack called [b]"Buffer overflow attack"[/b] exploits the absence of array bound checking codes to gain unauthorized access to memory locations and manipulate the code there.

    Further issues may be caused since this can lead to [b]unstable programs[/b], crashing your applications, and even hanging your system.

    Good luck
    Happy coding :)
  • Thank you for your explanation.

    However, why it said C guarantees ... a pointer to the position after the last elementelements (beyond array limits) is a valid pointer? We can also say that a pointer to the position after any variable is a valid pointer!? Or, we can even say all pointer to any position is a valid pointer???

  • [color=Blue]Not the best book you got - get another one.
    This pointer is invalid in most cases. The problem is that this pointer will cause undefined behaviour. This means that pointer may be valid and may be invalid depending on some factors, like compiling a program with optimizations switched on or off. Or the array was allocated on stack or on heap - or other kinds of factors. The quality code should never even allow the undefined behaviour. So, just accept it that this pointer is invalid.

    I would like to add something here. Maybe book is talking about a special kind of array in C - it is called a string. A string is array of characters terminated by a character with binary code zero (character null). A lot of 1st "graders" get confused here. For example take string "HELLO" - how many elements in that array? For untrained eye - it has 5 elements. But that eye did not count that terminating null. With that null - the string array has 6 elements. So, if the book was talking about that kind of situation - pointer to the element following the LAST STRING ELEMENT - then that statement IS CORRECT if we talking about the valid characters (not null). Here is that example in visual aid:[/color]
    | [color=Green]H[/color] | [color=Green]E[/color] | [color=Green]L[/color] | [color=Green]L[/color] | [color=Green]O[/color] | [color=Red]null[/color] | .... undefined behaviour |
    | | | | | | | begins here |
    | |
    Pointer still valid -->| |
    Pointer is invalid -->|
  • Thank you so much.

    I checked the book again......
    The book is C Primer Plus, Fifth Edition (is that book not good?)
    In Chapter 10. Arrays and Pointers, it said:

    C guarantees that, given an array, a pointer to any array element, or to the position after the last element, is a valid pointer. But the effect of incrementing or decrementing a pointer beyond these limits is undefined. Also, you can dereference a pointer to any array element. However, even though a pointer to one past the end element is valid, it's not guaranteed that such a one-past-the-end pointer can be dereferenced.

    It is only for the one-past-the-end pointer.
    I confused why a valid pointer might not be dereferenced.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!