deciphering a macro

U

Uno

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,
 
E

Eric Sosman

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."
 
U

Uno

Eric said:
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;
 
V

Vincenzo Mercuri

Uno said:
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.
 
U

Uno

Vincenzo said:
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.

?
 
F

Francois Grieu

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
 
U

Uno

Francois said:
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?
 
F

Francois Grieu

Le 01/07/2010 09:52, Uno a écrit :
Francois said:
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
 
E

Eric Sosman

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

Uno

Eric Sosman wrote:

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.;-)
 
E

Eric Sosman

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."
 
U

Uno

Eric said:
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?
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."
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.
 
U

Uno

Richard said:
Uno wrote:



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

Keith Thompson

Uno said:
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.

[...]
 
U

Uno

Keith said:
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?
 
U

Uno

Richard said:
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.
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,
 
N

Nick Keighley

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top