Really basic question here:
I’ve seen a bunch of CoreAudio code that uses memset when dealing with structs and I haven’t been able to figure out why. This is from a .m ObjC file.
Here’s the line from the code block below:
memset(&clientFormat, 0, sizeof(clientFormat));
//—————
AudioStreamBasicDescription clientFormat;
if ( sourceFormat.mFormatID == kAudioFormatLinearPCM ) {
clientFormat = sourceFormat;
} else {
memset(&clientFormat, 0, sizeof(clientFormat));
int sampleSize = sizeof(AudioSampleType);
clientFormat.mFormatID = kAudioFormatLinearPCM;
clientFormat.mFormatFlags = kAudioFormatFlagsCanonical;
clientFormat.mBitsPerChannel = 8 * sampleSize;
clientFormat.mChannelsPerFrame = sourceFormat.mChannelsPerFrame;
clientFormat.mFramesPerPacket = 1;
clientFormat.mBytesPerPacket = clientFormat.mBytesPerFrame = sourceFormat.mChannelsPerFrame * sampleSize;
clientFormat.mSampleRate = sourceFormat.mSampleRate;
}
As called, it sets the memory at
&clientFormat, with a length ofsizeof(clientFormat), to0. This is required because memory in C is not cleared to0after allocation withmalloc()and most other allocation functions.