C return a++ - is it safe?

J

James Harris

If in C one codes a function which includes

return a++;

does 'a' get incremented? Would the behaviour be safe to rely upon? I
guess this might apply if 'a' were static or such as

return ptr->element++;

I didn't find this in the comp.lang.c faq (at least the first one that
came up from Google; there seemed to be many) but didn't find it. No
doubt someone will tell me where I should have looked.

(For the time being I'm coding: a++; return a - 1; )
 
J

Joachim Schmitz

James Harris said:
If in C one codes a function which includes

return a++;

does 'a' get incremented? Would the behaviour be safe to rely upon? I
guess this might apply if 'a' were static or such as

return ptr->element++;

I didn't find this in the comp.lang.c faq (at least the first one that
came up from Google; there seemed to be many) but didn't find it. No
doubt someone will tell me where I should have looked.

(For the time being I'm coding: a++; return a - 1; )
It is safe as ; constitutes a sequence point

Bye, Jojo
 
C

Christopher Benson-Manica

[comp.lang.c] Joachim Schmitz said:
James Harris said:
If in C one codes a function which includes

return a++;

does 'a' get incremented?
Yes.
Would the behaviour be safe to rely upon?
Yes.
I didn't find this in the comp.lang.c faq (at least the first one that
came up from Google; there seemed to be many) but didn't find it. No
doubt someone will tell me where I should have looked.

I imagine K&R explains the issue, but if you looked there and still
weren't sure, this is an appropriate venue for the question - I'm
pretty sure it's not in the FAQ.
It is safe as ; constitutes a sequence point

That's true, but that wasn't OP's question.
 
K

Kenneth Brody

Christopher said:
[comp.lang.c] Joachim Schmitz said:
James Harris said:
If in C one codes a function which includes

return a++;

