Afficher un message
Vieux 06/02/2008, 01h14   #6
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Neatest way to get the end pointer?

On Feb 6, 2:08 am, Keith Thompson <ks...@mib.org> wrote:
> vipps...@gmail.com writes:
> > On Feb 6, 12:40 am, "Tomás Ó hÉilidhe" <t...@lavabit.com> wrote:
> >> I commonly use pointers to iterate thru an array. For example:

>
> >> int my_array[X];

> [snip]
> >> 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, 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.
Correct me if I am wrong, but I have also seen comments in GNU code
like this:
--
/* ANSI C violation */
char * s = p - 1;
--
Where p points to the start of a string passed. 's' is never
dereferenced, only used for comparing purposes (while(p > s) or
something similar)
  Réponse avec citation
 
Page generated in 0,04759 seconds with 9 queries