Does static makes difference for const local variable?What is the difference between const and readonly?What...
Does windows 10s telemetry include sending *.docs if word crashed
How to remove blank page before my new report chapter?
Indirectly access environment variable
Why do stocks necessarily drop during a recession?
Can I string the D&D Starter Set campaign into another module, keeping the same characters?
how to acknowledge an embarrasing job interview, now that I work directly with the interviewer?
Can we use the stored gravitational potential energy of a building to produce power?
Why isn't there a non-conducting core wire for high-frequency coil applications
How to prevent cleaner from hanging my lock screen in Ubuntu 16.04
Can a hotel cancel a confirmed reservation?
Dilemma of explaining to interviewer that he is the reason for declining second interview
Why Normality assumption in linear regression
Citing paywalled articles accessed via illegal web sharing
Can making a creature unable to attack after it has been assigned as an attacker remove it from combat?
How can I deliver in-universe written lore to players without it being dry exposition?
Lick explanation
Cookies - Should the toggles be on?
Is there any other number that has similar properties as 21?
Highly technological aliens land nuclear fusion powered ships in medieval city and slaughter everyone, using swords?
What is the most triangles you can make from a capital "H" and 3 straight lines?
How can animals be objects of ethics without being subjects as well?
Why is working on the same position for more than 15 years not a red flag?
How do I say "Brexit" in Latin?
Can a person refuse a presidential pardon?
Does static makes difference for const local variable?
What is the difference between const and readonly?What are the differences between a pointer variable and a reference variable in C++?Are static class variables possible?Difference between static class and singleton pattern?What does “static” mean in C?What is the difference between const int*, const int * const, and int const *?Static variables in JavaScriptWhy are static variables considered evil?Difference between `constexpr` and `const`Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
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?
c++ static const
|
show 4 more comments
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?
c++ static const
2
In thestatic
case they may not be on the stack, but in a read-only section. Probably compiler dependent as well.
– Matthieu Brucher
2 hours ago
2
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)
– user463035818
2 hours ago
1
@user463035818 I am having discussion during code review ;)
– bartop
2 hours ago
2
depending on the reviewer that can be a real problem :P
– user463035818
2 hours ago
2
@ScheffwithoutStatic
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
).
– YSC
1 hour ago
|
show 4 more comments
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?
c++ static const
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?
c++ static const
c++ static const
edited 2 hours ago
bartop
asked 2 hours ago
bartopbartop
2,957826
2,957826
2
In thestatic
case they may not be on the stack, but in a read-only section. Probably compiler dependent as well.
– Matthieu Brucher
2 hours ago
2
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)
– user463035818
2 hours ago
1
@user463035818 I am having discussion during code review ;)
– bartop
2 hours ago
2
depending on the reviewer that can be a real problem :P
– user463035818
2 hours ago
2
@ScheffwithoutStatic
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
).
– YSC
1 hour ago
|
show 4 more comments
2
In thestatic
case they may not be on the stack, but in a read-only section. Probably compiler dependent as well.
– Matthieu Brucher
2 hours ago
2
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)
– user463035818
2 hours ago
1
@user463035818 I am having discussion during code review ;)
– bartop
2 hours ago
2
depending on the reviewer that can be a real problem :P
– user463035818
2 hours ago
2
@ScheffwithoutStatic
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
).
– YSC
1 hour ago
2
2
In the
static
case they may not be on the stack, but in a read-only section. Probably compiler dependent as well.– Matthieu Brucher
2 hours ago
In the
static
case they may not be on the stack, but in a read-only section. Probably compiler dependent as well.– Matthieu Brucher
2 hours ago
2
2
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)
– user463035818
2 hours ago
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)
– user463035818
2 hours ago
1
1
@user463035818 I am having discussion during code review ;)
– bartop
2 hours ago
@user463035818 I am having discussion during code review ;)
– bartop
2 hours ago
2
2
depending on the reviewer that can be a real problem :P
– user463035818
2 hours ago
depending on the reviewer that can be a real problem :P
– user463035818
2 hours ago
2
2
@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
).– YSC
1 hour ago
@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
).– YSC
1 hour ago
|
show 4 more comments
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
There is one difference with 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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54942664%2fdoes-static-makes-difference-for-const-local-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited 1 hour ago
answered 1 hour ago
eerorikaeerorika
83.8k662128
83.8k662128
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 1 hour ago
Abhishek GargAbhishek Garg
1168
1168
add a comment |
add a comment |
There is one difference with 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.
add a comment |
There is one difference with 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.
add a comment |
There is one difference with 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.
There is one difference with 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.
answered 54 mins ago
P.WP.W
15.4k31453
15.4k31453
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54942664%2fdoes-static-makes-difference-for-const-local-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
In the
static
case they may not be on the stack, but in a read-only section. Probably compiler dependent as well.– Matthieu Brucher
2 hours ago
2
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)
– user463035818
2 hours ago
1
@user463035818 I am having discussion during code review ;)
– bartop
2 hours ago
2
depending on the reviewer that can be a real problem :P
– user463035818
2 hours ago
2
@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
).– YSC
1 hour ago