473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segfault City

[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.

http://www.developerdotstar.com/community/node/291

This "teacher of C" demonstrates his prowess with a masterful display of
incompetence in a 200-line program that travels as swiftly as possible to
Segfault City.

(Actually, using my normal gcc switches, it doesn't even get past TP3, but
it will at least compile if you just use gcc -o foo foo.c, after hacking it
around a bit to do things like mending the line-spanning single-line
comments and string literals, which - in fairness to the author - are
probably an artifact of the Webification of the code.)

I wonder how long I'll take to stop laughing.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 24 '06 #1
162 6643
Richard Heathfield <in*****@invali d.invalid> writes:
[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.

http://www.developerdotstar.com/community/node/291

This "teacher of C" demonstrates his prowess with a masterful display of
incompetence in a 200-line program that travels as swiftly as possible to
Segfault City.

(Actually, using my normal gcc switches, it doesn't even get past TP3, but
it will at least compile if you just use gcc -o foo foo.c, after hacking it
around a bit to do things like mending the line-spanning single-line
comments and string literals, which - in fairness to the author - are
probably an artifact of the Webification of the code.)

I wonder how long I'll take to stop laughing.


Interesting. I just tried it myself. Copy-and-pasting the program
was tedious, but once I did that there were no line-wrapping errors.
Perhaps it's a function of which browser you use.

Obviously the "//" comments won't work if you use strict C90 mode.

With "gcc -g -std=c99 -pedantic -Wall -W -O3 sieve.c -o sieve", I got:

sieve.c: In function `string2Number' :
sieve.c:64: warning: comparison between signed and unsigned
sieve.c: In function `main':
sieve.c:202: warning: int format, time_t arg (arg 2)

The first is a comparison between an int and the result of strlen();
assuming no strings longer than 2**31-1 characters (on my 32-bit
system), this bug shouldn't cause any visible symptoms. Likewise, I
left the second bug in place, since both int and time_t happen to be
32-bit signed integers on the system I'm using. (If I were actually
going to use the program, I'd fix both bugs first.)

I got a quick seg fault when I ran it, because of the following really
dumb statement:

printf( strcat(strcat(" Sieve of Eratosthenes primes from least prime >= %d ",
"to greatest prime <= %d\n"),
"Sieve contains %d entries\n"),
MAX(intStart, 3),
intFinish,
intSieveLimit );

Using "-fwritable-strings" avoided the seg fault, but messed up the
output (obviously it was still invoking undefined behavior).

I changed the above to use string literal catenation rather than
strcat():

printf( "Sieve of Eratosthenes primes from least prime >= %d "
"to greatest prime <= %d\n"
"Sieve contains %d entries\n",
MAX(intStart, 3),
intFinish,
intSieveLimit );

With that change, the program seems to work (its failure to indicate
that 2 is prime seems to be an intentional design flaw rather than a
coding error).

It's still a good example of ugly C (they're called argc and argv, not
intArgCount and strArgValues), and it could probably be written more
clearly in no more than half the space, but it's not nauseatingly bad.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 24 '06 #2
On 2006-06-24, Richard Heathfield <in*****@invali d.invalid> wrote:
[X-posted, and followups set]

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

Just out of curiousity, did you stumble upon that while Googling your own
name? I'm not accusing you of arrogance, but it's interesting that he
mentions and insults you specifically in his preamble.
It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm
not sure which.

http://www.developerdotstar.com/community/node/291

This "teacher of C" demonstrates his prowess with a masterful display of
incompetence in a 200-line program that travels as swiftly as possible to
Segfault City.

Ooh. Segfault City. Where all bad programs go to die. :-)
(Actually, using my normal gcc switches, it doesn't even get past TP3, but
it will at least compile if you just use gcc -o foo foo.c, after hacking it
around a bit to do things like mending the line-spanning single-line
comments and string literals, which - in fairness to the author - are
probably an artifact of the Webification of the code.)

I wonder how long I'll take to stop laughing.


I stopped reading after the first function. I wasn't sure why he kept
casting chars to ints, as though there wasn't implicit casting going on
by default. Then he tested for > '9', which made no sense to me. Realizing
that I wouldn't be able to trace that logic, I stopped.

