Home > Guides > Programming > C/C++

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Static Storage Duration and Thread Storage Duration

Last updated Jun 26, 2009.

Global objects and objects in a namespace scope, static data members of a class, and local static objects in functions reside static storage duration. An object with static storage duration resides in the same memory address throughout the program's execution. Every such object is constructed only once during the lifetime of the program. By default, static data is initialized to binary zeros. Static objects with a nontrivial constructor or an explicit dynamic initializer undergo a second initialization phase called dynamic initialization.

The scope of an object declared static in a function is restricted to that function. Objects with static storage duration appear in the following examples:

int num=0; //global variables have static storage
extern int x; //also global, defined in a separate translation unit
int func()
{
  static int calls; //local static. initialized to 0 by default
  x=calls;
  return ++calls;
}

class C
{
private:
  static bool b;
};

namespace NS
{
  std::string str; //str has static storage
}

The extern Storage Class Specifier

The extern specifier can be applied only to the names of objects and to functions. The extern specifier cannot be used in the declaration of class members or function parameters. Identifiers declared extern have external linkage, meaning they are visible from all translation units of the same program.

A name declared in a namespace scope without a storage-class-specifier has external linkage unless it has internal linkage because of a previous declaration and provided it is not declared const. Objects declared const and not explicitly declared extern have internal linkage, meaning they are visible only from the translation unit in which they are declared.

Notice that the extern keyword is overloaded. It can also be used in explicit-instantiations of templates and in and linkage-specifications, but it is not a storage-class-specifier in such contexts.

The thread_local Storage Duration

The thread_local specifier indicates that the named object or reference has thread storage duration. thread_local shall be applied only to the names of objects or references of namespace scope and to the names of objects or references of block scope that also specify static as their storage class. The thread_local storage class is a new C++09 feature. It's discussed in detail here.

Related Resources

There are currently no related podcasts. Please check back later.

Emily Nave#TuesdayTrivia: WindowsRT, Tiles and Charms...Oh My! Win "Building Windows 8 Apps with C# and XAML" eBook by Jeremy Likness
By Emily NaveNovember 6, 2012Comments
Software developers know the thing you can count on in your career is change. Microsoft is redefining the way apps are developed -- are you ready to get on board?

Rachel BaylessInformIT's 17 Days of Giveaways
By Rachel BaylessJuly 3, 2012Comments

Get ready for a month full of giveaways. From July 9 through the end of the month, InformIT will be having 17 days of giveaways. Each week has a theme to make sure that there’s something YOU will be excited to win!

John  TraenkenschuhDennis Ritchie--May He Rest in Peace...
By John TraenkenschuhOctober 16, 2011Comments
Time to Remember a True Computing Pioneer...


See More Blogs