Re: Neatest way to get the end pointer?
- From: Peter Nilsson <airia@xxxxxxxxxxx>
- Date: Tue, 5 Feb 2008 16:34:25 -0800 (PST)
vipps...@xxxxxxxxx wrote:
Keith Thompson <ks...@xxxxxxx> wrote:
vipps...@xxxxxxxxx writes:
"Tomás Ó hÉilidhe" <t...@xxxxxxxxxxx> wrote:[snip]
I commonly use pointers to iterate thru an array.
For example:
int my_array[X];
1) my_array is an int[X]
2) &my_array is an int(*)[X]
3) &my_array+1 is the address of the non-existant
array located after the current one.
And you invoke undefined behavior.
No, he doesn't. &my_array+1 is a valid address,
Though it requires a conversion back to int * if used
as a pointer 'index' compared against an int * iterator.
just past the end of my_array. Computing this address
is ok; attempting to dereference it would invoke UB.
I think that when the result of an expression is a non-
valid pointer, the behavior is undefined.
True, but one byte past the end of an array _is_ a valid
pointer.
Correct me if I am wrong,
Keith already has.
but I have also seen comments in GNU code like this:
--
/* ANSI C violation */
char * s = p - 1;
One byte beyond is fine (though you can't dereference it),
but there are no special rights for one (or more) byte(s)
_before_.
--
Where p points to the start of a string passed. 's' is
never dereferenced, ...
Doesn't matter. Implementations do not have to cope with
'one byte before'. An allocation can be made at the start
of a memory page. Merely calculating an address prior to
such a page may trap. In contrast, implementations _must_
cope with one byte beyond, which simply means that at
least one byte (but typically _at most_ one byte) is
reserved at the end of the memory space.
--
Peter
.
- Follow-Ups:
- Re: Neatest way to get the end pointer?
- From: vippstar
- Re: Neatest way to get the end pointer?
- References:
- Neatest way to get the end pointer?
- From: Tomás Ó hÉilidhe
- Re: Neatest way to get the end pointer?
- From: vippstar
- Re: Neatest way to get the end pointer?
- From: Keith Thompson
- Re: Neatest way to get the end pointer?
- From: vippstar
- Neatest way to get the end pointer?
- Prev by Date: Re: Neatest way to get the end pointer?
- Next by Date: Re: Confused by compiler warning
- Previous by thread: Re: Neatest way to get the end pointer?
- Next by thread: Re: Neatest way to get the end pointer?
- Index(es):
Relevant Pages
|