Re: Need help with C syntax in examples from 'Learning Core Audio' book
Re: Need help with C syntax in examples from 'Learning Core Audio' book
- Subject: Re: Need help with C syntax in examples from 'Learning Core Audio' book
- From: Paul Davis <email@hidden>
- Date: Fri, 18 May 2012 23:26:10 -0400
its hard to say this without being too inflammatory, but this habit of
using 4 byte values to store a "unique identifier" but doing so by
alternately considering it to be a 4 character array and/or a 32 bit
integer, is the kind of ugly programming that is the trademark of ...
well, lets just say "operating systems that do not share the lineage
that OS X is derived from". its incredibly disappointing to see it
continue to play a central role in an API started in the 2000's.
please, programmers who are new to this stuff: consider this sort of
thing an aberration and play this sort of game only to the extent that
an existing API requires you to.
if you want unique identifiers, use either genuine UUID's (aka GUID's)
or at worst, an actual char id[4], and do not attempt to cast the
latter to an integer or anything else.
your friendly neighbourhood "i learned unix in 1986" hacker,
--p
On 5/18/12, David Duncan <email@hidden> wrote:
>
> On May 18, 2012, at 6:54 PM, Raimondas K. wrote:
>
>> On Wed, May 16, 2012 at 7:29 AM, Brian Lambert <email@hidden>
>> wrote:
>>> It is taking the value of error and converting it from the host’s native
>>> byte order to big-endian format.
>>> CFSwapInt32HostToBig(error);
>>>
>>> It then writes the binary value of the error in big-endian format to
>>> bytes
>>> 1,2,3,4 of the 20 character error string.
>>>
>>> (errorString + 1) is a pointer to byte 1 of the 20 character string.
>>>
>>> (UInt32 *)(errorString + 1) casts this pointer to be a pointer to a
>>> UInt32.
>>>
>>> *(UInt32 *)(errorString + 1) dereferences this UInt32 pointer, allowing
>>> it
>>> to be used as an l-value.
>>
>>
>> I am surprised this actually works -- no byte alignment issues?
>
> Depends on compiler and CPU architecture. I'm fairly certain this won't be
> an issue on any combination that Mac OS X or iOS supports today.
>
>>
>> =R.
>>
>>>
>>> So, this allows one to assign the UInt32 value
>>> of CFSwapInt32HostToBig(error) into bytes 1,2,3,4 of the 20 character
>>> error
>>> string as though it was a UInt32.
>>>
>>> Then the code goes on to examine the bytes using isprint.
>>>
>>>
>>> On Wed, May 16, 2012 at 4:00 AM, Ben <email@hidden>
>>> wrote:
>>>>
>>>> I'm running through the examples in the book 'Learning Core Audio' and I
>>>> came across these two lines...
>>>>
>>>>
>>>> char errorString[20];
>>>> // see if it appears to be a 4-char-code
>>>> *(UInt32 *)(errorString + 1) = CFSwapInt32HostToBig(error);
>>>>
>>>> The second line looks pretty alien to me (rusty on C), can someone
>>>> explain
>>>> what is going on please? I understand what the function
>>>> CFSwapInt32HostToBig() is doing, but what is going on with errorString?
>>>>
>>>> Thanks in advance.
>>>> _______________________________________________
>>>> Do not post admin requests to the list. They will be ignored.
>>>> Coreaudio-api mailing list (email@hidden)
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>>
>>>> This email sent to email@hidden
>>>
>>>
>>>
>>> _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> Coreaudio-api mailing list (email@hidden)
>>> Help/Unsubscribe/Update your Subscription:
>>>
>>> This email sent to email@hidden
>>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Coreaudio-api mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
> --
> Reality is what, when you stop believing in it, doesn't go away.
> Failure is not an option. It is a privilege reserved for those who try.
>
> David Duncan
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Coreaudio-api mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden