I need to allocate space for a temporary array once per iteration. I try to use realloc each iteration to optimize memory using. Like that:
int *a = (int*)std::alloc(2 * sizeof(int));
for(int i=0; i<N; ++i)
{
int m = calculate_enough_size();
a = (int*)std::realloc(m * sizeof(int));
// ...
}
std::free(a);
N
is a big number, 1000000 for example. There are example m
values per iteration: 8,2,6,10,4,8
Am I doing right when I realloc a
at each iteration? Does it prevent redundant memory allocation?