does 'a' get incremented? [...]
(For the time being I'm coding: a++; return a - 1; )
It is safe as ; constitutes a sequence point

That's true, but that wasn't OP's question.

Indirectly, it is. The increment is guaranteed to take place by
the time the sequence point is "reached". Therefore, the increment
is guaranteed to be done before the return completes.

However, if "a" were a non-static non-volatile automatic variable,
can the compiler skip the increment? I believe so, because it will
go out of scope upon executing the return, and the result is "as
if" the increment were actually done.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Kenneth Brody said:
Christopher said:
[comp.lang.c] Joachim Schmitz said:
If in C one codes a function which includes
return a++;

does 'a' get incremented? [...]
(For the time being I'm coding: a++; return a - 1; )
It is safe as ; constitutes a sequence point

That's true, but that wasn't OP's question.

Indirectly, it is. The increment is guaranteed to take place by
the time the sequence point is "reached". Therefore, the increment
is guaranteed to be done before the return completes.

However, if "a" were a non-static non-volatile automatic variable,
can the compiler skip the increment? I believe so, because it will
go out of scope upon executing the return, and the result is "as
if" the increment were actually done.

The OP specifically mentioned the case where ``a'' is not local to the
function.
 
C

Chris Hills

Keith Thompson said:
Kenneth Brody said:
Christopher said:
If in C one codes a function which includes
return a++;

does 'a' get incremented? [...]
(For the time being I'm coding: a++; return a - 1; )
It is safe as ; constitutes a sequence point

That's true, but that wasn't OP's question.

Indirectly, it is. The increment is guaranteed to take place by
the time the sequence point is "reached". Therefore, the increment
is guaranteed to be done before the return completes.

However, if "a" were a non-static non-volatile automatic variable,
can the compiler skip the increment? I believe so, because it will
go out of scope upon executing the return, and the result is "as
if" the increment were actually done.

The OP specifically mentioned the case where ``a'' is not local to the
function.

Regardless of the standard(s) I think this is one you would need to
empirically test of the compiler(s) in question.

If this were Ada one could just refer to the standard but it's C and
nothing is guaranteed.
 
P

pete

James said:
If in C one codes a function which includes

return a++;

does 'a' get incremented?
Yes.

Would the behaviour be safe to rely upon?
Yes.

I guess this might apply if 'a' were static

Yes.
 
K

Keith Thompson

Chris Hills said:
Keith Thompson said:
Kenneth Brody said:
Christopher Benson-Manica wrote:
If in C one codes a function which includes
return a++;

does 'a' get incremented?
[...]
(For the time being I'm coding: a++; return a - 1; )
It is safe as ; constitutes a sequence point

That's true, but that wasn't OP's question.

Indirectly, it is. The increment is guaranteed to take place by
the time the sequence point is "reached". Therefore, the increment
is guaranteed to be done before the return completes.

However, if "a" were a non-static non-volatile automatic variable,
can the compiler skip the increment? I believe so, because it will
go out of scope upon executing the return, and the result is "as
if" the increment were actually done.

The OP specifically mentioned the case where ``a'' is not local to the
function.

Regardless of the standard(s) I think this is one you would need to
empirically test of the compiler(s) in question.

If this were Ada one could just refer to the standard but it's C and
nothing is guaranteed.

Huh?

The C standard defines how the return statement and the "++" operator
work. If ``return a++;'' doesn't increment ``a'' (other than in cases
covered by the as-if rule), then the compiler is broken.

The C and Ada standards are both written in English, and in fairly
similar styles. C leaves more things undefined than Ada does, but
it's reasonably clear about *what* it leaves undefined.
 
R

Richard Harter

Chris Hills said:
Keith Thompson said:
Christopher Benson-Manica wrote:
If in C one codes a function which includes
return a++;

does 'a' get incremented?
[...]
(For the time being I'm coding: a++; return a - 1; )
It is safe as ; constitutes a sequence point

That's true, but that wasn't OP's question.

Indirectly, it is. The increment is guaranteed to take place by
the time the sequence point is "reached". Therefore, the increment
is guaranteed to be done before the return completes.

However, if "a" were a non-static non-volatile automatic variable,
can the compiler skip the increment? I believe so, because it will
go out of scope upon executing the return, and the result is "as
if" the increment were actually done.

The OP specifically mentioned the case where ``a'' is not local to the
function.

Regardless of the standard(s) I think this is one you would need to
empirically test of the compiler(s) in question.

If this were Ada one could just refer to the standard but it's C and
nothing is guaranteed.

Huh?

The C standard defines how the return statement and the "++" operator
work. If ``return a++;'' doesn't increment ``a'' (other than in cases
covered by the as-if rule), then the compiler is broken.

The C and Ada standards are both written in English, and in fairly
similar styles. C leaves more things undefined than Ada does, but
it's reasonably clear about *what* it leaves undefined.

I believe you're missing his point. Ada has a very thorough
validation suite and stringent restrictions on what may be called
an Ada compiler. This isn't the case with C compilers; anybody
can produce what they call a C compiler. It is even possible
that there is no such thing as a fully conforming C compiler.

His point is that in the real world this is the kind of thing
that you should check whether the compiler gets it right. Off
hand, I would think that it is the sort of thing that a compiler
could be expected to get right but I might well be wrong.




Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri, http://www.varinoma.com
In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die
 
L

Larry__Weiss

Richard said:
I believe you're missing his point. Ada has a very thorough
validation suite and stringent restrictions on what may be called
an Ada compiler. This isn't the case with C compilers; anybody
can produce what they call a C compiler. It is even possible
that there is no such thing as a fully conforming C compiler.

His point is that in the real world this is the kind of thing
that you should check whether the compiler gets it right. Off
hand, I would think that it is the sort of thing that a compiler
could be expected to get right but I might well be wrong.

Do set of programs exist that attempt to validate a C implementation?

If so, are any of these freely available?

- Larry
 
C

Chris Hills

[QUOTE="pete said:
If in C one codes a function which includes

return a++;

does 'a' get incremented?
Yes.

Would the behaviour be safe to rely upon?
Yes.

I guess this might apply if 'a' were static

Yes.[/QUOTE]

Oh dear.......
 
C

Chris Hills

Richard Harter said:
Chris Hills said:
If in C one codes a function which includes
return a++;

does 'a' get incremented?
[...]
(For the time being I'm coding: a++; return a - 1; )
It is safe as ; constitutes a sequence point

That's true, but that wasn't OP's question.

Indirectly, it is. The increment is guaranteed to take place by
the time the sequence point is "reached". Therefore, the increment
is guaranteed to be done before the return completes.

However, if "a" were a non-static non-volatile automatic variable,
can the compiler skip the increment? I believe so, because it will
go out of scope upon executing the return, and the result is "as
if" the increment were actually done.

The OP specifically mentioned the case where ``a'' is not local to the
function.

Regardless of the standard(s) I think this is one you would need to
empirically test of the compiler(s) in question.

If this were Ada one could just refer to the standard but it's C and
nothing is guaranteed.

Huh?

The C standard defines how the return statement and the "++" operator
work. If ``return a++;'' doesn't increment ``a'' (other than in cases
covered by the as-if rule), then the compiler is broken.

The C and Ada standards are both written in English, and in fairly
similar styles. C leaves more things undefined than Ada does, but
it's reasonably clear about *what* it leaves undefined.

I believe you're missing his point. Ada has a very thorough
validation suite and stringent restrictions on what may be called
an Ada compiler. This isn't the case with C compilers; anybody
can produce what they call a C compiler. It is even possible
that there is no such thing as a fully conforming C compiler.

His point is that in the real world this is the kind of thing
that you should check whether the compiler gets it right.
Precisely

Off
hand, I would think that it is the sort of thing that a compiler
could be expected to get right but I might well be wrong.

In other words you wouldn't bet your life on it....
 
M

Mark McIntyre

Ada has a very thorough
validation suite and stringent restrictions on what may be called
an Ada compiler. This isn't the case with C compilers; anybody
can produce what they call a C compiler.

But this is only true to the same extent that anyone can produce
something they call an Ada compiler. For instance AFAIK its not
actually illegal in the UK or US to call your firstborn child "An Ada
Compiler".
It is even possible
that there is no such thing as a fully conforming C compiler.

*shrug*. And possibly there are no general purpose multiplatform Ada
compilers either.

I think your point is moot since Ada and C have different uses and
scopes. Any discussion is likely to be as sensible as debating whether
fish are more useful than footballs. The answer is "it depends".

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Martin Wells

Kenneth:
However, if "a" were a non-static non-volatile automatic variable,
can the compiler skip the increment? I believe so, because it will
go out of scope upon executing the return, and the result is "as
if" the increment were actually done.


If the local variable were neither volatile nor static, then the
increment is utterly redundant... and I wouldn't mind the compiler
issuing a warning something like:

WARNING: Redundant operation on local automatic variable

Only two reasons come to mind as to why you'd have a redundant
operation on a local automatic variable:

1) You intended to define it as either static or volatile.
2) You're stupid.

Martin
 
K

Kenneth Brody

[... Talking about "return a++;" ...]

Martin said:
Kenneth:


If the local variable were neither volatile nor static, then the
increment is utterly redundant... and I wouldn't mind the compiler
issuing a warning something like:

WARNING: Redundant operation on local automatic variable

"Redundant", or "useless"?
Only two reasons come to mind as to why you'd have a redundant
operation on a local automatic variable:

1) You intended to define it as either static or volatile.
2) You're stupid.

