deciphering a macro

Discussion in 'C Programming' started by Uno, Jul 1, 2010.

  1. Uno

    Uno Guest

    This is Plauger's macro override for putc:

    #define putc(c, str) ((str)->_Next < (str)->_Wend \
    ? (*(str)->_Next++ = c) : (putc)(c, str))

    So if str._Next is less than str._Wend then
    str._Next, iterated once, equals c
    else
    you have to call the definition of putc.

    How did I do with the logic on that?

    Thanks for your comment, and cheers,
    --
    Uno
    Uno, Jul 1, 2010
    #1
    1. Advertising

  2. Uno

    Eric Sosman Guest

    On 6/30/2010 10:24 PM, Uno wrote:
    > This is Plauger's macro override for putc:
    >
    > #define putc(c, str) ((str)->_Next < (str)->_Wend \
    > ? (*(str)->_Next++ = c) : (putc)(c, str))
    >
    > So if str._Next is less than str._Wend then
    > str._Next, iterated once, equals c
    > else
    > you have to call the definition of putc.
    >
    > How did I do with the logic on that?


    Your phrasing suggests that the pointer is incremented before
    the character is stored, which it's not. I'd have preferred "store
    c where str->_Next points, and increment str->_Next."

    --
    Eric Sosman
    lid
    Eric Sosman, Jul 1, 2010
    #2
    1. Advertising

  3. Uno

    Uno Guest

    Eric Sosman wrote:
    > On 6/30/2010 10:24 PM, Uno wrote:
    >> This is Plauger's macro override for putc:
    >>
    >> #define putc(c, str) ((str)->_Next < (str)->_Wend \
    >> ? (*(str)->_Next++ = c) : (putc)(c, str))
    >>
    >> So if str._Next is less than str._Wend then
    >> str._Next, iterated once, equals c
    >> else
    >> you have to call the definition of putc.
    >>
    >> How did I do with the logic on that?

    >
    > Your phrasing suggests that the pointer is incremented before
    > the character is stored, which it's not. I'd have preferred "store
    > c where str->_Next points, and increment str->_Next."
    >


    Thx, eric.

    if the value where str->_Next points is less than the value where
    str->_Wend points then store c where str->_Next points, and increment
    str->_Next, otherwise use the function.

    Better?

    I'm looking for more of this source, but as a macro neophyte, I have no
    method for this.

    Does this tell you anything?

    typedef struct {
    unsigned short _Mode;
    short _Handle;
    unsigned char *_Buf, *_Bend, *_Next;
    unsigned char *_Rend, *_Rsave, *_Wend;
    unsigned char _Back[2], _Cbuf, _Nback;
    char *_Tmpnam;
    } FILE;
    --
    Uno
    Uno, Jul 1, 2010
    #3
  4. Uno wrote:

    > if the value where str->_Next points is less than the value where
    > str->_Wend points then store c where str->_Next points, and increment
    > str->_Next, otherwise use the function.


    Here you are dealing with pointers. It means:
    if the memory address str->_Next is lower than the memory
    address str->_Wend, the comparison '<' refers to pointers
    not to values pointed to.




    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jul 1, 2010
    #4
  5. Uno

    Uno Guest

    Vincenzo Mercuri wrote:
    > Uno wrote:
    >
    >> if the value where str->_Next points is less than the value where
    >> str->_Wend points then store c where str->_Next points, and increment
    >> str->_Next, otherwise use the function.

    >
    > Here you are dealing with pointers. It means:
    > if the memory address str->_Next is lower than the memory
    > address str->_Wend, the comparison '<' refers to pointers
    > not to values pointed to.
    >
    >
    >
    >


    if the memory address str->_Next points is numerically less than the
    memory address str->_Wend points to, then store c where str->_Next
    points, and increment str->_Next, otherwise use the function.

    ?
    --
    Uno
    Uno, Jul 1, 2010
    #5
  6. Uno commented the following library code:

    typedef struct {
    unsigned short _Mode;
    short _Handle;
    unsigned char *_Buf, *_Bend, *_Next;
    unsigned char *_Rend, *_Rsave, *_Wend;
    unsigned char _Back[2], _Cbuf, _Nback;
    char *_Tmpnam;
    } FILE;

    #define putc(c, str) ((str)->_Next < (str)->_Wend \
    ? (*(str)->_Next++ = c) : (putc)(c, str))


    > if the memory address str->_Next points is numerically less than the
    > memory address str->_Wend points to, then store c where str->_Next
    > points, and increment str->_Next, otherwise use the function.


    Yes. The macro putc is an alternative to the function putc,
    that does not involve a function call, but behaves like a function
    (although one can't take its address). It works by caching write into
    a memory buffer (presumably _Buf), endind at _Bend, with current
    pointer at _Next. If that can't work [e.g. when the buffer is full,
    this corresponds to !((str)->_Next < (str)->_Wend) ], the function
    putc is called. On many machines, this speeds up putc by several
    binary orders of magnitude.

    François Grieu
    Francois Grieu, Jul 1, 2010
    #6
  7. Uno

    Uno Guest

    Francois Grieu wrote:
    > Uno commented the following library code:
    >
    > typedef struct {
    > unsigned short _Mode;
    > short _Handle;
    > unsigned char *_Buf, *_Bend, *_Next;
    > unsigned char *_Rend, *_Rsave, *_Wend;
    > unsigned char _Back[2], _Cbuf, _Nback;
    > char *_Tmpnam;
    > } FILE;
    >
    > #define putc(c, str) ((str)->_Next < (str)->_Wend \
    > ? (*(str)->_Next++ = c) : (putc)(c, str))
    >
    >
    >> if the memory address str->_Next points is numerically less than the
    >> memory address str->_Wend points to, then store c where str->_Next
    >> points, and increment str->_Next, otherwise use the function.

    >
    > Yes. The macro putc is an alternative to the function putc,
    > that does not involve a function call, but behaves like a function
    > (although one can't take its address). It works by caching write into
    > a memory buffer (presumably _Buf), endind at _Bend, with current
    > pointer at _Next. If that can't work [e.g. when the buffer is full,
    > this corresponds to !((str)->_Next < (str)->_Wend) ], the function
    > putc is called. On many machines, this speeds up putc by several
    > binary orders of magnitude.


    Is the definition of the _Next in the #define from this struct?
    --
    Uno
    Uno, Jul 1, 2010
    #7
  8. Le 01/07/2010 09:52, Uno a écrit :
    > Francois Grieu wrote:
    >> Uno commented the following library code:
    >>
    >> typedef struct {
    >> unsigned short _Mode;
    >> short _Handle;
    >> unsigned char *_Buf, *_Bend, *_Next;
    >> unsigned char *_Rend, *_Rsave, *_Wend;
    >> unsigned char _Back[2], _Cbuf, _Nback;
    >> char *_Tmpnam;
    >> } FILE;
    >>
    >> #define putc(c, str) ((str)->_Next < (str)->_Wend \
    >> ? (*(str)->_Next++ = c) : (putc)(c, str))
    >>
    >>
    >>> if the memory address str->_Next points is numerically less than the
    >>> memory address str->_Wend points to, then store c where str->_Next
    >>> points, and increment str->_Next, otherwise use the function.

    >>
    >> Yes. The macro putc is an alternative to the function putc,
    >> that does not involve a function call, but behaves like a function
    >> (although one can't take its address). It works by caching write into
    >> a memory buffer (presumably _Buf), endind at _Bend, with current
    >> pointer at _Next. If that can't work [e.g. when the buffer is full,
    >> this corresponds to !((str)->_Next < (str)->_Wend) ], the function
    >> putc is called. On many machines, this speeds up putc by several
    >> binary orders of magnitude.

    >
    > Is the definition of the _Next in the #define from this struct?


    Yes. In putc, the second parameter is a pointer to FILE.

    Francois Grieu
    Francois Grieu, Jul 1, 2010
    #8
  9. Uno

    Eric Sosman Guest

    On 6/30/2010 11:03 PM, Uno wrote:
    > Eric Sosman wrote:
    >> On 6/30/2010 10:24 PM, Uno wrote:
    >>> This is Plauger's macro override for putc:
    >>>
    >>> #define putc(c, str) ((str)->_Next < (str)->_Wend \
    >>> ? (*(str)->_Next++ = c) : (putc)(c, str))
    >>>
    >>> So if str._Next is less than str._Wend then
    >>> str._Next, iterated once, equals c
    >>> else
    >>> you have to call the definition of putc.
    >>>
    >>> How did I do with the logic on that?

    >>
    >> Your phrasing suggests that the pointer is incremented before
    >> the character is stored, which it's not. I'd have preferred "store
    >> c where str->_Next points, and increment str->_Next."
    >>

    >
    > Thx, eric.
    >
    > if the value where str->_Next points is less than the value where
    > str->_Wend points then store c where str->_Next points, and increment
    > str->_Next, otherwise use the function.
    >
    > Better?


    It still seems a bit confused, or maybe just a little baroque.
    How about "If the value of str->_Next is less than the value of
    str->_Wend, store c where str->_Wend points and advance str->_Wend,
    otherwise use the function."

    > I'm looking for more of this source, but as a macro neophyte, I have no
    > method for this.


    Do you have the book the source comes from? It's a good book
    and does a good job of explaining why the source looks the way it
    does, but it presupposes a certain proficiency in C. If you're
    just looking at the source without the book's explanations (and
    especially if you're the neophyte you say you are), I don't think
    you're likely to gain much understanding. Get the book if you
    don't already have it, and read it if you do.

    --
    Eric Sosman
    lid
    Eric Sosman, Jul 1, 2010
    #9
  10. Uno

    Uno Guest

    Eric Sosman wrote:


    >> I'm looking for more of this source, but as a macro neophyte, I have no
    >> method for this.

    >
    > Do you have the book the source comes from? It's a good book
    > and does a good job of explaining why the source looks the way it
    > does, but it presupposes a certain proficiency in C. If you're
    > just looking at the source without the book's explanations (and
    > especially if you're the neophyte you say you are), I don't think
    > you're likely to gain much understanding. Get the book if you
    > don't already have it, and read it if you do.
    >


    What I meant when I said that I was looking for more of this source, I
    was trying to find it on my own system, with which I have only 6 months
    experience. My search for stdio.h turned up 7 results, and I was sunk.

    Plauger's book is fascinating, and he was nice enough to send me the
    source, so if I have trouble with any part of it, I can learn from my
    compiler. (or copy, paste, and post to usenet)

    It will be the only book that comes with me to Lake Heron, NM now for 4
    days of camping at 7200 feet. I'll waive to you in your Arizona swelter.;-)
    --
    Uno
    Uno, Jul 2, 2010
    #10
  11. Uno

    Eric Sosman Guest

    On 7/1/2010 11:13 PM, Uno wrote:
    > Eric Sosman wrote:
    >
    >>> I'm looking for more of this source, but as a macro neophyte, I have no
    >>> method for this.

    >>
    >> Do you have the book the source comes from? It's a good book
    >> and does a good job of explaining why the source looks the way it
    >> does, but it presupposes a certain proficiency in C. If you're
    >> just looking at the source without the book's explanations (and
    >> especially if you're the neophyte you say you are), I don't think
    >> you're likely to gain much understanding. Get the book if you
    >> don't already have it, and read it if you do.

    >
    > What I meant when I said that I was looking for more of this source, I
    > was trying to find it on my own system, with which I have only 6 months
    > experience. My search for stdio.h turned up 7 results, and I was sunk.


    Do you understand that Plauger's code is *a* way of implementing the
    Standard library, not *the* way? The library implementation(s) you
    find on your system will certainly be different from Plauger's, in
    detail and perhaps in overall structure.

    > Plauger's book is fascinating, and he was nice enough to send me the
    > source, so if I have trouble with any part of it, I can learn from my
    > compiler. (or copy, paste, and post to usenet)


    He sent you the code *and* permission to redistribute? I think
    you should double-check before posting too freely. "Fair use" covers
    a certain amount of copying (IANAL), but there's a limit.

    > It will be the only book that comes with me to Lake Heron, NM now for 4
    > days of camping at 7200 feet. I'll waive to you in your Arizona swelter.;-)


    Never been there; not likely to go. And it's "wave."

    --
    Eric Sosman
    lid
    Eric Sosman, Jul 2, 2010
    #11
  12. Uno

    Uno Guest

    Eric Sosman wrote:
    > On 7/1/2010 11:13 PM, Uno wrote:
    >> Eric Sosman wrote:
    >>
    >>>> I'm looking for more of this source, but as a macro neophyte, I have no
    >>>> method for this.
    >>>
    >>> Do you have the book the source comes from? It's a good book
    >>> and does a good job of explaining why the source looks the way it
    >>> does, but it presupposes a certain proficiency in C. If you're
    >>> just looking at the source without the book's explanations (and
    >>> especially if you're the neophyte you say you are), I don't think
    >>> you're likely to gain much understanding. Get the book if you
    >>> don't already have it, and read it if you do.

    >>
    >> What I meant when I said that I was looking for more of this source, I
    >> was trying to find it on my own system, with which I have only 6 months
    >> experience. My search for stdio.h turned up 7 results, and I was sunk.

    >
    > Do you understand that Plauger's code is *a* way of implementing the
    > Standard library, not *the* way? The library implementation(s) you
    > find on your system will certainly be different from Plauger's, in
    > detail and perhaps in overall structure.


    That's one thing that Keith has taught me over the years, namely, that
    trapseing through the headers on your own implementation might not be
    the best way to do what you're trying to achieve.

    [OT]
    Is there a linux or gcc command that will tell me the inclusion tree for
    stdio.h?
    >
    >> Plauger's book is fascinating, and he was nice enough to send me the
    >> source, so if I have trouble with any part of it, I can learn from my
    >> compiler. (or copy, paste, and post to usenet)

    >
    > He sent you the code *and* permission to redistribute? I think
    > you should double-check before posting too freely. "Fair use" covers
    > a certain amount of copying (IANAL), but there's a limit.


    Yeah, and again he is really cool about it. You *can* use his stuff and
    make money off it; you simply have to acknowledge.

    My quoting a paragraph here or line there could never be stitched
    together into a workproduct; you would need *all* of the source.

    I will tell people how to write a very gracious man and replicate my
    steps to getting the soft version of Plauger's library. I would
    consider owning his book _The C Standard Library_ as a prereq. During
    the formation of the C standard, he chaired the effort on the standard
    library.

    My particular interest is not that I want to write a standard library
    myself, but I want to be able to use C to call whatever library I need.

    I've come to learn that "library" can mean "any number you can calculate
    with fortran."
    >
    >> It will be the only book that comes with me to Lake Heron, NM now for 4
    >> days of camping at 7200 feet. I'll waive to you in your Arizona
    >> swelter.;-)

    >
    > Never been there; not likely to go. And it's "wave."


    Your loss, it's beautiful. It was a little bit rugged up there, tho.
    We camped in tents but could easily walk to a place where you have
    electricity and could take a duke on a toilet with proper plumbing and
    sanitation. (That's my dealbreaker for "roughing it.")

    It has what every C programmer wants: no bugs.
    --
    Uno
    Uno, Jul 6, 2010
    #12
  13. Uno

    Uno Guest

    Richard Heathfield wrote:
    > Uno wrote:
    >
    > <snip>
    >
    >> That's one thing that Keith has taught me over the years,

    >
    > If you've been learning from Keith for years, how come you still don't
    > know that getc and related functions return int, not char?


    Because the difference doesn't amount to a hill of beans when I'm
    looking for something more important.
    >
    > You would do well to spend less time delving into the deep stuff (for
    > now), and a lot more time learning the basics. Once you have learned
    > them, try not to forget them.
    >


    You would do well not to sound like seebs.
    --
    Uno
    Uno, Jul 7, 2010
    #13
  14. Uno ha scritto:

    > You would do well not to sound like seebs.


    Maybe you mistook thread for threat. uno.

    "Speaking words of wisdom, write in C...".


    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jul 7, 2010
    #14
  15. Uno

    bart.c Guest

    "Uno" <> wrote in message
    news:...

    >>> It will be the only book that comes with me to Lake Heron, NM now for 4
    >>> days of camping at 7200 feet. I'll waive to you in your Arizona
    >>> swelter.;-)


    (The one time I camped in AZ, the overnight low was 22F (-5C).)

    --
    Bartc
    bart.c, Jul 7, 2010
    #15
  16. Uno <> writes:
    > Richard Heathfield wrote:
    >> Uno wrote:
    >>
    >> <snip>
    >>
    >>> That's one thing that Keith has taught me over the years,

    >>
    >> If you've been learning from Keith for years, how come you still
    >> don't know that getc and related functions return int, not char?

    >
    > Because the difference doesn't amount to a hill of beans when I'm
    > looking for something more important.


    You should rethink your idea of what's important. Hint: correctness
    should be high on the list.

    [...]

    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Jul 7, 2010
    #16
  17. Uno

    Uno Guest

    Vincenzo Mercuri wrote:
    > Uno ha scritto:
    >
    >> You would do well not to sound like seebs.

    >
    > Maybe you mistook thread for threat. uno.
    >
    > "Speaking words of wisdom, write in C...".
    >
    >


    I understand peace.
    --
    Uno
    Uno, Jul 11, 2010
    #17
  18. Uno

    Uno Guest

    Keith Thompson wrote:

    > You should rethink your idea of what's important. Hint: correctness
    > should be high on the list.


    Sentences of the form:

    inanimate object should

    are philosophically unintelligible.

    Objects lack agency and volition.

    Hence,

    correctness should

    is ludicrous.

    But the first one's better. An interpersonal should:

    x ought y

    Was falling into Hume's is-ought gap something you're proud of, when
    this failed-distinction creates wars, environmental disasters, and
    people without HBO?
    --
    Uno
    Uno, Jul 11, 2010
    #18
  19. Uno

    Uno Guest

    Richard Heathfield wrote:
    > Uno wrote:
    >> Richard Heathfield wrote:
    >>> Uno wrote:
    >>>
    >>> <snip>
    >>>
    >>>> That's one thing that Keith has taught me over the years,
    >>>
    >>> If you've been learning from Keith for years, how come you still
    >>> don't know that getc and related functions return int, not char?

    >>
    >> Because the difference doesn't amount to a hill of beans when I'm
    >> looking for something more important.

    >
    > If there's anything more important than getting things right, I'd love
    > to hear what it is.


    Where to start. I'd like to get accurate numbers from British
    Petroleum. I refuse to let Jim Demint stand between two governments in
    his awesome, wretched Southern, filibustering Repugnance.

    I remember you as one of the good guys while Blair was dismantling your
    higher laws.

    >
    >>>
    >>> You would do well to spend less time delving into the deep stuff (for
    >>> now), and a lot more time learning the basics. Once you have learned
    >>> them, try not to forget them.
    >>>

    >>
    >> You would do well not to sound like seebs.

    >
    > Seebs will give you the same advice as me (in this regard) for the
    > simple reason that it's good advice. So will Keith Thompson. So will
    > anyone with a reasonable level of C knowledge. Why bother to ask C
    > people C questions if you are not even remotely interested in learning
    > the answers?
    >


    Dream of C, Richard:

    http://i27.tinypic.com/13zzknp.jpg
    http://i26.tinypic.com/2eplz47.jpg

    Tja,
    --
    Uno
    Uno, Jul 11, 2010
    #19
  20. On 11 July, 09:45, Uno <> wrote:
    > Richard Heathfield wrote:
    > > Uno wrote:
    > >> Richard Heathfield wrote:
    > >>> Uno wrote:


    > >>>> That's one thing that Keith has taught me over the years,

    >
    > >>> If you've been learning from Keith for years, how come you still
    > >>> don't know that getc and related functions return int, not char?

    >
    > >> Because the difference doesn't amount to a hill of beans when I'm
    > >> looking for something more important.

    >
    > > If there's anything more important than getting things right, I'd love
    > > to hear what it is.


    there are some subjects where being pedantically right is over kill.
    You can be about right or good enough. Unfortunately computer
    programming isn't one of those subjects. The computer does exactly
    what you tell it. Absolute corretness is important.

    > Where to start.  I'd like to get accurate numbers from British
    > Petroleum.


    and this has what to do with comp.lang.c?

    > I refuse to let Jim Demint stand between two governments in
    > his awesome, wretched Southern, filibustering Repugnance.


    I have no idea who Jim Demint (that's actually a name?) is. I suspect
    many posters to clc don't either. Or care. I guess he is american
    because you mentioned "filibuster" and "repugnance" (which, I
    understand is a cool dood way to refer to the Republican party by
    people who don't like them).

    > I remember you as one of the good guys while Blair was dismantling your
    > higher laws.


    I'm from the UK and I didn't even know we *had* any higher laws. What
    *is* a higher law?

    <snip>
    Nick Keighley, Jul 11, 2010
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip S

    Deciphering ISO C (Chap 6.3.2.3 - Pointers)

    Philip S, Nov 10, 2003, in forum: C Programming
    Replies:
    2
    Views:
    419
    Keith Thompson
    Nov 10, 2003
  2. John

    stat.st_mode deciphering

    John, Jun 19, 2004, in forum: C Programming
    Replies:
    1
    Views:
    723
  3. Cogito
    Replies:
    2
    Views:
    318
    Cogito
    Jul 21, 2007
  4. rodchar

    deciphering trace info

    rodchar, Aug 8, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    367
    rodchar
    Aug 18, 2008
  5. johkar
    Replies:
    3
    Views:
    1,978
    johkar
    Jul 10, 2009
Loading...

Share This Page