Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #112462

Re: What is the difference between (r+ and w+)

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: What is the difference between (r+ and w+)
Date 2017-06-18 14:49 +0100
Organization A noiseless patient Spider
Message-ID <87h8zd4d9n.fsf@bsb.me.uk> (permalink)
References (1 earlier) <ln4lvejey6.fsf@kst-u.example.com> <oi4jq0$74j$1@gioia.aioe.org> <oi4l0m$vl6$1@jstuckle.eternal-september.org> <oi4mab$a0f$1@gioia.aioe.org> <oi4p0m$8on$1@dont-email.me>

Show all headers | View raw


Richard Heathfield <rjh@cpax.org.uk> writes:

> On 18/06/17 02:50, Christopher wrote:
<snip>
>> <https://drive.google.com/file/d/0Bwx0lG_v1tcONExSejZWMGJaMUU/view?usp=sharing>
>
> The "Hot Tip" is wrong. The stdio.h header contains no functions at
> all.

How do you know that stdio.h contains no functions at all?

> (Nor is stdio.h necessarily a file, and this assumption appears
> in the first sentence of the main text as well, and indeed throughout
> the quoted text.)

A quote from K&R2:

  A.12.4 File Inclusion
  
  A control line of the form
    # include <filename>
  causes the replacement of that line by the entire contents of the
  file filename.

<snip>
> The third paragraph introduces a
> definitely false claim in its first sentence (the claim that fopen()
> must always be used to open files),

Another quote from K&R2:

  Before it can be read or written, a file has to be opened by the
  library function fopen."

<snip>
> Using printf instead of fprintf to display diagnostic information is poor style.

Another K&R2 quote, this time from an example program:

  if (op2 != 0.0)
      push(pop() / op2);
  else
      printf("error: zero divisor\n");

> This:
>
> int func(void)
> {
>   if(this)
>   {
>     return such-and-such;
>   }
>   else
>   {
>     return something-else;
>   }
> }
>
> is legal, but it's so much cleaner to return an int at the function
> body level:

And int K&R2 we find:

  /* lower: convert c to lower case; ASCII only */
  int lower(int c)
  {
      if (c >= 'A' && c <= 'Z')
          return c + 'a' - 'A';
      else
          return c;
  }

<snip>
> and good habits are best taught early.

After reading K&R, yes?

<snip>
> The use of 1 rather than EXIT_FAILURE is non-portable.

  /* find all lines matching pattern */
  main()
  {
      char line[MAXLINE];
      int found = 0;
      while (getline(line, MAXLINE) > 0)
          if (strindex(line, pattern) >= 0) {
              printf("%s", line);
              found++;
          }
      return found;
  }

(no guesses as to where that's from).

> Finally, the failure to pass any diagnostic flags *at all* to gcc is
> worrying.

  "compile it with the command
  cc hello.c"

(Yes, K&R2 again.)

Have people called out K&R for all these things?  If not, there's more
than a whiff of a double standard here.

Other things in K&R that I suspect would get a serious tutting in some
other book include:

This use of main:

  main()
  {
      printf("hello, world\n");
  }

Casting malloc:

  struct tnode *talloc(void)
  {
      return (struct tnode *) malloc(sizeof(struct tnode));
  }

And this about strlen:

  "Here is our version:

  /* strlen: return length of s */
  int strlen(char s[])
  {
    <snip -- the code is fine it's the type that's the issue>
  }

  strlen and other string functions are declared in the standard header
  <string.h>."

And this possible integer overflow:

  for (i = 0, j = strlen(s)-1; i < j; i++, j--)
      c = s[i], s[i] = s[j], s[j] = c;

And here, if lim is too small, a buffer overflow is possible because c
is used uninitialised:

  /* getline: read a line into s, return length */
  int getline(char s[],int lim)
  {
      int c, i;

      for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
          s[i] = c;
      if (c == '\n') {
          s[i] = c;
          ++i;
      }
      s[i] = '\0';
      return i;
  }

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What is the difference between (r+ and w+) Christopher <invalid@invalid.invalid> - 2017-06-17 23:59 -0100
  Re: What is the difference between (r+ and w+) Keith Thompson <kst-u@mib.org> - 2017-06-17 17:51 -0700
    Re: What is the difference between (r+ and w+) Christopher <invalid@invalid.invalid> - 2017-06-18 00:00 -0100
      Re: What is the difference between (r+ and w+) Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-17 21:32 -0400
        Re: What is the difference between (r+ and w+) Christopher <invalid@invalid.invalid> - 2017-06-18 00:50 -0100
          Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 03:40 +0100
            Re: What is the difference between (r+ and w+) jacobnavia <jacob@jacob.remcomp.fr> - 2017-06-18 11:48 +0200
              Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 13:13 +0100
                Re: What is the difference between (r+ and w+) Gareth Owen <gwowen@gmail.com> - 2017-06-18 16:20 +0100
            Re: What is the difference between (r+ and w+) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-18 14:49 +0100
              Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 15:46 +0100
                Re: What is the difference between (r+ and w+) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 00:16 +0100
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-19 05:37 +0100
                Re: What is the difference between (r+ and w+) Keith Thompson <kst-u@mib.org> - 2017-06-19 09:25 -0700
                Re: What is the difference between (r+ and w+) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 17:35 +0100
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-19 18:19 +0100
                Re: What is the difference between (r+ and w+) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 20:51 +0100
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-19 21:16 +0100
                Re: What is the difference between (r+ and w+) Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 04:58 -0700
                Re: What is the difference between (r+ and w+) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 17:43 +0100
                Re: What is the difference between (r+ and w+) Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 22:16 -0700
                Re: What is the difference between (r+ and w+) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-20 11:53 +0100
                Re: What is the difference between (r+ and w+) Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-20 22:45 -0700
              Re: What is the difference between (r+ and w+) Good Guy <hello.world@example.com> - 2017-06-18 18:17 +0100
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 18:20 +0100
                Re: What is the difference between (r+ and w+) bartc <bc@freeuk.com> - 2017-06-18 18:27 +0100
                Re: What is the difference between (r+ and w+) jacobnavia <jacob@jacob.remcomp.fr> - 2017-06-20 11:11 +0200
                Re: What is the difference between (r+ and w+) bartc <bc@freeuk.com> - 2017-06-20 10:27 +0100
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-20 10:54 +0100
                Re: What is the difference between (r+ and w+) jacobnavia <jacob@jacob.remcomp.fr> - 2017-06-20 12:25 +0200
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-20 12:34 +0100
                Re: What is the difference between (r+ and w+) jameskuyper@verizon.net - 2017-06-20 05:37 -0700
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-20 14:11 +0100
                Re: What is the difference between (r+ and w+) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-20 14:35 +0100
                Re: What is the difference between (r+ and w+) raltbos@xs4all.nl (Richard Bos) - 2017-06-18 17:30 +0000
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 21:21 +0100
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 21:23 +0100
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 21:47 +0100
                Re: What is the difference between (r+ and w+) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-18 17:01 -0400
                Re: What is the difference between (r+ and w+) "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-19 10:41 -0400
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 21:50 +0100
                Re: What is the difference between (r+ and w+) Keith Thompson <kst-u@mib.org> - 2017-06-18 13:52 -0700
                Re: What is the difference between (r+ and w+) jacobnavia <jacob@jacob.remcomp.fr> - 2017-06-20 11:16 +0200
                Re: What is the difference between (r+ and w+) raltbos@xs4all.nl (Richard Bos) - 2017-06-26 15:54 +0000
                Re: What is the difference between (r+ and w+) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-18 17:13 -0400
                Re: What is the difference between (r+ and w+) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-18 17:17 -0400
                Re: What is the difference between (r+ and w+) Keith Thompson <kst-u@mib.org> - 2017-06-18 15:14 -0700
                Re: What is the difference between (r+ and w+) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-18 18:24 -0400
                Re: What is the difference between (r+ and w+) Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 04:11 -0700
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 23:01 +0100
          Re: What is the difference between (r+ and w+) Keith Thompson <kst-u@mib.org> - 2017-06-17 19:55 -0700
            Re: What is the difference between (r+ and w+) Christopher <invalid@invalid.invalid> - 2017-06-18 02:10 -0100
              Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 04:15 +0100
              Re: What is the difference between (r+ and w+) raltbos@xs4all.nl (Richard Bos) - 2017-06-18 08:31 +0000
                Re: What is the difference between (r+ and w+) Good Guy <hello.world@example.com> - 2017-06-18 18:28 +0100
                Re: What is the difference between (r+ and w+) Keith Thompson <kst-u@mib.org> - 2017-06-18 11:25 -0700
                Re: What is the difference between (r+ and w+) raltbos@xs4all.nl (Richard Bos) - 2017-06-18 19:13 +0000
                Re: What is the difference between (r+ and w+) David Kleinecke <dkleinecke@gmail.com> - 2017-06-18 14:30 -0700
                Re: What is the difference between (r+ and w+) Richard Heathfield <rjh@cpax.org.uk> - 2017-06-18 23:04 +0100
              Re: What is the difference between (r+ and w+) GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-06-18 10:43 +0200
                Re: What is the difference between (r+ and w+) Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-18 10:33 -0400
                Re: What is the difference between (r+ and w+) GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-06-19 01:27 +0200
                Re: What is the difference between (r+ and w+) Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-18 21:16 -0400
      Re: What is the difference between (r+ and w+) Ian Collins <ian-news@hotmail.com> - 2017-06-18 13:45 +1200
      Re: What is the difference between (r+ and w+) Keith Thompson <kst-u@mib.org> - 2017-06-17 19:05 -0700

csiph-web