xyz xyz - 6 months ago 29
C Question

Working with large local arrays: is there a faster way than malloc?

I am working with large arrays in C for numerical calculations.

Within one of the functions I make use of some temporary local (disposable) arrays. Normally I would just declare these as

double v[N]
and then not worry about having to free the memory manually.

The problem that I have noticed is that when my N gets very large (greater than 4 million) my program fails to declare the variable. Hence I resorted to
which allows me to run the program successfully. The problem is that my execution time almost doubles from 24s to 40s when I use the dynamic allocation (for a smaller N).

It is possible for me to modify the code to avoid creating these temporary arrays in the first place, however it would impact on the readability of the code and coding effort. I am currently using a preprocessor macro to access the vector like a 2D matrix. Is there another solution that will avoid the CPU cost whilst allowing me to save the data like a matrix?

Answer Source

When you declare a variable local to the method you are working with automatic allocated variables which go on the stack and unfortunately the stack size is limited. Using malloc means that the variable will be allocated on heap and the time difference is what you pay for that dynamic allocation.

I see two possible solutions:

  • use a static global array (and reuse it when necessary) so that the compiler will be able to optimize accesses to it
  • change the stack size so that you won't have problems with larger arrays local to your functions, this can be done even dynamically, take a look here.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download