Segfault City

K

Kenneth Brody

pete said:
I usually describe it as a "loose nut",
especially when describing automotive problems.

"The problem with your car is that
there's a loose nut behind the wheel."

AKA "PEBKAC".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
H

Herbert Rosenau

Now that it has an int type parameter,
how should it deal with a negative int value
like ('5' - 1 - (unsigned char)-1)?
You would have to learn what isdigit() does with what values. When c
is a digit the macro will give you a positive number between 0 and 9
(0x00 -0x09 in hex 00 - 011 in octal), inclusive. Else you gets a -1.


What else should go on?

--
Tschau/Bye
Herbert

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

Richard Heathfield

Dave Vandervies said:
Dave Vandervies (e-mail address removed)
The ASCII code is a 7-bit code, and chars must be at least 8 bits in size.
Therefore, there are no ASCII characters suitable for terminating a C
string. --Richard Heathfield in comp.lang.c

Did I really say that?

I crack me up. :)
 
B

Bill Pursell

Herbert said:
No, because that does NOT catch an error a programmer may have done.

It's not intended to catch an error. It is intended to clearly
document
the fact that the caller needs to verify the data, and to cause
failures
in debug mode to help bring that to the programmer's attention.
 
P

Peter Shaggy Haywood

Groovy hepcat Richard Heathfield was jivin' on Sat, 24 Jun 2006
02:22:02 +0000 in comp.lang.c.
Segfault City's a cool scene! Dig it!
[X-posted, and followups set]

I found something interesting on the Web today, purely by chance.

It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm
not sure which.

Oh, Richard, why did you have to drag that fool in here? Was
comp.lang.c getting too quiet for you? The Nilgewater over in
comp.programming is bad enough with letting it loose in here as well.
It was nice and quiet in here without him. But now look what you've
done!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

I know Nilges will pay no attention to this, and may start calling
me names and making weird claims about how I assasinate his character
(as if I needed to - he does an exemplary job of that himself) and
threatening to sue me for libel, but it has to be said, for the
newbies in here who might innocently but bafflingly mistake Nilges
(spinoza1111) for someone sane, if for noone else.

Ol' square-britches (e-mail address removed) was jivin' on 24 Jun 2006
11:02:58 -0700 in comp.lang.c.
Re: Segfault City's a bad trip! Dig it!
I'm not a C "expert" by your lights, but I am an expert programmer, and
the above is one of the silliest things I've ever seen posted on the
net. You may be a "recognized C expert",

You're the only one I see claiming to be an expert. And yet you have
shown what sort of expert you are. As the old joke goes, an "ex" is a
has-been, and a "spirt" is a drip under pressure.
but you can't program and
should not be permitted to write software or write about software. Here
is why:

(1) You call the code a "straightforward test for numerics" but it
isn't any such thing.

It isn't? Well, what is it, then?
It assumes that the library contains an isdigit()

Which it must, if it is a standard conforming C library for a hosted
environment, as you have been told numerous times already.
function and doesn't "test" at all. Instead, using isdigit() it checks
for a digit (which makes your statement wrong and a lie because isdigit

What's wrong about it? He's checking for a digit by calling
isdigit(). How is that wrong?
does the work you claim "your" code does) and then converts the digit
to a number.

Indeed. That is the purpose of the functions Richard posted.
(2) Earlier this evening I mistook your code to fail if the character
value exceeded '9' because your code was so terribly wrong (in saying

No, his code is so terribly correct.
it was something it was not) that I actually overlooked (1);

What did the code say it is that it isn't?
you are
not making the test you say you are making.

Yes he is. What part of that don't you understand? He is checking
for a digit by calling isdigit(), whose purpose it to check for a
digit.
I was in a hurry because
I'm wasting my time replying to you.

Apparently so, since you refuse to listen and learn. But if you feel
that way, why are you replying?
To call the above code "straightforward" is obscene.

If by "obscene" you mean "correct" or "truthful" or "right".
Otherwise, what part of it do you not find straightforward?
You need to code
what you mean,

Indeed. He did. Just because you're so lacking in intellegence that
you can't understand it is no reason for you to fly off the handle.
Instead, ask about that which you don't understand, and someone will
endeavour to educate you.
and the minimal assumption you can make about digits is
that their value, when converted to an integer (whether byte, unsigned
or other) is inclusively between that of the integer value of '0' and
that of '9'.

Good for you! You got something correct. See; it's not all that
hard, is it? You and Richard and everyone else here who knows the
slightest thing about C are in agreement. The digit characters are
adjacent and in numerical sequence in the source and execution
character sets. This much has been guaranteed by the standard.
Now, in what way does that differ from what Richard has been saying
all along?
The value of letters in EBCDIC did not conform to this, but this was a

Of course not. Letters are not digits. The standard does not
guarantee anything about the order or adjacentness of letters in
either the source character set or execution character set.
mistake caused by the fact that Herman Hollerith, the designer of the
IBM punched card, needed "zone" bits.

And this has what to do with standard C?
It was fixed in ASCII. In all

ASCII which was designed *before* EBCDIC? So something that was
designed after something else is fixed by the thing after which it was
designed?
And the question remains, what does this have to do with standard C?
extant coding systems except perhaps obscure ones, digit values are
adjacent.

Correct (assuming "coding systems" means character sets, of course).
At least we can say that all character sets supported by the C
language have adjacent and sequential digit sets.
We're all in agreement on this, so why do you keep harping on about
it?
You may believe that a "standard" C programmer would always use isdigit
to ensure his code worked on non-ASCII, non-EBCDIC systems.

I don't know why you would think that. The fact that isdigit() works
for ASCII and EBCDIC and all other character sets that support the C
language is not really a major consideration in choosing that over an
expression like c >= '0' && c <= '9'. The fact that it is easier to
use and possibly more efficient is a major consideration.
The isdigit
function would then I suppose completely conceal the machine and code
dependent test and here, the code would be transportable between ASCII
and EBCDIC machines.

Indeed it would be, and to implementations that use other character
sets too. So would it be if you had used an expression like c >= '0'
&& c <= '9'. However, there is no need to do the latter when the
former suffices.
This would be a good thing. The problem is that actual working C
programmers can't be sure of having a fully conforming implementation

That's true. However, the chances of having a conforming
implementation are far greater than those of having a non-conforming
one. Conforming implementations exist for a vast range of systems.
Non-conforming implementations are few and far between. And even they
might have an isdigit() function.
for legacy code, and don't waste time if they're smart making legacy
programs conform to your favorite standard. And, you intentionally
mislead them, as a self-appointed C expert, by saying that your code is
a "straighforward test for numerics" when it is not.

Huh?
In your code, assuming that the C environment the programmer is using
(which doesn't have to be standard) has an isdigit, you don't break if
the digit value is greater than the value of 9. However, your code,
starting with your claim, is a lie, and you are posting (perhaps as a
paid agent) disinformation in a campaign of character assassination

So, there's someone out there paying Richard money to spread
disinformation? Who would do that and why? Really, you are a fool!
similar to that you conducted against Herb Schildt. **** you.

Weapons of the ignorant: outrageous claims and foul-mouthed personal
abuse.
You're a code monkey and on the job, if this is your style, your
interest is solely in spreading confusion, and, probably, office
politics and character assassination.

You know you're only making yourself look bad, don't you? Oh no, of
course you don't. Silly question! Well, allow me to enlighten you. You
*are* only making yourself look bad.
As much as you are, and probably more.


Plenty. What he's saying is that your code is unnecessarily obscure and
difficult to read. That's the mark of a bad programmer, unless your intent
is to win the IOCCC.

Obscure? Difficult to read? Give me a break. You say above that "this
would be a straightforward TEST FOR NUMERICS" [emphasis mine] and then
you use isdigit() to do the heavy lifting.

In what way is it obscure or hard to read to use a simple function
with a fairly obvious name to carry out a simple task?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
O

Old Wolf

Richard said:
Thirdly, a call to isdigit() is far clearer

I find

isdigit( (unsigned char) ch )

uglier and harder to read than

( ch >= '0' && ch <= '9' )
as well as being quicker to type,

The isdigit version has more characters, although it is subjective as
to which is easier to type.
and it may even run a bit faster, too.

The compiler could easily optimise either one.

FWIW I've also worked on embedded environments where the
ctype functions don't work (the headers do a table lookup, but
the table never got initialized to meaningful values).

NB. This message should not be construed in any way to be
a defence of the Nilgewater. In fact I thought that article was
satire, until I saw its author's posts on this thread.
 
O

Old Wolf

Chris said:
Please point out a C implementation that's in any significant amount of
use (i.e., not a toy implementation built by a student for a compilers
class, or something like that) that fails to implement isdigit.

There's an EFTPOS terminal in China that has ctype.h defining
isdigit as a table lookup macro, but the table is never initialized.
Whatever code they use to set up the C runtime environment is
obviously munged. I believe there are tens of thousands of these
devices in use.
 
A

av

I find

isdigit( (unsigned char) ch )

uglier and harder to read than

( ch >= '0' && ch <= '9' )


The isdigit version has more characters, although it is subjective as
to which is easier to type.


The compiler could easily optimise either one.

FWIW I've also worked on embedded environments where the
ctype functions don't work (the headers do a table lookup, but
the table never got initialized to meaningful values).

i wrote each one of my 2 127+127 tables number in 2 static arrays of
ints. no initialization
 
M

Malcolm

Old Wolf said:
I find

isdigit( (unsigned char) ch )

uglier and harder to read than

( ch >= '0' && ch <= '9' )
The need to cast is inexcusable in a library function.
In fact I always omit it, saying that the bug is in the standard rather than
my code. There are dangers in going down this route, of course.
 
