Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it invoking UB ?!

1 view
Skip to first unread message

Debanjan

unread,
Oct 4, 2009, 6:36:51 AM10/4/09
to
A code snippet looks likes this :
{
int x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},result[5]={0,0,0,0,0};
int j=0;

while(j++<5)
result[j]=x[j]-y[j];

cout<<"contents of array :\n";
j=0;

do
{
cout<<'\t'<<x[j]<<'\t'<<y[j]<<'\t'<<result[j]<<'\n';
j++;
}while(j<5);

}

It's giving some strange outputs in some compiler.For instance check
this : http://codepad.org/vO6QYUFa.

I think while(j++<5) is invoking UB is it ? I have doubts.

Moi

unread,
Oct 4, 2009, 6:41:18 AM10/4/09
to
On Sun, 04 Oct 2009 03:36:51 -0700, Debanjan wrote:

>
> cout<<"contents of array :\n";

You can not shift by a character literal.

AvK

Debanjan

unread,
Oct 4, 2009, 6:58:22 AM10/4/09
to

I think you din't get that correctly,it's std::cout standard way for
console writing in C++.I forget that this in comp.lang.c This is the C
version which I posted in the above link too :

#include<stdio.h>

int main()


{
int x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},result[5]={0,0,0,0,0};
int j=0;

while(j++<5)
result[j]=x[j]-y[j];

printf("contents of array :\n");
j=0;
do
{
printf("\t%d\t%d\t%d\n",x[j],y[j],result[j]);
j++;
}while(j<5);

return 0;
}

Apologies for posting C++ here.

Seebs

unread,
Oct 4, 2009, 7:06:37 AM10/4/09
to
On 2009-10-04, Debanjan <debanj...@gmail.com> wrote:
> A code snippet looks likes this :

Ignoring your decision to use C++ output functions:

> int x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},result[5]={0,0,0,0,0};
> int j=0;
>
> while(j++<5)
> result[j]=x[j]-y[j];

This does indeed invoke undefined behavior. You are probably thinking
that the "j++", being a postincrement, gives you the "previous" value of j,
but by the time the "result=..." thing happens, j is 5, and you're both
modifying and accessing things outside the bounds of the arrays.

> I think while(j++<5) is invoking UB is it ? I have doubts.

Itself, no.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Debanjan

unread,
Oct 4, 2009, 7:11:13 AM10/4/09
to
On Oct 4, 4:06 pm, Seebs <usenet-nos...@seebs.net> wrote:

> On 2009-10-04, Debanjan <debanjan4...@gmail.com> wrote:
>
> > A code snippet looks likes this :
>
> Ignoring your decision to use C++ output functions:
>
> >    int x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},result[5]={0,0,0,0,0};
> >    int j=0;
>
> >    while(j++<5)
> >          result[j]=x[j]-y[j];
>
> This does indeed invoke undefined behavior.  You are probably thinking
> that the "j++", being a postincrement, gives you the "previous" value of j,
> but by the time the "result=..." thing happens, j is 5, and you're both
> modifying and accessing things outside the bounds of the arrays.
>
> > I think  while(j++<5) is invoking UB is it ? I have doubts.
>
> Itself, no.
>
> -s
> --
> Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Yes that why I said I had doubts.But then what the reason of the
strange output in some compilers ?! For instance please check that
link.

Debanjan

unread,
Oct 4, 2009, 7:32:52 AM10/4/09
to
On Oct 4, 4:06 pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-10-04, Debanjan <debanjan4...@gmail.com> wrote:
>
> > A code snippet looks likes this :
>
> Ignoring your decision to use C++ output functions:
>
> >    int x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},result[5]={0,0,0,0,0};
> >    int j=0;
>
> >    while(j++<5)
> >          result[j]=x[j]-y[j];
>
> This does indeed invoke undefined behavior.  You are probably thinking
> that the "j++", being a postincrement, gives you the "previous" value of j,
> but by the time the "result=..." thing happens, j is 5, and you're both
> modifying and accessing things outside the bounds of the arrays.
>

But in the code we are no where modifying the value of the array y ..
So Why the problem with y ? the result seems working fine.

Richard Heathfield

unread,
Oct 4, 2009, 7:49:42 AM10/4/09
to

Yes you can, because a character literal is of int type:

