20

Imagine the 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?

10
  • 4
    In the static case they may not be on the stack, but in a read-only section. Probably compiler dependent as well. Commented Mar 1, 2019 at 10:31
  • 4
    out of curiosity: do you have a real problem at hand, or is this just an academic exercise? (its a valid question in both cases) Commented Mar 1, 2019 at 10:35
  • 1
    @user463035818 I am having discussion during code review ;) Commented Mar 1, 2019 at 10:37
  • 4
    depending on the reviewer that can be a real problem :P Commented Mar 1, 2019 at 10:38
  • 6
    @Scheff withoutStatic builds the array each times it is invoked from static data (.LC0). withStatic uses an array whose construction has been optimized as a constant (withStatic()::arr). Commented Mar 1, 2019 at 10:53

4 Answers 4

16

Forget the array for a moment. That muddles two separate issues. You've got answers that address the lifetime and storage issue. I'll address the initialization issue.

void f() { static const int x = get_x(); // do something with x } void g() { const int x = get_x(); // do something with x } 

The difference between these two is that the first one will only call get_x() the first time that f() is called; x retains that value through the remainder of the program. The second one will call get_x() each time that g() is called.

That matters if get_x() returns different values on subsequent calls:

int current_x = 0; int get_x() { return current_x++; } 
Sign up to request clarification or add additional context in comments.

Comments

13

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.

Comments

6

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

The difference depends exactly on how you use foo().

1st case:(low probability): Your implementation is such that you will call foo() only once , maybe you have created separate function to divide code logic as practiced. Well in this case declaring as static is very bad, because a static variable or object remains in memory until programs ends . So just imagine that your variable occupying memory unnecessarily.

2nd case:(high probability): Your implementation is such that you will call foo() again and again . Then non-static object will get allocated and de allocated again and again.This will take huge amount of cpu clock cycles which is not desired .Use static in this case.

Comments

3

In this particular context, one point to consider regarding using static on a variable with initialization:

From C++17 standard:

6.7.1 Static storage duration [basic.stc.static]
...
2 If a variable with static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 15.8.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.