473,782 Members | 2,542 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 3973
Chris Hills <ch***@phaedsys .orgwrites:
In article <ln************ @nuthaus.mib.or g>, Keith Thompson
<ks***@mib.orgw rites
>>Chris Hills <ch***@phaedsys .orgwrites:
>>In article <ln************ @nuthaus.mib.or g>, Keith Thompson
<ks***@mib.or gwrites
Chris Hills <ch***@phaedsys .orgwrites:
[...]
>>>>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.

The test suite will test for GCC not ISO C
>>ability to act as a more-or-less ISO C compiler
>I
would assume that a gcc test suite
>>In any case, my assumption was a

More or less and two assumptions when discussing a test suite for
conformance to an ISO standard? No more need be said I think

When we do compiler validation there are no assumptions or more or less.
>>, and Chuck
probably made the same assumption.

Another probably and assumption. When testing or validating there is
no place for probably or assumptions
Agreed. Anybody who wants to try using the gcc test suite to test a
non-gcc ISO C compiler obviously will have to investigate the suite to
determine whether it will suits his purposes. Maybe it will, maybe it
won't.
> Accusing Chuck of hypocricy for
suggesting it is needlessly insulting.

Chuck is VERY pedantic and insulting to other in this NG. I was
judging him by his own standards.

Also he is being VERY hypocritical GCC is Not C but "C like" as he
tells everyone else about their compiler when discussing other
compilers.

He can't have it both ways.
And neither can you.

I asked you whether you're actually familiar with the gcc test suite.
Though you didn't answer directly, your responses implied that you
haven't even looked at it.

The previous poster asked about C test suites. Suggesting the gcc
test suite is perfectly reasonable (with the proviso that it *may or
may not* be suitable). You, on the other hand, are accusing Chuck of
hypocricy -- and you *don't know what you're talking about*.

Next time, do a little research before you start flinging insults.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 2 '07 #41
Chris Hills <ch***@phaedsys .orgwrites:
[snip]
Also he is being VERY hypocritical GCC is Not C but "C like" as he
tells everyone else about their compiler when discussing other
compilers.
As you should know, gcc can be used as a reasonably conforming C90 or
C95 compiler, and a partially conforming C99 compiler.

But we were talking about the gcc test suite, not about gcc itself.

See <http://pcc.ludd.ltu.se/standards_and_r egression_testi ng/and
<http://pcc.ludd.ltu.se/standards_and_r egression_testi ng/gcc_testsuite/>
for information about using the gcc test suite on pcc, a a non-gcc C
compiler. Obviously any gcc-specific tests are expected to fail, but
it appears that the suite is still useful.

This was on the first page of Google results for "gcc test suite".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 2 '07 #42
Chris Hills wrote:
Keith Thompson <ks***@mib.orgw rites
.... snip ...
>
>Accusing Chuck of hypocricy for suggesting it is needlessly
insulting.

Chuck is VERY pedantic and insulting to other in this NG. I was
judging him by his own standards.

Also he is being VERY hypocritical GCC is Not C but "C like" as
he tells everyone else about their compiler when discussing
other compilers.
Oh? Don't recall anything of the sort. I have been known to
chastise some people who insist on posting off-topic material.

BTW, "gcc -W -Wall -ansi -pedantic" is awfully close to a pure ISO
standard C compiler. That uses C95 standards, because that is what
the library on my system is compatible with, and it avoids the few
missing C99 features.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Nov 2 '07 #43
Chris Hills wrote:
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.
"Testing can show the presence of errors, but not their absence."
-E. W. Dijkstra
>2. If such a compiler bug existed, the unit test should detect it

Absolutely.
The probability of such a fault passing through, the C compiler test
cases, a C compiler validation suit and program unit test cases, is very
low. There is a range of other potential faults, I would worry far more
about.

An extremely fault tolerant system/module, could perhaps have
independent software design teams and guard against compiler faults, one
team writing the module in C, another in Ada.

