1

There's some code where it creates a float array like this:

mData = new float[channelCount * maxFrames];

then it does

memcpy(&mData[sampleIndex],
                   buffer,
                   (numSamples * sizeof(float)));

What does &mData[sampleIndex] mean? Well, we have a float array, we take an element of that array, and then take the address of that element. Wouldn't the address of that element be mData + sampleIndex?

What if I wanted to change memcpy by a for loop? I did this and it worked:

            for (int i=0; i< numSamples * sizeof(float); i++) {
                (&mData[sampleIndex])[i] = buffer[i];
            } 

but I don't know what (&mData[sampleIndex])[i] means. Should it be mData + sampleIndex + i?

This code is supposed to work to record microfone wav data, so we should be able to store things in multiple channels. How this code manages such channels?

2
  • 1
    Your loop is wrong, it should be for (int i=0; i< numSamples; i++) since you are assigning whole floats and not individual bytes, assuming buffer[] is an array of float to match that mData is an array of float. Commented Sep 15, 2020 at 0:04
  • memcpy(&mData[sampleIndex], buffer, (numSamples * sizeof(float))); is useful when you want to memcpy data into the middle of an array. Commented Sep 15, 2020 at 0:04

2 Answers 2

2

What does &mData[sampleIndex] mean? Well, we have a float array, we take an element of that array, and then take the address of that element. Wouldn't the address of that element be mData + sampleIndex?

Yes.

What if I wanted to change memcpy by a for loop?

Your loop doesn't quite do the same thing. memcpy is copying numSamples * sizeof(float) bytes while your loop is copying numSamples * sizeof(float) floats. Since a float consists of multiple bytes (on most systems), this may result in a buffer overflow.

but I don't know what (&mData[sampleIndex])[i] means. Should it be mData + sampleIndex + i?

It's not quite the same. (&mData[sampleIndex])[i] would be equal to *(mData + sampleIndex + i)

How this code manages such channels?

This code simply copies values from one array into another. It doesn't "manage" anything.

Sign up to request clarification or add additional context in comments.

2 Comments

How can I copy numSamples * sizeof(float) bytes instead of floats?
@GuerlandoOCs by copying numSamples floats.
1

The syntax array[index] is the same as *(array + index), thus:

&mData[sampleIndex]

is the same as:

&(*(mData + sampleIndex))

Which is simply:

mData + sampleIndex

And so, (&mData[sampleIndex])[i] is getting a float* pointer to the mData element at index sampleIndex, and then applying the index i to that pointer. So yes, in this case:

(&mData[sampleIndex])[i] = buffer[i];

is the same as:

*(mData + sampleIndex + i) = *(buffer + i);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.