a[++j]=a[j]+a[j+1];

C

Chris Dollin

Naresh said:
#include<stdio.h>

void main(void)

`main` should return `int`. The behaviour of your
code is undefined.
{
int k = 0;

k = k++;

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

It gives me expected outcome as 1 on msvc 6 without any warnings or
errors. Then, why should we call it as undefined behaviour.

Because it /is/ undefined behaviour - the standard does not specify
any particular behaviour for this expression (`k = k++`). An implementation
may issue a warning, it may reject the code, it may print 17, it may
delete your files.
Isnt it ok
to consider it as two statements as

k = k + 1;
k = k;

It's also OK to consider it as the two statements:

k = -1;
k = -2;

Or as a double-effect machine instruction [1]

mov k k | add k k #1

which does both operations in parallel and, if it so happens that
the target register is the same in both instructions, fills it
with the bitwise OR of the results. This gets your expected result,
but for

int k = 1;
k = k++;

will put 3 into k.

Of course a later architecture, noting that allowing multiple
writes to the same register was silly and that no compiler
would generate it, arranged that such instructions were
co-processor invocations to co-processor `k`, and now your
program attempts to do an FFT where the `k #1` part of the
instructions specifies the array address and breaks because
the address is illegal.

BOOM tomorrow. There's always a BOOM tomorrow.

[1] For an architecture which I have made up, but which isn't
necessarily stupid.
 
F

Flash Gordon

Naresh said:
Abdo Haji-Ali wrote:


If this cant be confirmed on all the compilers then is that the problem
or loophole in C standard (like undefined behavior) or a bug in that
compiler? Compilers/assemblers should go in accordance with the
standard not the vice-versa. This cooresponding ASM clearly works in
accordance to standard, at least it looks to me. And i m justified in
considering asm because C is middle level language.

Just by saying that the behavior is undefined never makes it undefined.

If the C standard, the one and only document that defines what C is,
says something is undefined, then it is undefined. Period. Enod of
discussion.

In English, "wombat yellow banana thing purple of to in at but" is
meaningless, yet I can type it and my editor (which does spell checking)
doesn't complain. "Just by saying it is meaningless does not make it
meaningless".
I'm getting consistent results and the consistent opcode generation at
least for this piece of code.

"Every time I cross the street with my eyes closed and ear defender on
so I can't see anything nothing goes wrong."

Undefined behaviour is like that. It is not defined. Not defined does
not mean it will get you run over, but one day it might and you will
have no one but yourself to blame.

Even if we gave you a simple piece of code that behaves unpredictably on
all of our systems it could produce the same result every time you try it.
I'm still wondering how can it be undefined. A piece of code, which
behaves erratic using these simple assignment, will help me to
understand it better. I should get conflicting (undefined) results for
the same equation. I've msvc6 installed on win2000.

No, with undefined behaviour you can get *anything*. That includes the
same behaviour every time you do it. However, tomorrow when you run it
your computer could hack in to the CIA's computers and add an entry
saying that you are the most dangerous terrorist on the planet. Or it
could cause demons to fly out of your nose.

Please don't quote peoples signatures, only quote the material you are
commenting on.
 
R

RSoIsCaIrLiIoA

what is supposed to do

int pto(void)
{int a[50]={0};

a[0]=1; a[1]=2; a[2]=3; a[3]=4;
j=0;
a[++j]=a[j]+a[j+1];
return a[j-1];

no, i would like to say here
"return a[j]"
 
K

Keith Thompson

Naresh said:
Thank you, i just understood, that

"b]etween the previous and next sequence point an object shall have its
stored value modiÞed at most once by the evaluation of an
expression."

and hence even simple assignment is considered as modification. Hence
forth, according to standard, u can never have two assignments within a
single statement (like i = i++;), thou, it may invariably be possible
for all the existing compilers ( or at least msvs6 on win2000) to
assign it properly and consistently.

There is no proper and consistent behavior for "i = i++;". As the
standard joke goes, it can legally make demons fly out of your nose.
(This obviously isn't possible, but if it did happen you couldn't
complain that the compiler is non-conforming.)

Even if it were well defined, it would probably mean one of two
things: increment i and then assign the old value of i to i (which
does nothing), or assign the old value of i to i and then increment i
(which just increments i). Either way, there would be no reason to do
something convoluted like "i = i++;". If you just want to increment
i, use "i++;". If you want to do nothing, do nothing.
 
K

Keith Thompson

[...]

(Context: "i = i++;".)
It has not done anything different yet, i believe it wont do anything
different even if i show it to president of US or pull out a gun at the
screen!
Thou' now, i understood ur predefined path of "go in accordance to the
standard." Yes, it is undefined.

This is *not* just a theoretical point. Optimizing compilers
rearrange expression evaluations to generate more efficient code. The
compiler is specifically allowed to *assume* that the code does not
invoke undefined behavior. If this assumption is incorrect (because
you lied to the compiler), the rearrangement can cause arbitrarily bad
things to happen.

Such things might not happen with something relatively simple like
"i = i++;", but each optimizing compiler is different, and you can't
assume *anything* about what can go wrong.

"If you lie to the compiler, it will get its revenge." -- Henry Spencer
 
M

Martin Ambuhl

Naresh said:
#include<stdio.h>

void main(void)
{
int k = 0;
k = k++;
printf("\n %d", k);
}

It gives me expected outcome as 1

There is no such thing as "expected outcome" from your program.
on msvc 6 without any warnings or
errors.

Then your compiler is broken or you have failed to properly set the
diagnostic level. Consider the diagnostics given by gcc below. They
are, by the way, *mildly* worded. These "warnings" flag clear errors:

#include <stdio.h>

void main(void)
/* warning: return type of 'main' is not 'int' */
{
int k = 0;
k = k++;
/* warning: operation on 'k' may be undefined */
printf("\n %d", k);
}
Then, why should we call it as undefined behaviour.

Because it is undefined, and your are misusing both the C programming
language and your compiler.
Isnt it ok
to consider it as two statements as

k = k + 1;
k = k;

No. If you mean that, write it, although a simple "k++;" statement
would do better.
It simply looks like Ra (register) = Ra + 1 and then Ra = Ra.

It simply looks like broken code from someone who thinks the return type
of main is void.
 
H

Herbert Rosenau

Thank you for being patient; i know what is post and pre increment and
so does the compiler, at least msvc6 knows this for sure, which it
implies very very clearly in the generated asm, atleast i can infer so.
I'm not imbibing asm in C but just showing how msvc6 assembler is
working after no compilation errors.


If this cant be confirmed on all the compilers then is that the problem
or loophole in C standard (like undefined behavior) or a bug in that
compiler? Compilers/assemblers should go in accordance with the
standard not the vice-versa. This cooresponding ASM clearly works in
accordance to standard, at least it looks to me. And i m justified in
considering asm because C is middle level language.

Just by saying that the behavior is undefined never makes it undefined.
I'm getting consistent results and the consistent opcode generation at
least for this piece of code.

I'm still wondering how can it be undefined. A piece of code, which
behaves erratic using these simple assignment, will help me to
understand it better. I should get conflicting (undefined) results for
the same equation. I've msvc6 installed on win2000.

That is because the compiler gets all fredom it needs from the
standard to create best code for its target mashine.

Read the fucking standard instead to cry "my compiiler what is the
only existent on the only hardware architecture prove that my mind is
right, so any other is wrong or buggy".

Your mind is crazy by NOT reading the standard and not able to
understund what 'undefined behavior' really means.

Even a = ++a; or a = a++; is undefined behavior because it modifies
the same variable more than once between 2 sequence points. It is the
decision of the compier if it will execute the increment before or
after it has done the assignment, but it will have done that before
the sequence point is reached.

You would never get a C programmer who is worth to get a job or gets
fired more quickly that you have gotten the job when you are unable to
accept that the standard is more clever than you will be in 1000
years.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
H

Herbert Rosenau

Such things might not happen with something relatively simple like
"i = i++;", but each optimizing compiler is different, and you can't
assume *anything* about what can go wrong.

Don't say that! Some years ago I had to port an 16 bit app to be 32
bit. It was easy don because the system API was not changed other than
to deliver 32 bit entries instead of 16 bit ones, but one point was
annoying me: the original developer was really lazy he used on some
points undefined behavior like i = i++; instead of i = i + 1; or
simply i++; and as the 32 bit compiler was using a better optimasion
even in debug mode than the old 16 bit one in memory optimasion the
program gaves strange results. It was hard work to find out why the
data was significant irreparable defective it had produced. The
program was only about 20 compilation units with each of 200 - 250
lines of code in it.

The OP seems to be one of that kind of peoples who have decided that
the know of all and anything and unable to learn. He laughts on you
when you tries to help him to learn what 'undefines behavior means'.
He has prepared himself already to be plonkeked based on his learn
resistence.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
K

Keith Thompson

the answer is 3

No.

Without context, it's difficult to tell what you're talking about.
Not everyone can easily see the article to which you're replying.
Please read <http://cfaj.freeshell.org/google/>. (If you had taken a
look a this newsgroup before posting, as you should have, you would
already know that.)

But since you're using groups.google,com, and you *can* easily see the
rest of the discussion, you should know that the program in the
orginal article invokes undefined behavior. There is no such thing as
"the answer".
 
J

John Bode

Naresh said:
#include<stdio.h>

void main(void)
{
int k = 0;

k = k++;

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

It gives me expected outcome as 1 on msvc 6 without any warnings or
errors. Then, why should we call it as undefined behaviour.

Because there's no guarantee that the result will be 1 on another
platform, or on the same platform with a different compiler, or the
same platform and compiler with different options.

Because I was bored one day, I wrote a series of programs that went
through all the permutations of j = i++ + i++, j = i++ + ++i, etc., and
compiled them for both Windows NT (using Visual Studio) and MacOS
(using MPW). On both platforms, at least one permutation gave a
"wrong" result, and the "wrong" results were different between the two
platforms.

When we say the behavior is undefined, we mean that the compiler is
under no obligation to generate a correct result. Just because you get
the result you expect doesn't mean that the code is correct.
Isnt it ok to consider it as two statements as

k = k + 1;
k = k;

Strictly speaking, no. With the expression "k = k++", there's no
guarantee that the side effect will be applied before the assignment.

In short, for expressions like "k = k++", there is *no* correct result.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,177
Messages
2,570,953
Members
47,507
Latest member
codeguru31

Latest Threads

Top