cout << '\01'; /* perfect legal */

But what you meant to say (that you cannot shift by a /string/
literal) is perfectly correct.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Ben Bacarisse

unread,
Oct 4, 2009, 7:55:38 AM10/4/09
to
Debanjan <debanj...@gmail.com> writes:

> On Oct 4, 4:06 pm, Seebs <usenet-nos...@seebs.net> wrote:
>> On 2009-10-04, Debanjan <debanjan4...@gmail.com> wrote:
>>
>> > A code snippet looks likes this :
>>
>> Ignoring your decision to use C++ output functions:
>>
>> >    int x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},result[5]={0,0,0,0,0};
>> >    int j=0;
>>
>> >    while(j++<5)
>> >          result[j]=x[j]-y[j];
>>
>> This does indeed invoke undefined behavior.  You are probably thinking
>> that the "j++", being a postincrement, gives you the "previous" value of j,
>> but by the time the "result=..." thing happens, j is 5, and you're both
>> modifying and accessing things outside the bounds of the arrays.
>>
>> > I think  while(j++<5) is invoking UB is it ? I have doubts.
>>
>> Itself, no.
>>
>> -s
>> --
>> Copyright 2009, all wrongs reversed.  Peter Seebach /
>> usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits,
>> religion, and funny
>> pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <--
>> get educated!

It's best to snip sig blocks.

> Yes that why I said I had doubts.But then what the reason of the
> strange output in some compilers ?! For instance please check that
> link.

Since the code does something undefined, any output you get can't be
explained by talking about the C code. What's more I would not spend
much time trying. If you feel you must know, look at the assembly
language and try to work it out from that but you won't learn much
from what one particular compiler does with one undefined program.

BTW, it is almost always best to use a for loop to iterate through an
array.

--
Ben.

Flash Gordon

unread,
Oct 4, 2009, 7:35:23 AM10/4/09
to
Debanjan wrote:
> On Oct 4, 3:41 pm, Moi <r...@invalid.address.org> wrote:
>> On Sun, 04 Oct 2009 03:36:51 -0700, Debanjan wrote:
>>
>>> cout<<"contents of array :\n";
>> You can not shift by a character literal.
>>
>> AvK
>
> I think you din't get that correctly,it's std::cout standard way for
> console writing in C++.I forget that this in comp.lang.c This is the C
> version which I posted in the above link too :
>
> #include<stdio.h>
>
> int main()
> {
> int x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},result[5]={0,0,0,0,0};
> int j=0;
>
> while(j++<5)

In your original post you asked if the above line invoked UB. It
doesn't, why did you think it would. However, it means the following
line is executed with j having values of 1,2,3,4,5, and the last of
these *does* invoke UB since you are reading off the end of x and y and
writing off the end of result.

> result[j]=x[j]-y[j];
> printf("contents of array :\n");
> j=0;
> do
> {
> printf("\t%d\t%d\t%d\n",x[j],y[j],result[j]);
> j++;
> }while(j<5);
>
> return 0;
> }

<snip>

For both loops it would be more sensible and simpler to use for loops.
--
Flash Gordon

Seebs

unread,
Oct 4, 2009, 8:29:09 AM10/4/09
to
On 2009-10-04, Debanjan <debanj...@gmail.com> wrote:
> Yes that why I said I had doubts.But then what the reason of the
> strange output in some compilers ?! For instance please check that
> link.

No, I will not check "that link".

UNDEFINED BEHAVIOR MEANS ANYTHING CAN HAPPEN.

Unless you are debugging the compiler itself, you don't need to know how
undefined behavior leads to a particular unusual outcome -- you just need
to know that undefined behavior can do ANYTHING. Even stuff that makes
no sense to you.

-s
--

Seebs

unread,
Oct 4, 2009, 8:30:12 AM10/4/09
to
On 2009-10-04, Debanjan <debanj...@gmail.com> wrote:
> But in the code we are no where modifying the value of the array y ..
> So Why the problem with y ? the result seems working fine.

ANYTHING CAN HAPPEN.

This does not mean "you might get one of two or three behaviors which are
totally obvious to you".

If you feel that something in y is being modified, one reason might be that
you modified space outside some other object, which happened to overlap
with where y was. Another reason might be that, well, anything can happen.

Jens Thoms Toerring

