Hello all,

If we change the length of a dynamic array using the normal GC-based methods, e.g. by setting the array's .length property, we find that the array's capacity typically does not simply equal the length, but some greater value; there is excess allocation.

Question: is there a comparable phenomenon for memory that is manually allocated using malloc? That is, that if we specify a particular number of bytes to allocate, it may be rounded up to a particular larger number?

And, if so -- is there any way of guaranteeing what that larger number will be?

The reason I ask is because, suppose that I use a dynamic array as a fixed-size buffer, and that its minimum size must be n. So, I can do:

    arr.length = n;
    if (arr.capacity > arr.length)
    {
        arr.length = arr.capacity;
    }

... and get the largest possible buffer that is at least size n, but does not allocate any more memory than setting length = n.

I'm wondering if I can do something similar with manual memory allocation.

Thanks in advance for any comments and advice!

Best wishes,

    -- Joe

Reply via email to