Unclear on static scope destruction


Unclear on static scope destruction



I'm unclear on how static can be used dependent on scope.


int Foo( ) // returns int based on number of times called
{
static int i;
return ++i;
}



This makes sense. But what about an inner scope? When is int i's scope destroyed?


int Foo( )
{
{
static int i;
return ++i;
}
}



Would this function the same, or would it int i's scope be destroyed after the function's end?


int Foo( )
{
for ( int x; x < 10; x++ )
{
static int i;
return ++i;
}
}



How about for this?





Well, isn't a function one scope level deep from a global? And yet a static there seems to have no issue retaining construction. Why would a static being further deep be any different? And of course, running your code would probably have answered your question regardless.
– WhozCraig
Jun 30 at 10:02



static





The terminology in your question is not clear. Scopes can end, but they are not "destroyed". Variables can be destroyed, but static variables are not destroyed until the program ends (which is more or less their defining characteristic). Could you rephrase your question using the more usual terminology?
– JaMiT
Jun 30 at 11:55





The phrase you are using "i's scope be destroyed" should be rephrased to "i's scope ends" or "i goes out of scope".
– Eljay
Jun 30 at 13:02









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux