# AudioSampleBuffer memory allocation

**URL:** <https://forum.juce.com/t/audiosamplebuffer-memory-allocation/6830>\
**Category:** Audio Plugins\
**Created:** [May 9, 2011, 1:11pm UTC](https://forum.juce.com/t/audiosamplebuffer-memory-allocation/6830 "2011-05-09T13:11:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anima](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anima](https://forum.juce.com/u/anima)\
**Post date:** [May 9, 2011, 1:11pm UTC](https://forum.juce.com/t/audiosamplebuffer-memory-allocation/6830/1 "2011-05-09T13:11:23Z")

</div>

I’m currently trying to write an AudioSpectrumBuffer in a similar style to the AudioSampleBuffer.

Something I don’t quite understand is the memory allocation process (down at the first hurdle or what). Why exactly is the memory stored as a

```auto

```

first, then

```auto

```

later in allocateData().

I guess my question is why isn’t it float instead of char in the first place.  
Obviously there is a reason for this but I don’t know what it is! 🙂

---

<div class="post-metadata">

**Author:** ![andrewj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/andrewj/32/13428_2.png) [@andrewj](https://forum.juce.com/u/andrewj)\
**Post date:** [May 9, 2011, 1:51pm UTC](https://forum.juce.com/t/audiosamplebuffer-memory-allocation/6830/2 "2011-05-09T13:51:43Z")

</div>

I’m pretty new to this, but my understanding is that HeapBlock is used as a convenient and leak safe way of allocating memory. reinterpret\_cast \<float\*\*\> then changes the type of the pointer so that it handles a 2D array of floats.

First dimension is the audio channel number, second dimension is the sample index (up to buffer size). Before the reinterpret\_cast you should see a bit of code which allocates the required amount of memory to satisfy these dimensions.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [May 9, 2011, 1:55pm UTC](https://forum.juce.com/t/audiosamplebuffer-memory-allocation/6830/3 "2011-05-09T13:55:14Z")

</div>

The memory allocation for AudioSampleBuffer has, at the beginning of the allocation, an array of float pointers, then followed by the actual sample data for each channel. The array of float pointer is what is returned from getArrayOfChannels().

---

<div class="post-metadata">

**Author:** ![anima](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anima](https://forum.juce.com/u/anima)\
**Post date:** [May 9, 2011, 2:21pm UTC](https://forum.juce.com/t/audiosamplebuffer-memory-allocation/6830/4 "2011-05-09T14:21:54Z")

</div>

Just saw this in the documentation.

> [@](#):
>
> A HeapBlock object can be treated in pretty much exactly the same way as an char\*, but as long as you allocate it on the stack or as a class member, it’s almost impossible for it to leak memory.

Pretty much clears up the confusion I was having. I understand the rest. Thanks for the help.
