When does Storage allocation for static data happens?

D

Divick

Hi I want to know when does the storate allocation happens for member
static data? For example I have following class declared in a header

typedef void (*fptr)(void);
class DllMethods
{
public:
static int data;
static fptr function;
};
int globalData;

//In the implementation I have commented out the static data
initialization
//fptr DllMethods::function = 0;
//int DllMethods::data = 0;

If in the implementation file, I do not do the static member
initialization, then does the compiler allocates storage for the static
data or not? If it allocates space for the static data then what does
the compiler initialize them to? Are they simply left uninitialized?

Thanks,
Divick
 
S

Sunil Varma

Divick said:
Hi I want to know when does the storate allocation happens for member
static data? For example I have following class declared in a header

typedef void (*fptr)(void);
class DllMethods
{
public:
static int data;
static fptr function;
};
int globalData;

//In the implementation I have commented out the static data
initialization
//fptr DllMethods::function = 0;
//int DllMethods::data = 0;

If in the implementation file, I do not do the static member
initialization, then does the compiler allocates storage for the static
data or not? If it allocates space for the static data then what does
the compiler initialize them to? Are they simply left uninitialized?

Thanks,
Divick

Static members in a class are global variables with class scope.
So you have to explicitly define the static variables outside the class
globally.
If you don't define them you will get a compilation error at the place
where you access those variables.
They are not allocated memory at all, if you do not define them outside
the class.

Regards
Sunil Varma
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top