Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Gestalt selector for Intel chips?



On Jun 29, 2006, at 1:00 PM, George Warner wrote:

On Wed, 28 Jun 2006 21:41:44 -0700, John Stiles <email@hidden>
wrote:

Historically, it was a global.
In modern times, "errno" is a macro that calls a private function (I
think it's called __error() or something). This allows it to be
thread-safe. By and large, though, you can treat it as if it were a
global variable.

Watch out thou that if you need it's value multiple times you should assign
it to a local variable because calling __error() clears its value:


    int error = errno;  // <== this expands to "(*__error())"
    fprintf( stderr, "%s: error: %ld (0x%08lX) \"%s\".\n",
        __PRETTY_FUNCTION__, error, error, strerror( error ));

If you used errno instead of error in the fprintf line then only the first
call would return the error. The last two times it's called it would return
zero (noErr).

Actually, __error() doesn't clear the value, and the example above will work fine if you use errno throughout instead of caching it.


The thing to remember with errno is not its implementation or that it's a macro, but instead two things:
1) Calling functions may cause its value to be changed. So while you don't need to cache it whenever you use it, if you need to call other functions before you use its value you might want to cache it. If those functions don't set errno, you're fine.
2) errno is undefined if you check it after a function which succeeds. In other words, don't check for errno == 0 to see if a function succeeded. Instead, check the return value of the function and only check errno if the function failed.


Hope this helps,
Eric

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Gestalt selector for Intel chips? (From: George Warner <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.