Explain this C array syntax
There is a section of code from a learning resource that I am studying. I've come across a good bit of syntax pertaining to arrays and I would be grateful if somebody could explain them to me.
1)
AudioBufferList *bufferList;
bufferList = (AudioBufferList *) malloc (
sizeof (AudioBufferList) + sizeof (AudioBuffer) * (channelCount - 1)
);
Line 1 here I understand. What I don't understand is everything from sizeof
on. The only value that I know for sure what it is here is channelcount
. What is sizeof
doing here and what size values could AudioBufferList
and AudioBuffer
be passing to it? Are there constant values or something?
2)
bufferList->mNumberBuffers = channelCount;
// initialize the mBuffers member to 0
AudioBuffer emptyBuffer = {0};
size_t arrayIndex;
for (arrayIndex = 0; arrayIndex < channelCount; arrayIndex++) {
bufferList->mBuffers[arrayIndex] = emptyBuffer;
}
// set up the AudioBuffer structs in the buffer list
bufferList->mBuffers[0].mNumberChannels = 1;
bufferList->mBuffers[0].mDataByteSize = totalFramesInFile * sizeof (AudioUnitSampleType);
bufferList->mBuffers[0].mData = soundStructArray[audioFile].audioDataLeft;
if (2 == channelCount) {
bufferList->mBuffers[1].mNumberCh开发者_Python百科annels = 1;
bufferList->mBuffers[1].mDataByteSize = totalFramesInFile * sizeof (AudioUnitSampleType);
bufferList->mBuffers[1].mData = soundStructArray[audioFile].audioDataRight;
}
What does ->
do?
size_t
mean?
Also is mBuffers
some sort of system constant?It is not objectiveC but just C
sizeof return the size of the datatype (static by the compilator)
wikipedia reference:
- sizeof
- size_t
- pointer (->)
This is almost exclusively c syntax and if you are confused about this code, then you should get a good reference on c. Instead of explaining the various bits, I can recommend an excellent reference: C A Reference Manual.
As a few other posters noted, this is more C than Objective-C. Grabbing a good C primer (K&R's C book is always a good choice) might be helpful. However, to answer a few of your questions:
What I don't understand is everything from "sizeof" on. The only value that I know for sure what it is here is "channelcount". What is sizeof doing here and what size values could AudioBufferList and AudioBuffer be passing to it? Are there constant values or something?
sizeof
is a C operator (despite the fact that it looks like a function) that returns the size in bytes of a given data type. malloc
is a function that allocates memory, so that line is basically saying "allocate enough memory for an AudioBufferList
, an AudioBuffer
, and the channels I need, and return a pointer to the allocated memory region".
What does "->" do?
It gets a member of a struct via a pointer. In C, a struct is sort of like an Objective-C object: it has members which are basically like instance variables in Objective-C. In Objective-C, if you want to call a method myValue
, you'd do [obj myValue]
; in C, if you have a pointer to a struct and want to get the value my_value
, you'd do obj->my_value
, which is basically the same as (*obj).my_value
, which is saying "get the object pointed to by obj
, and find the member my_value
in it".
What does "size_t" mean?
size_t
is a common C datatype. It is "typedef'ed" on most platforms to be an unsigned int
or unsigned long
, and it is used to represent the sizes of objects in a platform-independent way.
Also is "mBuffers" some sort of system constant?
No; it's a member (remember, kind of like an instance variable) of bufferList
, which is probably an AudioBufferList
data type.
Starting with the great comments and readings suggested above, which are a must to learn, perhaps it whold not be unworth mentioning that the code you're trying to understand is a CoreAudio code, an API which is entirely written in plain-C, although in itself far from being plain.
An additional piece of literature you may find helpful for reading may be:
Adamson/Avila: Learning Core Audio A Hands-On Guide to Audio Programming for Mac and iOS
ISBN-13: 978-0321636843
ISBN-10: 0321636848
Hope this can facilitate your learning curve.
精彩评论