Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Definition of perror() ?

Reply
Thread Tools

Definition of perror() ?

 
 
Disc Magnet
Guest
Posts: n/a
 
      01-03-2011
Is perror(s) just a macro to fprintf(stderr, "%s: %s\n", s,
strerror(errno)) ?
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-03-2011
Disc Magnet <(E-Mail Removed)> writes:

> Is perror(s) just a macro to fprintf(stderr, "%s: %s\n", s,
> strerror(errno)) ?


Literally? No. But the effects are similar in the common case.
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2011
Disc Magnet <(E-Mail Removed)> writes:
> Is perror(s) just a macro to fprintf(stderr, "%s: %s\n", s,
> strerror(errno)) ?


perror() is a standard library function. For the actual definition,
grab a copy of
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>
(that's the latest draft of the C standard) and read section 7.19.10.4.

Like any standard library function, it may additionally be defined as
a macro.

For a couple of reasons, the definition you present would not be
legal; first, the reference to s needs to be parenthesized, and
second, the behavior is different if s is a null pointer or points
to a null character.

I'm not sure how I'd define perror() as a macro that doesn't
potentially evaluate its argument more than once. And there's
probably not much reason to bother doing so; a call to perror()
isn't likely to be a performance bottleneck.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-03-2011
On 1/3/2011 5:45 PM, Keith Thompson wrote:
> Disc Magnet<(E-Mail Removed)> writes:
>> Is perror(s) just a macro to fprintf(stderr, "%s: %s\n", s,
>> strerror(errno)) ?

>
> perror() is a standard library function. For the actual definition,
> grab a copy of
> <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>
> (that's the latest draft of the C standard) and read section 7.19.10.4.
>
> Like any standard library function, it may additionally be defined as
> a macro.
>
> For a couple of reasons, the definition you present would not be
> legal; first, the reference to s needs to be parenthesized, and
> second, the behavior is different if s is a null pointer or points
> to a null character.


Another reason it wouldn't be legal is that you can use perror()
in a module that #include's <stdio.h> but omits <string.h>, which is
where strerror() is declared.

And there's yet another reason, this one truly nit-picky: "The
implementation shall behave as if no library function calls the
strerror function." If strerror() uses a static internal buffer to
hold the message string, and overwrites the buffer at each call, the
macro-ized perror() would cause a forbidden overwrite. That is, in

const char *message = strerror(EDOM);
strcpy (big_enough_array, message);
errno = ERANGE;
perror("This is a range error");
assert (strcmp(message, big_enough_array) == 0);

.... the assertion must not fail, as it (almost certainly) would if
perror() called strerror() and the latter overwrote its buffer.

> I'm not sure how I'd define perror() as a macro that doesn't
> potentially evaluate its argument more than once. And there's
> probably not much reason to bother doing so; a call to perror()
> isn't likely to be a performance bottleneck.


Indeed. A program whose performance is limited by the speed at
which it can report its own errors most likely has problems other
than performance.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-04-2011
On 1/4/2011 1:02 PM, Kenneth Brody wrote:
> On 1/3/2011 6:30 PM, Eric Sosman wrote:
>> On 1/3/2011 5:45 PM, Keith Thompson wrote:

> [...]
>>> I'm not sure how I'd define perror() as a macro that doesn't
>>> potentially evaluate its argument more than once. And there's
>>> probably not much reason to bother doing so; a call to perror()
>>> isn't likely to be a performance bottleneck.

>>
>> Indeed. A program whose performance is limited by the speed at
>> which it can report its own errors most likely has problems other
>> than performance.

>
> Well, in college, we had a FORTRAN compiler which was very efficient at
> generating non-optimized code. (The point being, why waste CPU cycles
> optimizing a program which is likely to not compile the first N times,
> and which would likely be run only a single time once it actually did
> compile.) It would generate extremely verbose, and extremely numerous,
> error messages.


So it spat out lots of messages about *your* errors, not about
"its own errors," right?

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
robertwessel2@yahoo.com
Guest
Posts: n/a
 
      01-04-2011
On Jan 4, 12:02*pm, Kenneth Brody <(E-Mail Removed)> wrote:
> On 1/3/2011 6:30 PM, Eric Sosman wrote:
>
> > On 1/3/2011 5:45 PM, Keith Thompson wrote:

> [...]
> >> I'm not sure how I'd define perror() as a macro that doesn't
> >> potentially evaluate its argument more than once. And there's
> >> probably not much reason to bother doing so; a call to perror()
> >> isn't likely to be a performance bottleneck.

>
> > Indeed. A program whose performance is limited by the speed at
> > which it can report its own errors most likely has problems other
> > than performance.

>
> Well, in college, we had a FORTRAN compiler which was very efficient at
> generating non-optimized code. *(The point being, why waste CPU cycles
> optimizing a program which is likely to not compile the first N times, and
> which would likely be run only a single time once it actually did compile..)
> * It would generate extremely verbose, and extremely numerous, error messages.



Good old WATFOR and WATFIV... Typically these student jobs would be
compile-link-and-go - the executable would never even get written to
disk (after the linker finished, it would just be run from memory).
So even if the student wanted to run the program more than once, it
would be recompiled, since it wouldn't have been saved anyway. And of
course, typical student problems were pretty small anyway, so not only
was the program only run once, it would only process a dataset with
(at most) a few hundred entries.
 
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
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
xpat definition is no help Cheechat Firefox 8 03-28-2005 05:44 AM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments