473,769 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3968
In article <47************ ***@yahoo.com>, CBFalconer
<cb********@yah oo.comwrites
>Chris Hills wrote:
>Larry__Weiss <lf*@airmail.ne twrites
... snip ...
>>
>>Do set of programs exist that attempt to validate a C
implementatio n?

Yes
>>If so, are any of these freely available?

No

Yes. The gcc test suite is freely available. However it is geared
to "gcc C" rather than ISO std C.
OFF TOPIC :-) this NG as you have REPEATEDLY told every one is for the
discussion of STANDARD C. GCC is not C it is "a C like language"
according to your oft repeated definition.

You of all people should refrain from this level of hypocrisy!

So it is NOT a C test suite.
It is a GCC build confirmation test.

Also AFAIK It falls far short of what a test suite for a compiler should
be.
See Perennial and Plum Hall for C test suites.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 1 '07 #21
In article <11************ **********@v3g2 000hsg.googlegr oups.com>, Dave
Hansen <id**@hotmail.c omwrites
>On Oct 30, 11:35 am, Chris Hills <ch...@phaedsys .orgwrote:
>In article <47274b17.15255 6...@news.sbtc. net>, Richard Harter
<c...@tiac.net writes
[...]
>>
>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....

In that case (betting my life), Ada has no advantage over C.

"Trust, but verify."

Regards,

-=Dave
Absolutely,.... For years I have been arguing there is no such thing as
a
"safe" language. It is all down to the quality of the implementation
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 1 '07 #22
In article <bJ************ *********@telen or.com>, Tor Rustad
<to********@hot mail.comwrites
>Kenneth Brody wrote:

[in addition, my response applies to Kuyper and Sosman]
>Tor Rustad wrote:
[...]
>>However, in safety-critical SW, I wouldn't advocate using construct
like

return a++;

anyway.
Why not? If the compiler doesn't handle it right, then I wouldn't
trust it for the rest of the "safety-critical" program either.


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.
>2. If such a compiler bug existed, the unit test should detect it
Absolutely.
>My comment on not advocating returning a++, is more of a stylish
matter, since post conditions in functions, usually follow after the
last expression, and before the return statement.

Returning expressions, obfuscate debugging too.
I agree completely.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 1 '07 #23
In article <1193844678.287 107@news1nwk>, Eric Sosman
<Er*********@su n.comwrites
>Tor Rustad wrote On 10/30/07 19:06,:
>[...]
However, in safety-critical SW, I wouldn't advocate using construct
like

return a++;

What's your opinion of `return getchar();'? Keep
in mind that in many implementations , getchar() is a
macro that expands to an expression with side-effects.
On an implementation I happen to have handy at the
moment, `return getchar();' produces (reformatted for
clarity)

return ( --(( &__iob[0]))->_cnt < 0
? __filbuf( (&__iob[0]) )
: (int) *(( &__iob[0]))->_ptr++ );

So, what's your verdict? Should `return getchar();'
be avoided in safety-critical software?
Personally I always prefer to just return a value.

I would

a = getchar();

return a;

It also helps of you are going to single step the C (as opposed to the
assembler) or you are doing white box unit testing.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 1 '07 #24
Chris Hills <ch***@phaedsys .orgwrites:
In article <47************ ***@yahoo.com>, CBFalconer
<cb********@yah oo.comwrites
>>Chris Hills wrote:
>>Larry__Weis s <lf*@airmail.ne twrites
... snip ...
>>>
Do set of programs exist that attempt to validate a C
implementati on?

Yes

If so, are any of these freely available?

No

Yes. The gcc test suite is freely available. However it is geared
to "gcc C" rather than ISO std C.

OFF TOPIC :-) this NG as you have REPEATEDLY told every one is for the
discussion of STANDARD C. GCC is not C it is "a C like language"
according to your oft repeated definition.

You of all people should refrain from this level of hypocrisy!

So it is NOT a C test suite.
It is a GCC build confirmation test.

Also AFAIK It falls far short of what a test suite for a compiler
should be.
See Perennial and Plum Hall for C test suites.
I'm not familiar with the gcc test suite, but since gcc attempts to
conform to C90 and/or C95 in certain modes, I would assume that the
test suite would, among other things, test that conformance.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 1 '07 #25
Chris Hills <ch***@phaedsys .orgwrites:
In article <1193844678.287 107@news1nwk>, Eric Sosman
<Er*********@su n.comwrites
>>Tor Rustad wrote On 10/30/07 19:06,:
>>[...]
However, in safety-critical SW, I wouldn't advocate using construct
like

return a++;

What's your opinion of `return getchar();'? Keep
in mind that in many implementations , getchar() is a
macro that expands to an expression with side-effects.
On an implementation I happen to have handy at the
moment, `return getchar();' produces (reformatted for
clarity)

return ( --(( &__iob[0]))->_cnt < 0
? __filbuf( (&__iob[0]) )
: (int) *(( &__iob[0]))->_ptr++ );

So, what's your verdict? Should `return getchar();'
be avoided in safety-critical software?

Personally I always prefer to just return a value.

I would

a = getchar();

return a;
Ok, that's fine -- but a compiler that mishandles ``return <expr>''
would be nearly as likely to mishandle ``a = <expr>''.

I just don't see that there's any significant need to worry more about
``return a++;'' than about any other straightforward language
construct.

If you think that something like:
result = a++;
return result;
is better style than
return a++;
I won't necessarily argue -- but either form calls for exactly the
same amount of testing.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 1 '07 #26
Chris Hills <ch***@phaedsys .orgwrites:
In article <bJ************ *********@telen or.com>, Tor Rustad
<to********@hot mail.comwrites
>>Kenneth Brody wrote:

[in addition, my response applies to Kuyper and Sosman]
>>Tor Rustad wrote:
[...]
However, in safety-critical SW, I wouldn't advocate using construct
like

return a++;

anyway.
Why not? If the compiler doesn't handle it right, then I wouldn't
trust it for the rest of the "safety-critical" program either.


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.
What a pity that certainty is not possible.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 1 '07 #27
Chris Hills <ch***@phaedsys .orgwrote:
In article <bJ************ *********@telen or.com>, Tor Rustad
1. I didn't find such a C compiler bug likely

"Likely" is not good enough. You have to be certain.
If you want certainty, you'll have to unplug your computer.

No, just turning it off may not be enough to be certain.

Richard
Nov 1 '07 #28
Keith Thompson said:
Chris Hills writes:
>Tor Rustad writes
<snip>
>>>1. I didn't find such a C compiler bug likely

"Likely" is not good enough. You have to be certain.

What a pity that certainty is not possible.
Are you sure about that?

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 1 '07 #29
Richard Heathfield wrote:
Keith Thompson said:
....
>What a pity that certainty is not possible.

Are you sure about that?
Reasonably sure, but not certain.
Nov 1 '07 #30

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

Similar topics

2
10494
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 variable such as: char *func1()
1
2621
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 { . . . ~MutexGuard(); //releases the mutex when the stack pops
7
3576
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
4338
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
2631
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 feasible. It would be helpful if people could test the patched Python with their own applications and report any incompatibilities. PEP: 349
7
6146
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> static char *foo (int y) { static char s;
14
3717
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?? Thanks, James McLaughlin.
13
2914
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 it and doubt it. Anybody heard of it? What would be the advantage? Regards, Marc Example:
173
8179
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. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
50
5237
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 success or not. Am I wrong? Bill
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.