Definition of 'evaluate'

O

Old Wolf

Does the standard define the meaning of 'evaluate' ? Someone
in another newsgroup said they thought evaluating a pointer
meant dereferencing it, and I couldn't find any text to contradict
this.
 
A

Alf P. Steinbach

* Old Wolf:
Does the standard define the meaning of 'evaluate' ? Someone
in another newsgroup said they thought evaluating a pointer
meant dereferencing it, and I couldn't find any text to contradict
this.

The standard defines "used" in terms of appearance in a "potentially
evaluated" expression, in §3.2/2. The term "evaluate" is not explicitly
defined, as far as I can see. But it means to produce a single value or
reference of the type of the expression, and is implicitly defined that
way by the usage of "evaluation" in §5 (Expressions).

For a pointer variable p, the id-expression p evaluates the pointer,
producing its value. This contradicts the notion that evaluation
necessarily involves dereferencing.

The dereferencing expression *p also evaluates the pointer.
 
A

Andrew Koenig

Does the standard define the meaning of 'evaluate' ? Someone
in another newsgroup said they thought evaluating a pointer
meant dereferencing it, and I couldn't find any text to contradict
this.

If evaluating a pointer meant dereferencing it, the following code fragment
would have undefined behavior:

int* p = 0;
int* q = p;

because (a) initializing q requires evaluating p and (b) dereferencing a
zero pointer evokes undefined behavior.
 
M

Michael DOUBEZ

Andrew Koenig a écrit :
If evaluating a pointer meant dereferencing it, the following code fragment
would have undefined behavior:

int* p = 0;
int* q = p;

because (a) initializing q requires evaluating p and (b) dereferencing a
zero pointer evokes undefined behavior.

That was a question I had (I didn't remember it):
3.5.4.2 note 4
The effect of using an invalid pointer value (including passing it to
adeallocation function) is undefined.

Is returning a pointer value actually 'using it' ?

The relevant example was:
int* f()
{
return reinterpret_cast<int*>(0x42424242);

}
Is calling f() UB because it returns an invalid pointer or is it just
using/deferencing the returned value that is UB ?

Michael
 

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
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top