473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C return a++ - is it safe?

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; )
--
James

Oct 29 '07
51 3888
Tor Rustad <to********@hotmail.comwrote:
Chris Hills wrote:
In article <bJ*********************@telenor.com>, Tor Rustad
Kenneth Brody wrote:

First, I didn't agree with Chris Hills, because

1. I didn't find such a C compiler bug likely
"Likely" is not good enough. You have to be certain.

"Testing can show the presence of errors, but not their absence."
-E. W. Dijkstra
"Beware of bugs in the above code; I have only proved it correct, not
tried it." - Donald E. Knuth
Still, there is a non-zero probability, that the same compiler fault can
hit all of them, e.g. via some common mode of failure.
Highly likely, in fact, if they're all working from the same specs.

Richard
Nov 5 '07 #51
oz****@bigpond.com (ozbear) writes:
On Sat, 03 Nov 2007 03:34:58 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
>>James Harris <ja************@googlemail.comwrites:
>>On 30 Oct, 20:10, Keith Thompson <ks...@mib.orgwrote:
...
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?

In any context other than a return statement
mov eax, a
{use eax}
inc eax

Not quite "any". In a function call, f(a++) the side-effects must
have happened by the time function is entered. Of course, if the "use
it" is "push it" and the function call follows the inc (and another
mov) we are OK, but the same could also be done for the return (the
"use it" being put into register used for return values).
<snip>

I am not quite sure I understand what you are saying. While a
sequence point does occur just before and after the function call, the
value of /a/ passed will be its pre-incremented value.
Yes. I was making a very small point. The discussion was about how
likely it is that 'return a++;' will be miss-compiled. James Harris
pointed out that "in any context other than return" the compiler can
load the value of a, generate the code that uses it, then emit an
increment instruction. I was just pointing out that 'f(a++);' is
similar -- the code that uses 'a' gets the prior value but the
increment must have happened before any of that code is executed.

--
Ben.
Nov 11 '07 #52

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Eskie | last post by:
Hello, I've seen in some programs that they provide functions which has the following prototype: char *func1(); However I'm not sure it's safe. If the char array beeing returned is a local...
1
by: Tropos | last post by:
Query: Will a MutexGuard object release before a function return value is copied? Consider the C++ code: class MutexGuard //A familiar sort of class for making mutexes exception-safe { . ....
7
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
5
by: starket | last post by:
Hi folks, I'm new to programming, please help, char * b, q, *r; b=getbuf(); q = *b; r= anotherfunction(b); /* we want to use ‘q' and ‘r' here*/ char * getbuf() {
2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
7
by: Jim Showalter | last post by:
I always thought that it is safe for a function to return a pointer to static storage. And the following code does compile quietly with: gcc -pedantic -Wall -o foo foo.c #include <stdio.h> ...
14
by: zeroDontSpamtype | last post by:
Hi, Why do strcpy and strcat (and strupr and strlwr in some nonstandard implementations) return a char*? Surely the logical (and DMA-safe) )return type for these would have been void?? ...
13
by: cppquester | last post by:
A colleague told me that there is a rule about good stype that a function in C++ should have only one point of return (ie. return statement). Otherwise there might be trouble. I never heard about...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
50
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.