April 04, 2014 Manually-allocated memory and maximum array capacity | ||||
---|---|---|---|---|
| ||||
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 |
April 04, 2014 Re: Manually-allocated memory and maximum array capacity | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joseph Rushton Wakeling | Joseph Rushton Wakeling:
> Question: is there a comparable phenomenon for memory that is manually allocated using malloc?
Manually allocated memory can over-allocate, but not geometrically as arrays do.
Take a look at the difference between core.memory.extend and core.memory.realloc.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation