Pointer arithmetic question

J

Joe Wright

Keith Thompson wrote:

[ too much snippage ]
Undefined behavior includes cases where the standard fails to
state what the behavior is.

This is too perfect for further comment. Go Keith!
 
K

Keith Thompson

Joe Wright said:
Keith Thompson wrote:

[ too much snippage ]
Undefined behavior includes cases where the standard fails to
state what the behavior is.

This is too perfect for further comment. Go Keith!

Thanks -- but I'm going to comment further myself.

I was summarizing C99 4p2, which says:

If a "shall" or "shall not" requirement that appears outside of a
constraint is violated, the behavior is undefined. Undefined
behavior is otherwise indicated in this International Standard by
the words "undefined behavior" or by the omission of any explicit
definition of behavior. There is no difference in emphasis among
these three; they all describe "behavior that is undefined".
 
J

Jim

Actually, adding 0 to a null pointer invokes undefined behavior:

If both the pointer operand and the result point to elements of
the same array object, or one past the last element of the array
object, the evaluation shall not produce an overflow; otherwise,
the behavior is undefined.

It's likely to quietly yield a null pointer on most implementations,
but the standard doesn't require it.

I would say that the behaviour is defined for NULL+0. The standard
(C99 6.3.2.3 3) says any constant integer expression == 0 is
equivalent to a NULL pointer.

Jim
 
K

Keith Thompson

Jim said:
I would say that the behaviour is defined for NULL+0. The standard
(C99 6.3.2.3 3) says any constant integer expression == 0 is
equivalent to a NULL pointer.

NULL+0 is a null pointer constant only if NULL is an integer constant
expression. But we were discussing null pointers, not null pointer
constants.

Given:
int *ptr = NULL; /* or 0, or '-'-'-' */
the expression
ptr + 0
invokes undefined behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top