static storage duration and dynamic initialization

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

Consider the program x.cpp:

#include <cstdlib>
#include <string>

using namespace std;

class Test
{
static string str;
};

string Test::str;

int main()
{
return EXIT_SUCCESS;
}

Here Test::str has static storage duration as per the ISO/
IEC-14882:2003 document in section 3.7.1 paragraph 4.
Am I correct ?

Now as per section 3.6.2 paragraph 1 in this document,
"Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place. Zero-initialization
and intialization with a constant expression are collectively called
static intialization; all other initialization is dynamic
initialization."

In the above program, consider the initialization
string Test::str;

Since this involves the default ctor of 'string' class to be called,
it is not zero-initialization. Am I correct ? If I am correct, the
above initialization belongs to dynamic initialization category. Am I
correct ?

Kindly explain.

Thanks
V.Subramanian
 
V

Victor Bazarov

Consider the program x.cpp:

#include <cstdlib>
#include <string>

using namespace std;

class Test
{
static string str;
};

string Test::str;

int main()
{
return EXIT_SUCCESS;
}

Here Test::str has static storage duration as per the ISO/
IEC-14882:2003 document in section 3.7.1 paragraph 4.
Am I correct ?
Yes.


Now as per section 3.6.2 paragraph 1 in this document,
"Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place. Zero-initialization
and intialization with a constant expression are collectively called
static intialization; all other initialization is dynamic
initialization."

In the above program, consider the initialization
string Test::str;

Since this involves the default ctor of 'string' class to be called,
it is not zero-initialization. Am I correct ?

There are two phases of initialization. *First*, memory is allocated
(the object has to reside somewhere) and zero-initialized (because
that's what is done to static storage right after the program is loaded
to be run). *Second*, when the program starts all static objects
undergo their dynamic initialization (if applicable), and at some point,
before 'main' is called, the constructor for your object is called so it
can perform additional (dynamic) initialization of the object.
> If I am correct, the
above initialization belongs to dynamic initialization category. Am I
correct ?

Yes, it does. The object's storage (memory it occupies) is still going
to be zero-initialized before any dynamic initialization takes place.
That's what the paragraph is about.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top