K
kj
In this program I'm writing I want to pre-allocate space for a but
an indeterminate (but potentially large) number of instances of a
given structure. And if this amount of memory proves insufficient,
I'll add another large chunk with realloc. Etc.
So somewhere I my code I'll have an initial pre-allocation that
looks like (very schematically):
size_t size = INITIAL_SIZE;
foo *pile_o_foo;
int next_slot = 0;
pile_o_foo = (foo *)malloc(size);
/* error check */
/* ... */
while (size <= next_slot * sizeof foo) {
size *= 2;
pile_o_foo = (foo *)realloc(pile_o_foo, size);
/* error check */
}
/* ... */
Is there a way to choose a good value for INITIAL_SIZE? Should I
just pick a number out of a hat? 101? 1001? 5091? Or should I
pick a "nice power of 2" out of a hat, like 1024 or 4096? Or is
there a more clueful way to go about this?
TIA!
kynnjo
an indeterminate (but potentially large) number of instances of a
given structure. And if this amount of memory proves insufficient,
I'll add another large chunk with realloc. Etc.
So somewhere I my code I'll have an initial pre-allocation that
looks like (very schematically):
size_t size = INITIAL_SIZE;
foo *pile_o_foo;
int next_slot = 0;
pile_o_foo = (foo *)malloc(size);
/* error check */
/* ... */
while (size <= next_slot * sizeof foo) {
size *= 2;
pile_o_foo = (foo *)realloc(pile_o_foo, size);
/* error check */
}
/* ... */
Is there a way to choose a good value for INITIAL_SIZE? Should I
just pick a number out of a hat? 101? 1001? 5091? Or should I
pick a "nice power of 2" out of a hat, like 1024 or 4096? Or is
there a more clueful way to go about this?
TIA!
kynnjo