# AudioSampleBuffer still broken

**URL:** <https://forum.juce.com/t/audiosamplebuffer-still-broken/3329>\
**Category:** General JUCE discussion\
**Created:** [November 3, 2008, 10:20pm UTC](https://forum.juce.com/t/audiosamplebuffer-still-broken/3329 "2008-11-03T22:20:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [November 3, 2008, 10:20pm UTC](https://forum.juce.com/t/audiosamplebuffer-still-broken/3329/1 "2008-11-03T22:20:51Z")

</div>

```auto
void AudioSampleBuffer::setSize (const int newNumChannels,

                                 const int newNumSamples,

                                 const bool keepExistingContent,

                                 const bool clearExtraSpace,

                                 const bool avoidReallocating) throw()

{

    jassert (newNumChannels > 0);

    if (newNumSamples != size || newNumChannels != numChannels)

    {

        const int newTotalBytes = newNumChannels * newNumSamples * sizeof (float) + 32;

        if (keepExistingContent)

        {

            float* const newData = (clearExtraSpace) ? (float*) juce_calloc (newTotalBytes)

                                                     : (float*) juce_malloc (newTotalBytes);

            const int sizeToCopy = sizeof (float) * jmin (newNumSamples, size);

            for (int i = jmin (newNumChannels, numChannels); --i >= 0;)

            {

                memcpy (newData + i * newNumSamples,

                        channels[i],

                        sizeToCopy);

            }

            juce_free (allocatedData);

            allocatedData = newData;

            allocatedBytes = newTotalBytes;

        }

        else

        {

            if (avoidReallocating && allocatedBytes >= newTotalBytes)

            {

                if (clearExtraSpace)

                    zeromem (allocatedData, newTotalBytes);

            }

            else

            {

                juce_free (allocatedData);

                allocatedData = (clearExtraSpace) ? (float*) juce_calloc (newTotalBytes)

                                                  : (float*) juce_malloc (newTotalBytes);

                allocatedBytes = newTotalBytes;

            }

        }

        size = newNumSamples;

        if (newNumChannels > numChannels)

            channels = (float**) juce_realloc (channels, (newNumChannels + 1) * sizeof (float*));

        numChannels = newNumChannels;

        float* chan = allocatedData;

        for (int i = 0; i < newNumChannels; ++i)

        {

            channels[i] = chan;

            chan += size;

        }

        channels [newNumChannels] = 0;

    }

}

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 4, 2008, 4:55pm UTC](https://forum.juce.com/t/audiosamplebuffer-still-broken/3329/2 "2008-11-04T16:55:12Z")

</div>

Thanks - I bumped into that one too, and checked in a fix.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 4, 2008, 5:07pm UTC](https://forum.juce.com/t/audiosamplebuffer-still-broken/3329/3 "2008-11-04T17:07:25Z")

</div>

Thanks - I bumped into that one too, and checked in a fix.

---

<div class="post-metadata">

**Author:** ![mrblasto](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@mrblasto](https://forum.juce.com/u/mrblasto)\
**Post date:** [November 14, 2008, 6:50pm UTC](https://forum.juce.com/t/audiosamplebuffer-still-broken/3329/4 "2008-11-14T18:50:18Z")

</div>

I’m curious–exactly what is broken in AudioSampleBuffer? I’m afraid I can’t tell from examining the code sample or from doing differences on the code. In fact, the main thing that’s changed is that reallocation is done on newNumChannels + 1 instead of numNewChannels and I’m wondering why that is necessary.

Thanks – Jeff.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 18, 2008, 10:24am UTC](https://forum.juce.com/t/audiosamplebuffer-still-broken/3329/5 "2008-11-18T10:24:13Z")

</div>

As far as I know nothing is now broken in that class. The reason it has an extra element in the array is just so that the array of pointers to channels is zero-terminated, which is sometimes handy when you’re using it in plugins, etc.
