spinoza1111said:
Fred said:
On Jul 30, 10:58 am, Antoninus Twink <
[email protected]>
wrote:
On 30 Jul 2009 at 14:37, Bill Cunningham wrote:
Something Richard Heathfield said once very much sticks out in
my mind. If code is written properly there is no need (or
rarely) for casts.
As with most things Heathfield says, this needs to be taken with
a pinch of salt. As is typical for Heathfield, this statement is
pure polemic to push his "Sola ISO C" fundamentalism.
There are many places where casts are essential: for example, to
implement polymorphism in object-oriented C programming or for
type punning. There are also many places where casts are useful:
for example, casting a uint64_t to a uint32_t is a simple way to
reduce it mod 2^32.
Also, it is difficult to use qsort without using cast.
No, it's easy (see below).
Don't make the mistake of believing Antoninus Twink. Casts are
almost never a good idea, and the places where they are a good idea
are not the places you'd think.
Here is an example of using qsort without a cast:
#include <stdlib.h> /* for qsort */
#include <string.h> /* for strcmp, used in comparison function */
struct bigramfreq_
{
char bigram[3];
unsigned long freq;
};
typedef struct bigramfreq_ bigramfreq; /* the things we're sorting
*/
/* comparison function */
int compbigramfreq(const void *vp1, const void *vp2)
{
const bigramfreq *p1 = vp1;
const bigramfreq *p2 = vp2;
int diff = (p1->freq < p2->freq) - (p1->freq > p2->freq);
if(diff == 0)
{
diff = strcmp(p1->bigram, p2->bigram);
}
return diff;
}
Since I no longer use C, regarding its use as criminal save for
recreational programming, I may have missed something here, but this
code is to me globally incompetent...so incompetent, in fact, as to
relabel competence itself and normalize deviance.
You've missed something here.
You declare a function which takes a pointer to ANYTHING,
and convert it
Yes. That's how qsort works. It's impossible to demonstrate qsort
without using qsort. It's impossible to use qsort without using a
comparison function. The comparison function *must* be of type
int(const void *, const void *). Since void pointers can't be
dereferenced, it is necessary to convert them to something that can.
Therefore, to demonstrate qsort it is necessary to do such a
conversion.
This is no excuse, that you're instantiating a qsort exit. You're
deliberately using an out of date and unsafe tool, and because it
forces you to use const pointers to void, you use these, I am sure,
elsewhere, and you use them blindly or to entrap.
You call this incompetently weak typing by an incorrect name, because
it is NOT polymorphism, it's anamorphism, shapeless code and a bug
waiting to happen. In C you do not have the right to use a sorting
tool at all and you need to write a separate sort for each data type,
because the children who designed this language never even thought
about sorting, and never realized that it is not a problem that can be
solved safely, correctly and maintainably in a non-OO language,
because sorting IS polymorphic. It's a set or class of problems and as
such its solution cannot be properly or elegantly expressed in a
language without the ability to declare new types (and C's typedef and
struct are not able to declare types orthogonally to built-in types:
such types will forever be weak shadows).
You've also deceptively or foolishly made yet another false claim as
is your alternately deceptive or foolish wont. You say the code does
not use a cast. You're lying or just stupid (and this does not
logically exclude the strong possibility that you're a stupid bloody
liar, being || and not XOR). But in const bigramfreq *p1 = vp1, vp1 is
CASTED to a bigramfreq pointer!
I concede that I did not see you were using qsort. I never used this
tool because of its apparent lack of safety and use of void pointers,
and creeps like you like to snigger at small errors while you make the
big ones, starting with your idiotic advocacy of C. I am certain that
following qsort, you use const void pointers whenever you are in the
mood, and I believe that your code provides systematically wrong
answers in an undetected fashion. I am certain that the clerks and
secretaries forced to use your code are reduced to tears as they are
screamed at by managers to get results from it.
On the contrary, I know perfectly well that I'm getting pointers to
bigramfreq structures. See the qsort call.
There's nothing to prevent the sort exit from being called by new
code. You know only a narcissistic fantasy, a fetishised and
hypostatized Platonic Idea of your code uninterfered with by
maintenance programmers and fed on nothing but the expected inputs.
When that fantasy is deconstructed you proceed to destroy people.
What bug do you think needs debugging? I see none in the code I
posted.
Modern languages don't let you sort? I find that very hard to believe.
They allow the programmer to declare objects as compareable.
On the contrary, it works just fine, with no anomalies whatsoever.
Unless someone calls the qsort exit from another point in your code. I
have said before that one feature of a bad programming language is
that it makes smart people only seem stupid, and stupid bonehead thugs
like you seem smart: indeed, this is why you support C. John Nash
wrote beautiful C code but abandoned it for Mathematica at the
earliest possible moment because of this, whereas uneducated computer
thugs like you insist on using out of date languages to trip up
intelligent people and make you seem smart.
I don't care about your chimerae, no.
I prefer to generate right answers at high speed.
Then why do you use an out of date and unsafe language? Dijkstra
apparently found C beneath contempt in a *tour d'horizon* of
programming and its languages at
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD12xx/EWD1284.html.
He mentions Fortran because Fortran, in its own crude way, to him
broke ground. He does not mention C because C ignorantly swiped ideas
from Algol and proceeded to become a monstrosity.
Even if your code is correct, it is unmaintainable, since you cannot
force the next programmer to know all your tics and quirks.