Interesting, what actions should be done in case there is a mismatch in
the results. How to choose which SW module is correct? It appears, you
really need three SW teams, using 3 different languages/compilers.

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.

--
Tor <bw****@wvtqvm. vw | tr i-za-h a-z>
Nov 2 '07 #44
James Harris <ja************ @googlemail.com writes:
On 30 Oct, 20:10, Keith Thompson <ks...@mib.orgw rote:
...
>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).

One has to assume a very naive understanding of values, side-effects
and sequence points to suspect the code generated from the original
line.

--
Ben.
Nov 3 '07 #45
RoS
In data Thu, 01 Nov 2007 09:56:28 -0500, CBFalconer scrisse:
>Chris Hills wrote:
>Dave Hansen writes
>>Chris Hills <ch...@phaedsys .orgwrote:
Richard Harter <c...@tiac.netw rites
... snip ...
>>>>
Offhand, 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.

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
implementati on

Yes, no language is 'safe'. However, some are much safer than
others. For example, the presence of subtypes (such as Pascals
ranges) simply allows range checking everywhere. Whenever a
construct creates a value for that item that is known to be out of
range, the compiler complains. When a runtime operation exceeds
that range, the runtime complains. Similarly the accurate control
of pointers allows most misuse of pointer variables to be detected
_at compile time_. This is a great advantage over C.
i think i'm in your kill file so don't read what i write;

the same for very pedands ones

-----------------------------------------------------------------
------------------------------------------------------------------
you and other seems not understand the esistance of variables that
*can not* overflow e.g. size_t; or
int, unsigned, float, double etc in financial calculis etc etc

if some of that variables has an overflow it has to rappresent an
error number (eg: INT_MAX for +overflow and INT_MIN for -overflow)
so detect errors in run time is more easy

for an unsigned thype the aritimetic is very easy

0..UMAX

