p 161...File access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mdh

    #1

    p 161...File access

    Hi All,
    There is a line in the discussion of K&R (section 7-5) that says ...
    (end of 2nd paragraph, about the middle of the page)...in connection
    with the description of getc and putc...that

    "Like getchar and putchar, getc and putc may be macros instead of
    functions"

    Now, recently, I asked why va_list, va_arg, va_start etc were macros,
    and the answers included these two insights from KT;

    >>>>>>>
    a function can't modify an argument.

     A C function cannot take 
a type name as an argument
    <<<<<<<

    Is the significance of this statement ( ie relating to putchar/
    getchar) related to macros in va_list at all. And if not, what
    significance/insight should I attribute to this statement?
    Thanks as usual.



  • Gordon Burditt

    #2
    Re: p 161...File access

    >There is a line in the discussion of K&R (section 7-5) that says ...
    >(end of 2nd paragraph, about the middle of the page)...in connection
    >with the description of getc and putc...that
    >
    >"Like getchar and putchar, getc and putc may be macros instead of
    >functions"
    If they are macros, it's usually for efficiency reasons.

    You need to KNOW if getc() and putc() might be macros for
    reasons such as:
    FILE *f = &array_of_open_ files[0];

    c = getc(f++);
    might not do what you expect it to do because the first argument
    of getc() might be evaluated more than once, causing unexpected
    results. This is not an issue if getc() *MUST* be a function.

    >Now, recently, I asked why va_list, va_arg, va_start etc were macros,
    >and the answers included these two insights from KT;
    >
    >
    >>>>>>>>
    a function can't modify an argument.
    >
    > A C function cannot take 
a type name as an argument
    ><<<<<<<
    >Is the significance of this statement ( ie relating to putchar/
    >getchar) related to macros in va_list at all.
    Have you ever expected getc(unsigned long *) to work correctly?

    No. There are lots of different reasons why something might be a macro,
    and those reasons need not be related.

    >And if not, what
    >significance/insight should I attribute to this statement?
    Which statement?

    Code may be considered 'dead' because it can never be reached.
    Cats may be considered 'dead' because they have been shot through the
    head with a .45 .

    Do you think these two statements are related?

    Comment

    • Martien Verbruggen

      #3
      Re: p 161...File access

      On Sun, 28 Sep 2008 23:09:28 -0500,
      Gordon Burditt <gordonb.bvdua@ burditt.orgwrot e:
      >>There is a line in the discussion of K&R (section 7-5) that says ...
      >>(end of 2nd paragraph, about the middle of the page)...in connection
      >>with the description of getc and putc...that
      >>
      >>"Like getchar and putchar, getc and putc may be macros instead of
      >>functions"
      >
      If they are macros, it's usually for efficiency reasons.
      >
      You need to KNOW if getc() and putc() might be macros for
      reasons such as:
      FILE *f = &array_of_open_ files[0];
      >
      c = getc(f++);
      might not do what you expect it to do because the first argument
      of getc() might be evaluated more than once, causing unexpected
      results. This is not an issue if getc() *MUST* be a function.
      If a library function is implemented as a macro, it is not allowed to
      evaluate its arguments more than once.

      Excerpt from 7.1.4 p1:

      Any invocation of a library function that is implemented as a macro shall
      expand to code that evaluates each of its arguments exactly once, fully
      protected by parentheses where necessary, so it is generally safe to use
      arbitrary expressions as arguments.162)

      Footnote 162 says: Such macros might not contain the sequence points that the
      corresponding function calls do

      (This is copied from WG14/N1256. The C99 standard itself has identical
      wording, except that the footnote number is 156)

      In other words, the code above is guaranteed to behave identical whether
      getc() is a macro or a function.


      Martien
      --
      |
      Martien Verbruggen | I think there is a world market for maybe
      | five computers. -- Thomas Watson, chairman
      | of IBM, 1943

      Comment

      • Ben Pfaff

        #4
        Re: p 161...File access

        Martien Verbruggen <mgjv@tradingpo st.com.auwrites :
        If a library function is implemented as a macro, it is not allowed to
        evaluate its arguments more than once.
        [...]
        In other words, the code above is guaranteed to behave identical whether
        getc() is a macro or a function.
        It is amazing that you went to all the trouble of quoting all
        that without actually looking at the description of the getc
        function:

        7.19.7.5 The getc function
        [...]
        2 The getc function is equivalent to fgetc, except that if it
        is implemented as a macro, it may evaluate stream more than
        once, so the argument should never be an expression with
        side effects.

        --
        "I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

        Comment

        • Keith Thompson

          #5
          Re: p 161...File access

          gordonb.bvdua@b urditt.org (Gordon Burditt) writes:
          [...]
          >>Now, recently, I asked why va_list, va_arg, va_start etc were macros,
          >>and the answers included these two insights from KT;
          >>
          >>
          >>>>>>>>>
          >a function can't modify an argument.
          >>
          >> A C function cannot take 
a type name as an argument
          >><<<<<<<
          Gordon:

          The above (starting with "Now, recently") was written by mdh. He
          quoted something I had written, and he attributed it to me ("insights
          from KT"). You left *that* indirect attribution in place.

          Nothing bad happened.

          Thus endeth the lesson.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • mdh

            #6
            Re: p 161...File access

            On Sep 29, 12:55 am, Keith Thompson <ks...@mib.orgw rote:
            gordonb.bv...@b urditt.org (Gordon Burditt) writes:
            >
            [...]
            >
            >Now, recently, I asked why va_list, va_arg, va_start etc were macros,
            >and the answers included these two insights from KT;
            >
            a function can't modify an argument.
            >
            > A C function cannot take 
a type name as an argument
            ><<<<<<<
            >

            Hi Keith,
            I am still at a little bit of a loss why K&R specifically mentioned
            this factoid. There may have been a lesson in the earlier answer, but
            it evaded me.



            Comment

            • Martien Verbruggen

              #7
              Re: p 161...File access

              On Sun, 28 Sep 2008 22:40:01 -0700,
              Ben Pfaff <blp@cs.stanfor d.eduwrote:
              Martien Verbruggen <mgjv@tradingpo st.com.auwrites :
              >
              >If a library function is implemented as a macro, it is not allowed to
              >evaluate its arguments more than once.
              [...]
              >In other words, the code above is guaranteed to behave identical whether
              >getc() is a macro or a function.
              >
              It is amazing that you went to all the trouble of quoting all
              that without actually looking at the description of the getc
              function:
              >
              7.19.7.5 The getc function
              [...]
              2 The getc function is equivalent to fgetc, except that if it
              is implemented as a macro, it may evaluate stream more than
              once, so the argument should never be an expression with
              side effects.
              I never noticed that before. This seems to be limited to getc, putc,
              getwc and putwc. Any others that are exceptions to this rule that I
              missed?

              Martien
              --
              |
              Martien Verbruggen | If at first you don't succeed, try again.
              | Then quit; there's no use being a damn fool
              | about it.

              Comment

              • Mike Wahler

                #8
                Re: p 161...File access

                "Gordon Burditt" <gordonb.bvdua@ burditt.orgwrot e in message
                news:sbKdnasTP6 RlyX3VnZ2dnUVZ_ j-dnZ2d@posted.in ternetamerica.. .
                Cats may be considered 'dead' because they have been shot through the
                head
                nine times
                with a .45 .
                :-)

                -Mike


                Comment

                • Keith Thompson

                  #9
                  Re: p 161...File access

                  mdh <mdeh@comcast.n etwrites:
                  On Sep 29, 12:55 am, Keith Thompson <ks...@mib.orgw rote:
                  [...]
                  >>Now, recently, I asked why va_list, va_arg, va_start etc were macros,
                  >>and the answers included these two insights from KT;
                  >>
                  >a function can't modify an argument.
                  >>
                  >> A C function cannot take 
a type name as an argument
                  >><<<<<<<
                  >
                  Hi Keith,
                  I am still at a little bit of a loss why K&R specifically mentioned
                  this factoid. There may have been a lesson in the earlier answer, but
                  it evaded me.
                  I wasn't aware that K&R mentioned it (though it's possible they do).

                  *I* mentioned it in response to your question about why va_* are macros.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Gordon Burditt

                    #10
                    Re: p 161...File access

                    >>>Now, recently, I asked why va_list, va_arg, va_start etc were macros,
                    >>>and the answers included these two insights from KT;
                    >>>
                    >>>
                    >>>>>>>>>>
                    >>a function can't modify an argument.
                    >>>
                    >>> A C function cannot take 
