Reputation: 407
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 malloc()
and free()
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?
Upvotes: 0
Views: 1090
Reputation: 133567
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:
static
global array (and reuse it when necessary) so that the compiler will be able to optimize accesses to itUpvotes: 1