a+b = (a==UMAX || b==UMAX || oveflow(a+b)? UMAX : sum(a, b);
a-b = (a==UMAX || b==UMAX || a<b || oveflow(a-b)? UMAX : sum(a, b);

some like above for -*/

0..UMAX-1 is ok
UMAX is an error
>Unfortunatel y Pascal seems to have largely disappeared as a major
language. We are left with its descendent, Ada, which is a larger,
safer, and tested language. And also available in gcc.
>--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Nov 4 '07 #46
RoS wrote:

<snip>
you and other seems not understand the esistance of variables that
*can not* overflow e.g. size_t; or
int, unsigned, float, double etc in financial calculis etc etc
No. Signed types can overflow.
if some of that variables has an overflow it has to rappresent an
error number (eg: INT_MAX for +overflow and INT_MIN for -overflow)
so detect errors in run time is more easy
INT_MAX and INT_MIN are valid values. Using them to represent overflow
is, IMHO, not wise.
for an unsigned thype the aritimetic is very easy

0..UMAX
....
0..UMAX-1 is ok
UMAX is an error
No. Uxxx_MAX is a valid value of that type.

Nov 4 '07 #47
In article <ln************ @nuthaus.mib.or g>, Keith Thompson
<ks***@mib.orgw rites
>Chris Hills <ch***@phaedsys .orgwrites:
[snip]
>Also he is being VERY hypocritical GCC is Not C but "C like" as he
tells everyone else about their compiler when discussing other
compilers.

As you should know, gcc can be used as a reasonably conforming C90 or
C95 compiler, and a partially conforming C99 compiler.

But we were talking about the gcc test suite, not about gcc itself.

See <http://pcc.ludd.ltu.se/standards_and_r egression_testi ng/and
<http://pcc.ludd.ltu.se/standards_and_r egression_testi ng/gcc_testsuite/>
for information about using the gcc test suite on pcc, a a non-gcc C
compiler. Obviously any gcc-specific tests are expected to fail, but
it appears that the suite is still useful.

This was on the first page of Google results for "gcc test suite".
CBF gets VERY pedantic about ANYTHING non standard. As far as his logic
goes for any other compiler is that it is not a C compiler but a
compiler for a C like language.

Therefore the GCC compiler and it's test suite are not for C but a C
like language.

Now you understand what pisses off almost everyone else in this NG when
a few of you shout OT for everything that is not pure ISO-C You can't
expect to have it both ways.

However for testing a non gcc compiler the GCC test suite is not really
much use. (I asked some one who does do compiler validation for the
safety critical world and who has looked at the gcc test suite)

BTW the link you gave misses out one of the main C compiler test suits.
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 4 '07 #48
In article <ln************ @nuthaus.mib.or g>, Keith Thompson
<ks***@mib.orgw rites
>Chris Hills <ch***@phaedsys .orgwrites:
>In article <ln************ @nuthaus.mib.or g>, Keith Thompson
<ks***@mib.org writes
>>>Chris Hills <ch***@phaedsys .orgwrites:
In article <ln************ @nuthaus.mib.or g>, Keith Thompson
<ks***@mib.o rgwrites
>Chris Hills <ch***@phaedsys .orgwrites:

More or less and two assumptions when discussing a test suite for
conformance to an ISO standard? No more need be said I think

When we do compiler validation there are no assumptions or more or less.
>>>, and Chuck
probably made the same assumption.

Another probably and assumption. When testing or validating there is
no place for probably or assumptions

Agreed. Anybody who wants to try using the gcc test suite to test a
non-gcc ISO C compiler obviously will have to investigate the suite to
determine whether it will suits his purposes. Maybe it will, maybe it
won't.
There is a lot more to validation than that. I doubt it will be of any
real use.
>> Accusing Chuck of hypocricy for
suggesting it is needlessly insulting.

Chuck is VERY pedantic and insulting to other in this NG. I was
judging him by his own standards.

Also he is being VERY hypocritical GCC is Not C but "C like" as he
tells everyone else about their compiler when discussing other
compilers.

He can't have it both ways.

And neither can you.
I agree but just for once I am having a go at CBF with his own logic. He
doesn't like it.
>I asked you whether you're actually familiar with the gcc test suite.
Though you didn't answer directly, your responses implied that you
haven't even looked at it.
Correct. I asked some one else who had.
>The previous poster asked about C test suites. Suggesting the gcc
test suite is perfectly reasonable
No it is not. The GCC test suite is for GCC not ISO-C
>(with the proviso that it *may or
may not* be suitable). You, on the other hand, are accusing Chuck of
hypocricy -- and you *don't know what you're talking about*.
He is hypocritical. Given his usual stance on anything not PURE ISO C
the Gcc test suite falls down on his own logic.

This is something I and others have complained about that a small group
of you scream OT on anything you don't like but do not apply the same
rules to yourselves.

I could have suggested any number of test suites that (according to CBF
previously) are for compilers of a "c-like" language. When these
compilers are far more ISO -c that GCC.

CBF can't have it both ways. The GCC test suite is not an ISO C test
suite it is a GCC test suite. gcc is a "C like" language. Simple as
that.

Now you see how annoying it gets to the rest of use with al this OT
crap.

When it comes to compiler validation it is one of the few areas where
you have to be very precise and CBF wants to be approximate, maybe.

Anyway the good news I I will not be bothering you for a week as I am
off on a trip. Ironically it has to do with compiler validation...
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 4 '07 #49
RoS <Ro*@not.existw rites:
[...]
i think i'm in your kill file so don't read what i write;
If you deliberately evade killfiles by changing your name, don't
complain about people reading what you write.

Better yet, stop changing your name.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 4 '07 #50

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

Similar topics

2
10496
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
2622
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
2632
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
6147
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
2915
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
8186
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
5242
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
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
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
7492
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
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
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.