Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
the position after the last element of an array Posted by mark123smith on 30 Apr 2011 at 12:08 PM
Hi,

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.
Report
Re: the position after the last element of an array Posted by virtual_arts on 30 Apr 2011 at 7:50 PM
Well, not exactly. What it means is that you do not have "array bounds checking" 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 this is not a secure, and for that matter not at all a good programming practice. 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 serious security issues. Being able to pry the code of other applications is not in the best interest of many. A particular attack called "Buffer overflow attack" 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 unstable programs, crashing your applications, and even hanging your system.

Good luck
Happy coding :)
Report
Re: the position after the last element of an array Posted by mark123smith on 30 Apr 2011 at 8:56 PM
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???

Report
Re: the position after the last element of an array Posted by AsmGuru62 on 1 May 2011 at 4:38 AM
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:

    +---+---+---+---+---+------+--------------------------+
    | H | E | L | L | O | null | .... undefined behaviour |
    |   |   |   |   |   |      |      begins here         |
    +---+---+---+---+---+------+--------------------------+
                        |      |
 Pointer still valid -->|      |
         Pointer is invalid -->|

Report
Re: the position after the last element of an array Posted by mark123smith on 2 May 2011 at 4:44 AM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.