3) You're wondering whether your compiler will optimize it away.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Chris Hills said:
In other words you wouldn't bet your life on it....

All software (including compilers) has bugs. All software should be
thoroughly tested. But a compiler bug in the handling of ``return
a++;'' is quite low on the list of things I'd worry about -- higher
than ``1 + 1 == 2'', lower than, say, some obscure C99 feature, and
about the same as ``return (a = 42);''.

Consider how the return statement is defined: the expression is
evaluated, and the result is returned. Updating ``a'' is part of the
evaluation of the expression.

Is there some particular reason you're concerned that a compiler might
handle something this simple incorrectly?
 
C

Charlie Gordon

Martin Wells said:
Kenneth:


If the local variable were neither volatile nor static, then the
increment is utterly redundant... and I wouldn't mind the compiler
issuing a warning something like:

WARNING: Redundant operation on local automatic variable

Only two reasons come to mind as to why you'd have a redundant
operation on a local automatic variable:

1) You intended to define it as either static or volatile.
2) You're stupid.

Or you could be using macros with side effects:

#define GETBYTE(p) (*(p)++ & 0xFF)

int safegetbyte(const char *s) {
if (!s)
return -1;
return GETBYTE(s);
}
 
T

Tor Rustad

Chris Hills wrote:

[...]
Regardless of the standard(s) I think this is one you would need to
empirically test of the compiler(s) in question.

If this were Ada one could just refer to the standard but it's C and
nothing is guaranteed.

Well, some C compilers has been validated:

http://www.peren.com/pages/cvsa_set.htm
http://www.plumhall.com/stec.html

and at this point, the C90 test cases should detect such an compiler
bug. However, in safety-critical SW, I wouldn't advocate using construct
like

return a++;

anyway.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top