Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bug/Gross InEfficiency in HeathField's fgetline program

Reply
Thread Tools

Bug/Gross InEfficiency in HeathField's fgetline program

 
 
Ian Collins
Guest
Posts: n/a
 
      10-08-2007
Antoninus Twink wrote:
> On 7 Oct 2007 at 22:55, Richard Heathfield wrote:
>> Antoninus Twink said:
>>> It is also written in a hard-to-read and clunky style.

>> A matter of opinion. Which bit did you find hard to read?

>
> The function is a completely trivial one, yet I can't see it all at once
> in my editor without scrolling! Whitespace can help readability, but
> excessive whitespace can reduce it, and at the same time give too much
> weight to things that aren't important.
>

You must have a very small screen.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Philip Potter
Guest
Posts: n/a
 
      10-08-2007
Antoninus Twink wrote:
> On 7 Oct 2007 at 22:55, Richard Heathfield wrote:
>> It is not obvious to me that this code correctly replaces the code I wrote.

>
> If you believe that it doesn't correctly replace the code you wrote


He hasn't said that this is what he believes. He is stating that your
code does not *obviously* replace his correctly. It is up to you to
prove it does, if you're going to say his is defective and you have come
up with a replacement.

--
Philip Potter pgp <at> doc.ic.ac.uk
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      10-08-2007
"Antoninus Twink" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On 7 Oct 2007 at 22:55, Richard Heathfield wrote:
>> Antoninus Twink said:
>>> It is also written in a hard-to-read and clunky style.

>>
>> A matter of opinion. Which bit did you find hard to read?

>
> The function is a completely trivial one, yet I can't see it all at once
> in my editor without scrolling!

20 lines don't fit your screen???

Bye, Jojo



 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      10-08-2007
On Sun, 07 Oct 2007 19:59:08 -0700, Peter Nilsson wrote:

> Antoninus Twink <(E-Mail Removed)> wrote:
>> The function below is from Richard HeathField's fgetline
>> program. For some reason, it makes three passes through
>> the string (a strlen(), a strcpy() then another pass to
>> change dots) when two would clearly be sufficient. ...

>
> For some reason you've capitalised three letters in his
> name, when two would clearly be sufficient.

And two letters in InEfficiency, when zero would clearly be
sufficient.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-08-2007
Philip Potter said:

> Antoninus Twink wrote:
>> On 7 Oct 2007 at 22:55, Richard Heathfield wrote:
>>> It is not obvious to me that this code correctly replaces the code I
>>> wrote.

>>
>> If you believe that it doesn't correctly replace the code you wrote

>
> He hasn't said that this is what he believes.


I always knew you could read, Philip. Some other people, I'm not so
sure about.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-08-2007
Peter Nilsson <(E-Mail Removed)> wrote:

> Antoninus Twink <(E-Mail Removed)> wrote:
> > The function below is from Richard HeathField's fgetline
> > program. For some reason, it makes three passes through
> > the string (a strlen(), a strcpy() then another pass to
> > change dots) when two would clearly be sufficient. ...

>
> For some reason you've capitalised three letters in his
> name, when two would clearly be sufficient.


Well, yes. Now look at OP's /nom de Usenet/. Surprised?

Richard
 
Reply With Quote
 
Chris Hills
Guest
Posts: n/a
 
      10-08-2007
In article <(E-Mail Removed)>, Antoninus Twink
<(E-Mail Removed)> writes
>The function below is from Richard HeathField's fgetline program.


I know Richard has a thing about JN but can we not all sink to the same
level.

We should be discussing C, as it is used here, not all trying to be
pedants and petty point scoring.


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ http://www.velocityreviews.com/forums/(E-Mail Removed) www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      10-08-2007
Antoninus Twink <(E-Mail Removed)> writes:

> The function below is from Richard HeathField's fgetline program. For
> some reason, it makes three passes through the string (a strlen(), a
> strcpy() then another pass to change dots) when two would clearly be
> sufficient. This could lead to unnecessarily bad performance on very
> long strings. It is also written in a hard-to-read and clunky style.
>
> char *dot_to_underscore(const char *s)
> {
> char *t = malloc(strlen(s) + 1);
> if(t != NULL)
> {
> char *u;
> strcpy(t, s);
> u = t;
> while(*u)
> {
> if(*u == '.')
> {
> *u = '_';
> }
> ++u;
> }
> }
> return
> t;
> }
>
> Proposed solution:
>
> char *dot_to_underscore(const char *s)
> {
> char *t, *u;
> if(t=u=malloc(strlen(s)+1))
> while(*u++=(*s=='.' ? s++, '_' : *s++));
> return t;
> }