K

Keith Thompson

Malcolm said:
The need to cast is inexcusable in a library function.
In fact I always omit it, saying that the bug is in the standard rather than
my code. There are dangers in going down this route, of course.

Indeed there are. If I run your code and it breaks because of this, I
might be annoyed with the standard, but I'll blame you.

Do you also refuse to use a cast for something like this?

int *ptr;
...
printf("ptr = %p\n", (void*)ptr);

I agree that the need to cast an argument to isdigit() is annoying.
How would you re-design it so the cast isn't necessary, without
breaking existing code? (Personally, I think I'd require plain char
to be unsigned, but I don't think that's going to fly.)
 
M

Michael Mair

Keith said:
Indeed there are. If I run your code and it breaks because of this, I
might be annoyed with the standard, but I'll blame you.
I agree that the need to cast an argument to isdigit() is annoying.
How would you re-design it so the cast isn't necessary, without
breaking existing code? (Personally, I think I'd require plain char
to be unsigned, but I don't think that's going to fly.)

You could wrap them using macros; Jordan Abel posted a very
good solution (no problem with side effects) in
<[email protected]>
(I remember because I was rather dense in the ensuing
discussion).
,---
magic safe macro for isalpha:

#define ISALPHA(x) isalpha(sizeof(x)==1?(unsigned char)(x):(x))
`---


Cheers
Michael
 
P

pete

Michael Mair wrote:
You could wrap them using macros; Jordan Abel posted a very
good solution (no problem with side effects) in
<[email protected]>
(I remember because I was rather dense in the ensuing
discussion).
,---
magic safe macro for isalpha:

#define ISALPHA(x) isalpha(sizeof(x)==1?(unsigned char)(x):(x))
`---

I don't think that's best.

For
#define NEG_A ('A' - 1 - (unsigned char)-1)

putchar(NEG_A);
outputs 'A'

isalpha((unsigned char)NEG_A)
returns 1

ISALPHA(NEG_A)
is undefined.
 
K

Kenneth Brody

pete said:
I don't think that's best.
[... snip convoluted example ...]

Why not just ISALPHA('A')?

Remember, sizeof('A') is not necessarily 1.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Michael Mair

pete said:
Michael Mair wrote:



I don't think that's best.

For
#define NEG_A ('A' - 1 - (unsigned char)-1)

putchar(NEG_A);
outputs 'A'

isalpha((unsigned char)NEG_A)
returns 1

ISALPHA(NEG_A)
is undefined.

True. See the context in the thread from which this comes.
I have yet to see a better solution for the preconditions given
there.

Cheers
Michael
 
F

Flash Gordon

pete said:
Kenneth said:
pete said:
Michael Mair wrote:

You could wrap them using macros; Jordan Abel posted a very
good solution (no problem with side effects) in
<[email protected]>
(I remember because I was rather dense in the ensuing
discussion).
,---
magic safe macro for isalpha:

#define ISALPHA(x) isalpha(sizeof(x)==1?(unsigned char)(x):(x))
`---
I don't think that's best.
[... snip convoluted example ...]

Why not just ISALPHA('A')?

Because everyone already knows that 'A' is alpha.

Of course, the hole point of the example was to show how the macro could
fail, so failing to use the macro or failing to use it in a convoluted
way would have failed to make the point.
sizeof('A') is either never 1 or almost never 1.

sizeof(int)==1 is very common for DSP processors which I believe to be a
significant market. At least, I have more DSP processors in my house
than PCs, and I have more PCs than most people. Of course,
sizeof(int)==1 is very rare on hosted environments.
 
E

ena8t8si

Chris said:
... and also to /cast to unsigned char/. Grrr. I keep forgetting that bit.

No cast necessary:

int charToNumber( unsigned char c )
{ return isdigit(c) ? c-'0' : -1; }
 
C

Chris Dollin

No cast necessary:

int charToNumber( unsigned char c )
{ return isdigit(c) ? c-'0' : -1; }

That changes the interface of the function. That might be a good
design choice, but it's not the one I'd picked.
 
M

Mark F. Haigh

I no longer have a programming supervisor because over the course of a
successful thirty year career in programming including assistance to
John Nash, book publishing, and operating system development (VM/CMS
modifications and bootstrap loaders) I began to realize in recent years
that American programming culture is decadent, as is the culture of
places, like Richard's UK, in thrall to American models.

I realized that more and more programming "supervisors" are in fact
dependent on self-appointed experts like Richard who deliberately tell
lies, spread confusion and bully others.

Please go away. Your code sucks and you got called on it. So, please,
take it like a man, fix the code, then shut up.
I therefore moved to Asia where I am an educator in English, history,
and yes computer science. I no longer have to deal with an American
programming culture that outside Microsoft is decadent and increasingly
migrating to Asia.

Good for you. Don't let the doorknob hit your ass on the way out.


Mark F. Haigh
(e-mail address removed)
 

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top