a type name as an argument
                    >>><<<<<<<
                    >
                    >Gordon:
                    >
                    >The above (starting with "Now, recently") was written by mdh. He
                    >quoted something I had written, and he attributed it to me ("insights
                    >from KT"). You left *that* indirect attribution in place.
                    Are you sure "KT" isn't a reference to a Knowledge Tree web site?
                    >Nothing bad happened.
                    Something bad happened. Someone posted the article to which I am
                    replying, and talked about nothing but attributions, which are
                    entirely off-topic. Nowhere do any names of posters appear in the
                    C standard (with the possible exception of Dennis Ritchie).
                    Yes, this article is just as much an offender.

                    Attributions are bad because they provoke many of the "regulars"
                    into talking about the attributions and the people named in them
                    rather than talking about C. They call each other "trolls" and
                    trade insults.

                    Please don't read the attributions. And if you must read the
                    attributions, don't copy names from them into the body of the
                    article. The meaning of what was said doesn't change with who said
                    it.

                    Comment

                    • Flash Gordon

                      #11
                      Re: p 161...File access

                      Gordon Burditt wrote, On 29/09/08 19:10:
                      >>>Now, recently, I asked why va_list, va_arg, va_start etc were macros,
                      Keith Thompson has on many occasions specifically told you that he does
                      not want his posts quoted without attribution. Since this has often been
                      done when replying to you I find it unbelievable that you are not aware
                      of his desire.
                      >Nothing bad happened.
                      >
                      Something bad happened...
                      If that is bad, it is something bad happening in response to YOUR
                      failure to provide attributions.

                      However, as you are well aware Keith means that no one has sued him as a
                      result of posting that argument and it did not leed to complaints about
                      miss-attribution.

                      <snip>
                      Attributions are bad because they provoke many of the "regulars"
                      into talking about the attributions and the people named in them
                      rather than talking about C. They call each other "trolls" and
                      trade insults.
                      It is not the attributions that cause that. After all, such posts are
                      generally in *direct* response to the person, *not* in response to a
                      post quoting that person.
                      Please don't read the attributions. And if you must read the
                      attributions, don't copy names from them into the body of the
                      article.
                      Please ignore Gordon on this matter. The "standard" on news groups is to
                      attribute quoted material to the poster.
                      The meaning of what was said doesn't change with who said
                      it.
                      No, but how likely it is to be reliable does depend on who posted it. It
                      also allows one to spot whether it is a debate between just two people
                      or one with multiple people which can be useful.

                      For the record, I explicitly deny permission to quote any of the
                      articles I have ever posted to any news group without attribution. I
                      also deny permission to quote any of my future articles without attribution.
                      --
                      Flash Gordon
                      If spamming me sent it to smap@spam.cause way.com
                      If emailing me use my reply-to address
                      See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                      Comment

                      • Keith Thompson

                        #12
                        Re: p 161...File access

                        gordonb.xe32k@b urditt.org (Gordon Burditt) writes:
                        >>>>Now, recently, I asked why va_list, va_arg, va_start etc were macros,
                        >>>>and the answers included these two insights from KT;
                        >>>>
                        >>>>
                        >>>>>>>>>>>
                        >>>a function can't modify an argument.
                        >>>>
                        >>>> A C function cannot take 
a type name as an argument
                        >>>><<<<<<<
                        The above was written by mdh <mdeh@comcast.n et>.
                        >>
                        >>Gordon:
                        >>
                        >>The above (starting with "Now, recently") was written by mdh. He
                        >>quoted something I had written, and he attributed it to me ("insights
                        >>from KT"). You left *that* indirect attribution in place.
                        The above was written by me, Keith Thompson <kst-u@mib.org>.
                        Are you sure "KT" isn't a reference to a Knowledge Tree web site?
                        Yes.
                        >>Nothing bad happened.
                        >
                        Something bad happened. Someone posted the article to which I am
                        replying, and talked about nothing but attributions, which are
                        entirely off-topic. Nowhere do any names of posters appear in the
                        C standard (with the possible exception of Dennis Ritchie).
                        Yes, this article is just as much an offender.
                        >
                        Attributions are bad because they provoke many of the "regulars"
                        into talking about the attributions and the people named in them
                        rather than talking about C. They call each other "trolls" and
                        trade insults.
                        Nonsense. Your deliberate snipping of attributions occasionally
                        provokes such discussions. If you left them alone, nothing bad would
                        happen, and this discussion would not be taking place.
                        Please don't read the attributions. And if you must read the
                        attributions, don't copy names from them into the body of the
                        article. The meaning of what was said doesn't change with who said
                        it.
                        What I write is judged in part by what I've written before; same for
                        everyone else here. If Steve Summit writes something, I check my
                        facts very carefully before publicly disagreeing. If certain other
                        posters write something, I check my facts very carefully before
                        agreeing. Furthermore, this newsgroup isn't just a series of factual
                        statements (or misstatements) made in a vacuum; it's a kind of
                        community. Knowing who said what makes it a more pleasant place and
                        makes it easier to follow the discussions.

                        I understand that you, Gordon Burditt, are not likely to change your
                        mind on this topic. I only brought it up again because you
                        (accidentally) left a partial attribution in place, rather than rudely
                        deleting deleting it. I don't intend to discuss it again, except for
                        my usual request when I happen to post a followup to something you
                        wrote.

                        I, Keith Thompson, wrote this. Permission to quote this, or anything
                        else I post to Usenet, without attribution is denied. Permission to
                        quote with proper attribution is granted, as always.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • Martin Ambuhl

                          #13
                          Re: p 161...File access

                          Gordon Burditt (Yes, that's whp wrote it; sue me Gordon for attributing
                          to you your own words.) wrote:
                          Are you sure "KT" isn't a reference to a Knowledge Tree web site?
                          Yes, and you are an idiot if think otherwise.
                          Something bad happened. Someone posted the article to which I am
                          replying, and talked about nothing but attributions, which are
                          entirely off-topic.
                          Something bad happened. You stole other people's words without
                          attribution. That is anti-social and dishonest.
                          Attributions are bad because they provoke many of the "regulars"
                          into talking about the attributions and the people named in them
                          rather than talking about C. They call each other "trolls" and
                          trade insults.
                          That is just incredibly stupid. Proper attribution does not lead to
                          discussion of attribution. Where do you get off telling such blatant lies?
                          Please don't read the attributions. And if you must read the
                          attributions, don't copy names from them into the body of the
                          article. The meaning of what was said doesn't change with who said
                          it.
                          This is not only stupid and dishonest, it violates long-standing usenet
                          policy. Do not pay any attention to Gordon Burditt: he is ignorant and
                          anti-social and is trying to entice others into his den of ignorance and
                          misbehavior.

                          Comment

                          • dj3vande@csclub.uwaterloo.ca.invalid

                            #14
                            Re: p 161...File access

                            In article <r9k7r5x3gu.ln2 @news.flash-gordon.me.uk>,
                            Flash Gordon <spam@flash-gordon.me.ukwro te:
                            >Gordon Burditt wrote, On 29/09/08 19:10:
                            [quoting somebody without attribution:]
                            >>>>Now, recently, I asked why va_list, va_arg, va_start etc were macros,
                            >
                            >Keith Thompson has on many occasions specifically told you that he does
                            >not want his posts quoted without attribution. Since this has often been
                            >done when replying to you I find it unbelievable that you are not aware
                            >of his desire.
                            Perhaps it's time to start sending cease-and-desist letters?


                            dave

                            --
                            Dave Vandervies dj3vande at eskimo dot com
                            Oh, and hello to everyone that noticed I wasn't here. Everyone else,
                            please carry on not caring.
                            --Randy the Random in the scary devil monastery

                            Comment

                            • Keith Thompson

                              #15
                              Re: p 161...File access

                              dj3vande@csclub .uwaterloo.ca.i nvalid writes:
                              In article <r9k7r5x3gu.ln2 @news.flash-gordon.me.uk>,
                              Flash Gordon <spam@flash-gordon.me.ukwro te:
                              >>Gordon Burditt wrote, On 29/09/08 19:10:
                              [quoting somebody without attribution:]
                              >>>>>Now, recently, I asked why va_list, va_arg, va_start etc were macros,
                              >>
                              >>Keith Thompson has on many occasions specifically told you that he does
                              >>not want his posts quoted without attribution. Since this has often been
                              >>done when replying to you I find it unbelievable that you are not aware
                              >>of his desire.
                              >
                              Perhaps it's time to start sending cease-and-desist letters?
                              Nah. His use of my words without attribution is annoying, but I'm
                              under no obligation to do anything about it, and I choose not to in
                              this case. I doubt that bringing the legal system into this would be
                              either appropriate or constructive. This is about rudeness, not legal
                              liability, Gordon Burditt's absurd claims to the contrary
                              notwithstanding . I am Keith Thompson, replying to Flash Gordon's
                              reply to Gordon Burditt. Permission to quote this without attribution
                              is specifically denied.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              Nokia
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...