Does static makes difference for const local variable?

  • A+
Category:Languages

Imagine following declaration:

void foo(){     const std::array<int, 80000> arr = {/* a lot of different values*/};     //do stuff } 

And a second one:

void foo(){     static const std::array<int, 80000> arr = {/* a lot of different values*/};     //do stuff } 

What are the possible performance differences between these two if any? And is there any danger associated with any of these solutions?

 


And is there any danger associated with any of these solutions?

Non-static is dangerous because the array is huge, and the memory reserved for automatic storage is limited. Depending on the system and configuration, that array could use about 30% of the space available for automatic storage. As such, it greatly increases the possibility of stack overflow.

While an optimiser might certainly avoid allocating memory on the stack, there are good reasons why you would want your non-optimised debug build to also not crash.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: