Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Eventual undefined behaviour

Reply
Thread Tools

Eventual undefined behaviour

 
 
Charlie Gordon
Guest
Posts: n/a
 
      09-15-2007
"CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)...
> Charlie Gordon wrote:
>>

> ... snip ...
>>
>> Come on, what gcc target does not do simple wrap around on signed
>> integer overflow ?

>
> At least any that implement overflow traps. I can set up the 8088
> to do just that.


Yes, but do you have an OS or even a program that runs in this mode ?

--
Chqrlie.


 
Reply With Quote
 
 
 
 
=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-15-2007
On Sat, 15 Sep 2007 00:43:23 +0200, Charlie Gordon wrote:
> "Harald van D?k" <(E-Mail Removed)> a écrit dans le message de news:
> fcemg2$ruk$(E-Mail Removed)1.ov.home.nl...
>> Implementations are allowed to define the behaviour on signed integer
>> overflow as an extension, but as far as I'm aware, most don't. If you
>> try it, usually, you'll see the wraparound you probably expect, but you
>> shouldn't rely on it unless it's documented, or your code may well end
>> up broken for newer versions of the compiler. This happened quite a bit
>> with GCC 4.2, and will probably occur again with different versions or
>> different compilers.

>
> Come on, what gcc target does not do simple wrap around on signed
> integer overflow ?


All of them, regardless of the behaviour of the underlying hardware. The
standard allows

int f(int x) {
return x + 1 > x;
}

to be optimised to

int f(int x) {
return 1;
}

and gcc does exactly that.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      09-15-2007
On Fri, 14 Sep 2007 19:31:09 +0200, "Charlie Gordon"
<(E-Mail Removed)> wrote in comp.lang.c:

> "Harald van D?k" <(E-Mail Removed)> a écrit dans le message de news:
> fcecig$2a9$(E-Mail Removed)1.ov.home.nl...
> > On Fri, 14 Sep 2007 07:34:05 -0700, Spiros Bousbouras wrote:
> >> #include <stdio.h>
> >>
> >> int main(void) {
> >> int i ;
> >>
> >> for (i=1 ; i != 0 ; i++) ;
> >> printf("Finished !\n") ;
> >> return 0 ;
> >> }
> >>
> >> Assume that a compiler is clever enough to realise that the above will
> >> eventually overflow i. Can it refuse to produce an executable on that
> >> basis ?

> >
> > Yes, but only because it can be proven that the loop will always execute.
> > If the function were named differently, and the compiler could no longer
> > prove it would be called unconditionally, then the compiler would have to
> > accept the code.

>
> if i was defined as an unsigned variable, the behaviour is well defined and
> I see no reason for the compiler to complain. If it is smart enough, it
> will optimize to loop away.
>
> With i as an int, it depends on the implementation whether the behaviour is
> undefined or not. The compiler could detect the inevitable integer overflow


Signed integer overflow or underflow is always undefined.

> and complain about it, or even refuse to produce an executable, but on most
> architectures the behaviour is defined and the loop can be optimized away.


No, in C it is always undefined behavior. The result may be
predictable and repeatable on a particular platform, but that doesn't
make it defined.

Predictable and repeatable results are one possible consequence of
undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      09-15-2007
On 15 Sep, 00:47, Flash Gordon <(E-Mail Removed)> wrote:
> Note that some processors (the ones I know are DSPs have the ability to
> limit at maximum +ve/-ve.


What does "ve" mean ? Neither dictionary.com nor
acronymfinder.com gave me a definition which makes
sense in this context.

outOfTopic {
What does it mean for a DSP to "limit" ? I've
never seen "limit" being used as an intransitive
verb.
}

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-15-2007
Charlie Gordon wrote:
> "CBFalconer" <(E-Mail Removed)> a écrit:
>> Charlie Gordon wrote:
>>>

>> ... snip ...
>>>
>>> Come on, what gcc target does not do simple wrap around on signed
>>> integer overflow ?

>>
>> At least any that implement overflow traps. I can set up the 8088
>> to do just that.

>
> Yes, but do you have an OS or even a program that runs in this mode ?


Yes.

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

 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-15-2007
"CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> Charlie Gordon wrote:
>> "CBFalconer" <(E-Mail Removed)> a écrit:
>>> Charlie Gordon wrote:
>>>>
>>> ... snip ...
>>>>
>>>> Come on, what gcc target does not do simple wrap around on signed
>>>> integer overflow ?
>>>
>>> At least any that implement overflow traps. I can set up the 8088
>>> to do just that.

>>
>> Yes, but do you have an OS or even a program that runs in this mode ?

>
> Yes.


I'm impressed !
We should call that a DS8088

--
Chqrlie.


 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-15-2007
CBFalconer wrote:

> Charlie Gordon wrote:
>>

> ... snip ...
>>
>> Come on, what gcc target does not do simple wrap around on signed
>> integer overflow ?

>
> At least any that implement overflow traps. I can set up the 8088
> to do just that.
>

Or any target that has saturation arithmetic (the value is clipped in
the range of representable values).
Not all the world is a VAX!

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-15-2007
Spiros Bousbouras wrote, On 15/09/07 14:23:
> On 15 Sep, 00:47, Flash Gordon <(E-Mail Removed)> wrote:
>> Note that some processors (the ones I know are DSPs have the ability to
>> limit at maximum +ve/-ve.

>
> What does "ve" mean ? Neither dictionary.com nor
> acronymfinder.com gave me a definition which makes
> sense in this context.


+ve is a fairly well known abbreviation for positive in England and I
suspect a number of other countries. I'll let you work out what -ve is.

> outOfTopic {
> What does it mean for a DSP to "limit" ? I've
> never seen "limit" being used as an intransitive
> verb.
> }


With a 16 bit int
i = 0x7FFF;
i++; /* i is *still* 0x7FFF */
--
Flash Gordon
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-15-2007
Spiros Bousbouras <(E-Mail Removed)> writes:
> On 15 Sep, 00:47, Flash Gordon <(E-Mail Removed)> wrote:
>> Note that some processors (the ones I know are DSPs have the ability to
>> limit at maximum +ve/-ve.

>
> What does "ve" mean ? Neither dictionary.com nor
> acronymfinder.com gave me a definition which makes
> sense in this context.


"+ve" means "positive", "-ve" means "negative".

> outOfTopic {
> What does it mean for a DSP to "limit" ? I've
> never seen "limit" being used as an intransitive
> verb.
> }


I think it refers to saturation, meaning that a computation that
overflows yields an extreme value of the type. For example, INT_MAX +
1 would yield INT_MAX; INT_MIN * 3 would yield INT_MIN.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-15-2007
Spiros Bousbouras wrote:

> On 15 Sep, 00:47, Flash Gordon <(E-Mail Removed)> wrote:
>> Note that some processors (the ones I know are DSPs have the ability
>> to limit at maximum +ve/-ve.

>
> What does "ve" mean ? Neither dictionary.com nor
> acronymfinder.com gave me a definition which makes
> sense in this context.


+ve is shorthand for 'positive'. -ve is shorthand for 'negative'.
The sign in front of the ve is significant here.

>
> outOfTopic {
> What does it mean for a DSP to "limit" ? I've
> never seen "limit" being used as an intransitive
> verb.
> }


It is also known a saturating arithmetic.
With saturating arithmetic, the following holds:
int foo = INT_MAX;
foo++;
assert(foo == INT_MAX);
(and similar for decrement of lowest value).

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The eventual end of crappy lithium batteries? RichA Digital Photography 36 10-31-2009 05:32 AM
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
undefined behaviour Dan Cernat C++ 1 11-11-2003 04:49 PM



Advertisments