bartop
bartop

Reputation: 10315

Does static make a difference for a const local variable?

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?

Upvotes: 20

Views: 1616

Answers (4)

P.W
P.W

Reputation: 26800

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.

Upvotes: 3

Pete Becker
Pete Becker

Reputation: 76235

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++; }

Upvotes: 16

Abhishek Garg
Abhishek Garg

Reputation: 308

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.

Upvotes: 6

eerorika
eerorika

Reputation: 238301

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.

Upvotes: 13

Related Questions