Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Engineering a list container. Part 1.

Reply
Thread Tools

Engineering a list container. Part 1.

 
 
Öö Tiib
Guest
Posts: n/a
 
      12-26-2013
On Thursday, 26 December 2013 18:58:30 UTC+2, James Kuyper wrote:
> On 12/24/2013 10:46 PM, glen herrmannsfeldt wrote:
>
> > As far as I know, many compilers will use FSQRT in place of a
> > call to sqrt(). Presumably if no reference to errno is in sight.
> >
> > I am not sure how far away you would put a reference to errno
> > for the compiler to notice.

>
> errno acts like a global variable - checking the value of errno could
> occur in a different translation unit from the sqrt() call, so the
> compiler alone does not, in general, have enough information to perform
> such an optimization.


Standard has such footnote: "Thus, a program that uses errno for error
checking should set it to zero before a library function call, then inspect
it before a subsequent library function call."

There certainly can be case when it is hard to decide but in common
code it does not take rocket science to detect that the program does
not behave like said above between subsequent library function calls.



 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-26-2013
On 12/26/2013 11:58 AM, James Kuyper wrote:
> On 12/24/2013 10:46 PM, glen herrmannsfeldt wrote:
>
>> As far as I know, many compilers will use FSQRT in place of a
>> call to sqrt(). Presumably if no reference to errno is in sight.
>>
>> I am not sure how far away you would put a reference to errno
>> for the compiler to notice.

>
> errno acts like a global variable - checking the value of errno could
> occur in a different translation unit from the sqrt() call, so the
> compiler alone does not, in general, have enough information to perform
> such an optimization.


The compiler can (or should) be able to discern many cases
in which `errno' is clearly not being inspected. My example was

y = sqrt(x); // might set errno
// errno not inspected here
z = log(y); // might set errno

Any program that's interested in the `errno' output from sqrt()
and fails to examine it before calling log() is erroneous. The
implementation is not obliged to set `errno' as part of sqrt()
only to see it overwritten as part of log().

Your observation is valid for any `errno' value produced
by log(), unless the context includes further library calls
that might contaminate it in turn.

It appears to be less well known that almost any library
function may change `errno', even those that don't actually
encounter any errors. FAQ 12.24 explains one such example.
Another anti-pattern one occasionally sees goes something like

stream = fopen(filename, "r");
if (stream == NULL) {
perror(filename);
return errno;
}

.... where an `errno' from an fopen() failure (if there is one;
fopen() is not required to set `errno' though many versions do)
may have been overwritten during perror(). If the idea is to
return fopen()'s `errno' value, use something like

if (stream == NULL) {
int errno_save = errno;
perror(filename);
return errno_save;
}

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
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
A container library in C Part 4. The arraylist container jacob navia C Programming 10 10-01-2009 02:26 PM
A container library in C: Part 3. List container implementation jacob navia C Programming 5 09-28-2009 08:19 PM
Call for Papers: World Congress on Engineering WCE 2007 (IAENG conferences with Engineering Letters) imecs__2007@iaeng.org Java 0 12-19-2006 06:51 AM
CFP: Special Issue on Web Engineering (The journal Engineering Letters) imecs_2006@iaeng.org Java 0 01-07-2006 04:48 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments