A note for C programmers

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: A note for C programmers

Post by bob »

Food for thought. The English language is well-defined. Here's an online thesaurus lookup for unspecified.

Synonyms for unspecified
adj not specified

* undefined
* undetermined
* general
* unmentioned
* vague

Pretty sad when a standards committee begins to redefine commonly used words to mean something other than their usual meanings...
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: A note for C programmers

Post by hgm »

bob wrote:Just because the standard says "undefined" doesn't mean there is "no result". If you understand integer overflow, then causing it is not ALWAYS a bug.
Micro-Max actually depends on integer overflow, adding the same value 3*256+6 to the (char) pieceType to turn Pawns into Queens, as well as to the (int) score, to turn the value of a 7th-rank passer into that of a Queen.

But there is a risk: on hardware that uses 1-complement encoding. You might not get the same value as on a machine that uses 2-complement. But then again, you might not care (e.g. for calculating a hash).
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: A note for C programmers

Post by bob »

hgm wrote:
bob wrote:Just because the standard says "undefined" doesn't mean there is "no result". If you understand integer overflow, then causing it is not ALWAYS a bug.
Micro-Max actually depends on integer overflow, adding the same value 3*256+6 to the (char) pieceType to turn Pawns into Queens, as well as to the (int) score, to turn the value of a 7th-rank passer into that of a Queen.

But there is a risk: on hardware that uses 1-complement encoding. You might not get the same value as on a machine that uses 2-complement. But then again, you might not care (e.g. for calculating a hash).
Obvious question is, what hardware uses anything other than 2's complement for negative numbers? I've been programming since '66, and other than decimal machines from that era, every processor I have programmed since has used binary numbers and 2's complement for integer arithmetic.
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: A note for C programmers

Post by syzygy »

bob wrote:Pretty sad when a standards committee begins to redefine commonly used words to mean something other than their usual meanings...
I don't believe this is the first time you ever had a look at a standard.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: A note for C programmers

Post by Rein Halbersma »

bob wrote: I didn't "think" I understood strcpy(). I DID understand strcpy(). I STILL understand strcpy(). take a look at the source. Apple made an arrogant and ill-conceived decision that has brought them a lot of flak from many different directions, just as the glibc guys did with memcpy().
The C Standard is a precise technical specification (and not a dictionary where similar but slightly different terms can be loosely used as exact synonyms) on how to translate and implement a list of programming syntax constructs for an abstract machine into concrete assembly instructions for a concrete machine. Your particular machine and your particular library implementation might do things (signed integer overflow, memcpy, strcpy) in a perfectly well-defined and well-specified way, but that still leaves the compiler leeway in translating your C code to either those or to some other concrete machine instructions.

If you really feel that strongly about it, the proper way is to submit a technical proposal to the C Standard Committee on how to limit the particular piece of undefined or unspecified behavior you find disagreeable. I bet there will be others with a different environment that benefit from the current compiler freedom, and such a proposal would be unlikely to get accepted. The other alternative is to program those offending pieces in assembly for your particular machine.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: A note for C programmers

Post by AlvaroBegue »

The standard defines "unspecified behavior", not "unspecified". Similarly with "undefined behavior". You may not like these names, but they are used every day by a lot of people.

"Undefined behavior" means it works when you first run it, it passes all your tests and it explodes in your face when you show it to your boss or your most important customer. :)

Although you are generally right that people shouldn't go around breaking code that has worked forever, I recommend you just fix the code and move on.
User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: A note for C programmers

Post by michiguel »

bob wrote:Food for thought. The English language is well-defined. Here's an online thesaurus lookup for unspecified.

Synonyms for unspecified
adj not specified

* undefined
* undetermined
* general
* unmentioned
* vague

Pretty sad when a standards committee begins to redefine commonly used words to mean something other than their usual meanings...
The language of the C standard is perfect. Specify and define do not have identical meanings, and they used it correctly. Specify involves explicit numeration, define involves probably an explanation and a description of the limits..

Miguel
mvk
Posts: 589
Joined: Tue Jun 04, 2013 10:15 pm

Re: A note for C programmers

Post by mvk »

Here is a quote:
"If you fail to follow normal programming practices, and it blows up when you run the thing, that's hardly something to whack the vendor about. After all you _can_ put your foot under a running lawnmower, but should you do so, you don't have much reason to complain about the result. This is the same kind of thing. You _can_ do some things, but the question is _should_ you do them and if you do, who is responsible?"
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: A note for C programmers

Post by bob »

syzygy wrote:
bob wrote:Pretty sad when a standards committee begins to redefine commonly used words to mean something other than their usual meanings...
I don't believe this is the first time you ever had a look at a standard.
No. it isn't. I just can't see taking synonyms and then re-defining them to mean different things, when by definition, they mean the same thing.

The C standard has been problematic. As is/was C++. As is/was FORTRAN-66, FORTRAN-77, FORTRAN-90, etc.

You can't specify everything, for certain. Those things simply have to be left out. But not by voodoo where you say "undefined" which makes it perfectly OK for the compiler writer to do anything at all, as opposed to just doing the best thing he can do there. At least some take undefined to mean exactly that. I don't. Where would checksum be with integer overflow causing an abort, for example?
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: A note for C programmers

Post by bob »

AlvaroBegue wrote:The standard defines "unspecified behavior", not "unspecified". Similarly with "undefined behavior". You may not like these names, but they are used every day by a lot of people.

"Undefined behavior" means it works when you first run it, it passes all your tests and it explodes in your face when you show it to your boss or your most important customer. :)

Although you are generally right that people shouldn't go around breaking code that has worked forever, I recommend you just fix the code and move on.
Undefined behavior does NOT mean it will explode. Not one single instance of strcpy() will break on the example I gave where you do strcpy(st, st+n). Integer overflow does NOT make checksum break.