0

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?

7
  • Do you need to maintain the contents of the array across iterations? Commented Sep 12, 2014 at 11:20
  • you could save the current size of a through iterations and only realloc when you need more size Commented Sep 12, 2014 at 11:21
  • No. 'a' filled with 'm' new values after each realloc Commented Sep 12, 2014 at 11:22
  • Allocate one time the biggest size you need. Then in each iteration reset the values to default ones and then use it like you need it. Commented Sep 12, 2014 at 11:26
  • @user743414 Its not possible to get the biggest 'm' value before the iterations Commented Sep 12, 2014 at 11:29

3 Answers 3

3

Firstly, realloc takes 2 parameters. First is the original pointer and the second is the new size. You are trying to pass the size as the original pointer and the code shouldn't compile.

Secondly, the obligatory reminder: Don't optimize prematurely. Unless you've measured and found that the allocations are a bottleneck, just use std::vector.

Sign up to request clarification or add additional context in comments.

Comments

1

Few issues I have noticed are:

  • Realloc should be used in case you want older values remain in the memory, if you didn't bother about old values as mentioned in one of your comment use just alloc.

  • Please check size of already allocated memory before allocating again, if allocated memory is insufficient for new data then only allocate new memory.

Please refer to the sample code which will taking care of above mentioned problems:

int size = 2; int *a = (int*)std::alloc(size * sizeof(int)); for(int i=0; i<N; ++i) { int m = calculate_enough_size(); if(m > size) { size = m; std::free(a); a = (int*)std::alloc(size * sizeof(int)); } // ... } std::free(a); 

Also you can further optimized memory allocation by allocating some extra memory, e.g:

size = m*2; //! 

To better understand this step let's take an example suppose m = 8, then you will allocate memory = 16, so when now m changes to 10, 12 up-to 16 there is no need to allocate memory again.

Comments

0

If you can get all the sizes beforehand, allocate the biggest you need before the cycle and then use as much as needed.

If, on the other hand, you can not do that, then reallocation is a good solution, I think.

You can also further optimize your solution by reallocating only when a bigger size is needed:

int size = 0; for(int i = 0; i < N; ++i) { int new_size = calculate_enough_size(); if ( new_size > size ){ a = (int*)std::realloc(new_size * sizeof(int)); size = new_size; } // ... } 

Like this you will need less reallocations (half of them in a randomized case).

4 Comments

Its not possible to get the biggest 'm' value before the iterations
"reallocating only when a bigger size is needed". why is it better then realloc each iteration?
@user1312837 Because you will need less reallocs.
Realloc will possibly move your old data to a new location aka copying, but you don't need to reuse your old data. So do less realloc is better

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.