unread,
Oct 4, 2009, 8:57:53 AM10/4/09
to

But you are accessing a sixth element of the arrays 'x' and 'y'
which both don't exist and you are assigning to a sixth element
of the 'result' array which also does not exist, so you invoke
UB three times. The reason is that your test

while ( j++ < 5 )
....

won't stop the loop when j is 5 but only when j reaches 6 since
j is incremented only after the comparison is done. It's as if
you had written

while ( j < 5 )
{
j = j + 1;
...
}

If you don't see yet what's happening perhaps trying what

#include <stdio.h>
int main(void) {
int j = 0;
while (j++ < 5)
printf("%d\n", j);
return 0;
}

outputs will help.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

chad

unread,
Oct 4, 2009, 10:46:35 AM10/4/09
to
On Oct 4, 5:57 am, j...@toerring.de (Jens Thoms Toerring) wrote:

Here is what I get when I do something like while ( j++ < 5 )

[cdalten@localhost oakland]$ more ov.c
#include <stdio.h>

int main(void)
{

int j = 0;

while (j++ < 5) {
printf("%d\n", j);
}

printf("%d\n", j);

return 0;
}
[cdalten@localhost oakland]$ gcc -Wall ov.c -o ov
[cdalten@localhost oakland]$ ./ov
1
2
3
4
5
6
[cdalten@localhost oakland]$


However, I get different results when I change the loop such that

while ( j < 5 )
{
j = j + 1;
...
}

[cdalten@localhost oakland]$ more ov.c
#include <stdio.h>

int main(void)
{

int j = 0;

while (j < 5) {


j = j + 1;

printf("%d\n", j);
}

printf("%d\n", j);

return 0;
}
[cdalten@localhost oakland]$ gcc -Wall ov.c -o ov
[cdalten@localhost oakland]$ ./ov
1
2
3
4
5
5
[cdalten@localhost oakland]$


Why the discrepancy in the value after the while loop completes?

Debanjan

unread,
Oct 4, 2009, 11:13:28 AM10/4/09
to


while(j++<5){ ...}

Here j is checked with the value 5 if if true then the loop statements
will be executed after incrementing the value of j.'

while(j<5){ j = j+1; ... }

j is compared to 5 if true then only j is incremented in the loop
statements.

Hence the discrepancy.

Debanjan

unread,
Oct 4, 2009, 11:14:23 AM10/4/09
to
On Oct 4, 5:30 pm, Seebs <usenet-nos...@seebs.net> wrote:

> On 2009-10-04, Debanjan <debanjan4...@gmail.com> wrote:
>
> > But in the code we are no where modifying the value of the array y ..
> > So Why the problem with y ? the result seems working fine.
>
> ANYTHING CAN HAPPEN.
>
> This does not mean "you might get one of two or three behaviors which are
> totally obvious to you".
>
> If you feel that something in y is being modified, one reason might be that
> you modified space outside some other object, which happened to overlap
> with where y was.  Another reason might be that, well, anything can happen.
>
> -s
> --
> Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

hm,.. this clears my doubt. Thank you :)

James Kuyper

unread,
Oct 4, 2009, 1:11:33 PM10/4/09
to

How do you know that? The behavior is undefined, which means it's
entirely possible that the array y is being modified.

Moi

unread,
Oct 4, 2009, 6:41:48 PM10/4/09
to
On Sun, 04 Oct 2009 11:49:42 +0000, Richard Heathfield wrote:

> In <75d33$4ac87bce$5350c024$32...@cache100.multikabel.net>, Moi wrote:
>
>> On Sun, 04 Oct 2009 03:36:51 -0700, Debanjan wrote:
>>
>>
>>> cout<<"contents of array :\n";
>>
>> You can not shift by a character literal.
>
> Yes you can, because a character literal is of int type:
>
> cout << '\01'; /* perfect legal */
>
> But what you meant to say (that you cannot shift by a /string/ literal)
> is perfectly correct.

Yes, that was before the coffee started working.

BTW '\01' is an integer literal. (hint: sizeof)

TNX,
AvK

Richard Heathfield

unread,
Oct 4, 2009, 7:56:14 PM10/4/09
to

If you just mean its type is int, then I agree (see above, where I
point out that it has int type). If you mean it's not a character, I
disagree. (C's formal term is "integer character constant".)

0 new messages