I would move the ++ part

,----
| while(*u++=(*s=='.' ? '_' : *s))
| s++;
`----

But yes, much nicer, easier to read and understand and possibly
faster. And hopefully working :-;
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      10-08-2007
Antoninus Twink wrote:
> On 7 Oct 2007 at 22:55, Richard Heathfield wrote:
>> Antoninus Twink said:


> The function is a completely trivial one, yet I can't see it all at once
> in my editor without scrolling! Whitespace can help readability, but
> excessive whitespace can reduce it, and at the same time give too much
> weight to things that aren't important.
>
>>> char *dot_to_underscore(const char *s)
>>> {
>>> char *t = malloc(strlen(s) + 1);
>>> if(t != NULL)
>>> {
>>> char *u;
>>> strcpy(t, s);
>>> u = t;
>>> while(*u)
>>> {
>>> if(*u == '.')
>>> {
>>> *u = '_';
>>> }
>>> ++u;
>>> }
>>> }
>>> return
>>> t;
>>> }
>>>
>>> Proposed solution:
>>>
>>> char *dot_to_underscore(const char *s)
>>> {
>>> char *t, *u;
>>> if(t=u=malloc(strlen(s)+1))
>>> while(*u++=(*s=='.' ? s++, '_' : *s++));
>>> return t;
>>> }

>> It is not obvious to me that this code correctly replaces the code I wrote.

>
> If you believe that it doesn't correctly replace the code you wrote, it
> would be easy to demonstrate that by pointing out a specific input s for
> which it gives a different result, or an error (syntax error or
> undefined behavior or whatever).


What happens when malloc returns a null pointer?

--
Thad
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      10-08-2007
Thad Smith wrote:

> Antoninus Twink wrote:
>> On 7 Oct 2007 at 22:55, Richard Heathfield wrote:
>>> Antoninus Twink said:

>
>> The function is a completely trivial one, yet I can't see it all at
>> once in my editor without scrolling! Whitespace can help readability,
>> but excessive whitespace can reduce it, and at the same time give too
>> much weight to things that aren't important.
>>
>>>> char *dot_to_underscore(const char *s)
>>>> {
>>>> char *t = malloc(strlen(s) + 1);
>>>> if(t != NULL)
>>>> {
>>>> char *u;
>>>> strcpy(t, s);
>>>> u = t;
>>>> while(*u)
>>>> {
>>>> if(*u == '.')
>>>> {
>>>> *u = '_';
>>>> }
>>>> ++u;
>>>> }
>>>> }
>>>> return
>>>> t;
>>>> }
>>>>
>>>> Proposed solution:
>>>>
>>>> char *dot_to_underscore(const char *s)
>>>> {
>>>> char *t, *u;
>>>> if(t=u=malloc(strlen(s)+1))
>>>> while(*u++=(*s=='.' ? s++, '_' : *s++));
>>>> return t;
>>>> }
>>> It is not obvious to me that this code correctly replaces the code I
>>> wrote.

>>
>> If you believe that it doesn't correctly replace the code you wrote,
>> it would be easy to demonstrate that by pointing out a specific input
>> s for which it gives a different result, or an error (syntax error or
>> undefined behavior or whatever).

>
> What happens when malloc returns a null pointer?


AFAICT it returns a null pointer value, same as Richard's version.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Designing fgetline - a perspective Richard Harter C Programming 48 11-25-2007 11:40 PM
Re: Bug/Gross InEfficiency in HeathField's fgetline program Dik T. Winter C Programming 2 11-07-2007 01:38 PM
Program inefficiency? hall.jeff@gmail.com Python 17 10-01-2007 04:48 PM
RE: Program inefficiency? Michael.Coll-Barth@VerizonWireless.com Python 6 10-01-2007 06:22 AM
Segfaulting when trying to create custom 'fgetline' function Jeff Rodriguez C Programming 4 11-17-2003 08:31 AM



Advertisments