What, if anything, can it be guaranteed will be > '9' in standard C?

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 24 '06 #3
* Richard Heathfield:
[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.

http://www.developerdotstar.com/community/node/291

This "teacher of C" demonstrates his prowess with a masterful display of
incompetence in a 200-line program that travels as swiftly as possible to
Segfault City.

(Actually, using my normal gcc switches, it doesn't even get past TP3, but
it will at least compile if you just use gcc -o foo foo.c, after hacking it
around a bit to do things like mending the line-spanning single-line
comments and string literals, which - in fairness to the author - are
probably an artifact of the Webification of the code.)

I wonder how long I'll take to stop laughing.


I can understand you responding, Rirchard, since the web page contains a
comment about you, but I think it would have been more helpful to Edward
Nilges if you'd just sent him a private e-mail pointing out some of the
errors, such as the use of strcat in

printf( strcat(strcat(" The %s program calculates primes ",
"in a user-specifiable range. "),
"Use the following syntax: %s"),
strArgValues[0],
SYNTAX_REMINDER );

It's evident from the introductory text that Edward isn't a C
practitioner (e.g., he mentions how he had to figure out the declaration
syntax for arrays) and is used to more object-oriented languages, and
the code, such as the above, makes it painfully evident that he doesn't
have anybody doing quality assurance of the code -- which, one is led
to believe, is used in some C.Sc. course.

Perhaps he'd be glad to receive some help about that?
CC: sp*********@yah oo.com, which I'm assuming is Edward G. Nilges.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 24 '06 #4
["Followup-To:" header set to comp.lang.c.]
On 2006-06-24, Alf P. Steinbach <al***@start.no > wrote:
* Richard Heathfield:
[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.

http://www.developerdotstar.com/community/node/291

This "teacher of C" demonstrates his prowess with a masterful display of
incompetence in a 200-line program that travels as swiftly as possible to
Segfault City.

(Actually, using my normal gcc switches, it doesn't even get past TP3, but
it will at least compile if you just use gcc -o foo foo.c, after hacking it
around a bit to do things like mending the line-spanning single-line
comments and string literals, which - in fairness to the author - are
probably an artifact of the Webification of the code.)

I wonder how long I'll take to stop laughing.


I can understand you responding, Rirchard, since the web page contains a
comment about you, but I think it would have been more helpful to Edward
Nilges if you'd just sent him a private e-mail pointing out some of the
errors


Judging by the same comments, Edward Nilges has already receieved some
corrections from Richard and was less than happy about it:

"Richard acidulously explained my assisting John Nash with C as an
instance of the ape with the typewriter, in fact."

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 24 '06 #5
Andrew Poelstra wrote:
On 2006-06-24, Richard Heathfield <in*****@invali d.invalid> wrote:
[X-posted, and followups set]

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

Just out of curiousity, did you stumble upon that while Googling your own
name? I'm not accusing you of arrogance, but it's interesting that he
mentions and insults you specifically in his preamble.


Oooh, what fun! Richard is the target of a vendetta!
He'd better check his bed for horses' heads -- or perhaps
he needn't bother; the vendettor seems like someone who's
better acquainted with the opposite end of the animal.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 24 '06 #6
Andrew Poelstra <ap*******@loca lhost.localdoma in> writes:
[...]
I stopped reading after the first function. I wasn't sure why he kept
casting chars to ints, as though there wasn't implicit casting going on
by default. Then he tested for > '9', which made no sense to me. Realizing
that I wouldn't be able to trace that logic, I stopped.

What, if anything, can it be guaranteed will be > '9' in standard C?


I probably would have if I'd read it before compiling it.

Here's the function you're probably talking about:
=============== =============== ==========
// Convert character to its numeric form
//
//
int char2Number( char chrInChar )
{
int intInCharValue = ( int )chrInChar;
int intZeroValue = ( int )'0';
if ( intInCharValue < intZeroValue
||
intInCharValue > ( int )'9' ) return -1;
return intInCharValue - intZeroValue;
}
=============== =============== ==========

If something is < '0' or > '9', it's not a digit, and the function
returns -1 as an error indication.

On first glance, I assumed that something that size would convert a
string to an integer ("123" --> 123). Then I realized it only
converts a single character.

It's really impressive how ugly this manages to be without actually
being incorrect.

I might have to read the rest of this thing, but I don't think I'll
burden the newsgroup with a critique that would probably be several
times as long as the actual code.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 24 '06 #7
On Sat, 24 Jun 2006 05:35:36 +0200, "Alf P. Steinbach"
<al***@start.no > wrote in comp.lang.c:
* Richard Heathfield:
[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.

http://www.developerdotstar.com/community/node/291

This "teacher of C" demonstrates his prowess with a masterful display of
incompetence in a 200-line program that travels as swiftly as possible to
Segfault City.

(Actually, using my normal gcc switches, it doesn't even get past TP3, but
it will at least compile if you just use gcc -o foo foo.c, after hacking it
around a bit to do things like mending the line-spanning single-line
comments and string literals, which - in fairness to the author - are
probably an artifact of the Webification of the code.)

I wonder how long I'll take to stop laughing.


I can understand you responding, Rirchard, since the web page contains a
comment about you, but I think it would have been more helpful to Edward
Nilges if you'd just sent him a private e-mail pointing out some of the
errors, such as the use of strcat in

printf( strcat(strcat(" The %s program calculates primes ",
"in a user-specifiable range. "),
"Use the following syntax: %s"),
strArgValues[0],
SYNTAX_REMINDER );

It's evident from the introductory text that Edward isn't a C
practitioner (e.g., he mentions how he had to figure out the declaration
syntax for arrays) and is used to more object-oriented languages, and
the code, such as the above, makes it painfully evident that he doesn't
have anybody doing quality assurance of the code -- which, one is led
to believe, is used in some C.Sc. course.

Perhaps he'd be glad to receive some help about that?


Nilges is a bombastic, pedantic, and quite ignorant troll who used to
spew a lot of nonsense into comp.programmin g, and perhaps other groups
that I do not read regularly.

Perhaps he still does, I haven't seen any of his arrogant gibberish
since I added him to my kill file, long ago.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 24 '06 #8
"Alf P. Steinbach" <al***@start.no > writes:
* Richard Heathfield:
[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.
http://www.developerdotstar.com/community/node/291
This "teacher of C" demonstrates his prowess with a masterful
display of incompetence in a 200-line program that travels as
swiftly as possible to Segfault City.
(Actually, using my normal gcc switches, it doesn't even get past
TP3, but it will at least compile if you just use gcc -o foo foo.c,
after hacking it around a bit to do things like mending the
line-spanning single-line comments and string literals, which - in
fairness to the author - are probably an artifact of the
Webification of the code.)
I wonder how long I'll take to stop laughing.
I can understand you responding, Rirchard, since the web page contains
a comment about you, but I think it would have been more helpful to
Edward Nilges if you'd just sent him a private e-mail pointing out
some of the errors, such as the use of strcat in

Hardly I suggest you search for Nilges posts in the past and then you
know why Richard did what he did.
Perhaps he'd be glad to receive some help about that?

I doubt that really. He has shown a complete lack of insight in the
past. I doubt it has changed.

Regards
Friedrich
--
Please remove just-for-news- to reply via e-mail.
Jun 24 '06 #9
On 2006-06-24, Keith Thompson <ks***@mib.or g> wrote:
Andrew Poelstra <ap*******@loca lhost.localdoma in> writes:
[...]
I stopped reading after the first function. I wasn't sure why he kept
casting chars to ints, as though there wasn't implicit casting going on
by default. Then he tested for > '9', which made no sense to me. Realizing
that I wouldn't be able to trace that logic, I stopped.

What, if anything, can it be guaranteed will be > '9' in standard C?


I probably would have if I'd read it before compiling it.

Here's the function you're probably talking about:
============== =============== ===========
// Convert character to its numeric form
//
//
int char2Number( char chrInChar )
{
int intInCharValue = ( int )chrInChar;
int intZeroValue = ( int )'0';
if ( intInCharValue < intZeroValue
||
intInCharValue > ( int )'9' ) return -1;
return intInCharValue - intZeroValue;
}
============== =============== ===========

If something is < '0' or > '9', it's not a digit, and the function
returns -1 as an error indication.

On first glance, I assumed that something that size would convert a
string to an integer ("123" --> 123). Then I realized it only
converts a single character.

It's really impressive how ugly this manages to be without actually
being incorrect.

I might have to read the rest of this thing, but I don't think I'll
burden the newsgroup with a critique that would probably be several
times as long as the actual code.


I missed the "< '0'" because he disguised '0' behind a temp variable.
Indeed, 'tis ugly but not incorrect.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3146
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and an integer. However, as soon as the function is complete, I get a segfault and the interpreter dies. If I run Python interactively, just...
11
1663
by: H.A. Sujith | last post by:
The following code is causing a segfault at the first if statement. Am I doing something wrong or is it a compiler bug? //---------- #include <stdio.h> int main(int argc, char *argv) { int c; FILE *inp;
10
1938
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go beyond the file. At some point, I tried to mend that behavior using feof() but without success. The functionality is not harmed, but this has started...
165
6801
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But when the struct is declared in main (block scope) it will segfault when passing namelist to freeFileNames().
7
1680
by: spinoza1111 | last post by:
"segfault city" was an interchange wherein Richard Heathfield posted a complete lie: <heathfield> Richard Heathfield wrote: > This would be a straightforward test for numerics: > > int to_digit(int ch) > { > return isdigit((unsigned char)ch) ? ch - '0' : -1;
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.