static allocation

S

sks_cpp

When do static members allocate memory? (at load time)

How about static variables within a method?

Where is the memory for static variables - is it allocated on the heap or
does it have a region of its own?
 
J

John Harrison

sks_cpp said:
When do static members allocate memory? (at load time)

How about static variables within a method?

Where is the memory for static variables - is it allocated on the heap or
does it have a region of its own?

All static variables, of all types, have their memory allocated at load
time.

john
 
J

Josephine Schafer

John Harrison said:
All static variables, of all types, have their memory allocated at load
time.

Static variables in a function are constructed and destroyed only on the
first invocation of the function.
If the function never gets called the object is never created. Ofcourse this
means an extra overhead to check if the
object needs to be created each time the function is invoked.
But don't pay for things you don't use.
 
J

Josephine Schafer

Where is the memory for static variables - is it allocated on the heap or
does it have a region of its own?
Static variables are not allocated on the heap.
Yes they have a region of their own.
 
J

John Harrison

Josephine Schafer said:
Static variables in a function are constructed and destroyed only on the
first invocation of the function.
If the function never gets called the object is never created. Ofcourse this
means an extra overhead to check if the
object needs to be created each time the function is invoked.
But don't pay for things you don't use.

The question wsn't about construction, it was about memory allocation.

john
 
J

Josephine Schafer

John Harrison said:
The question wsn't about construction, it was about memory allocation.

See static objects in a class are always constructed and destructed, no
matter whether they are ever used or not.
The same is not true for static variables in a function. So shouldn't the
implementation defer the memory allocation also
for such variables till their first invocation just like construction?
Else we have paid a little even without having used it :).
 
G

Greg Comeau

Static variables are not allocated on the heap.
Yes they have a region of their own.

Just a side comment here that the standard never discusses
things such as the stack, etc. So these are normally
implementation things, though most implementation tend to
use the same stragegy (optimization issues aside).
 
G

Greg Comeau

All static variables, of all types, have their memory allocated at load
time.

Well, many compilers do it that way for namespace'd statics at least.
Of course too, this does not necessarily apply to static members
and probably some other situations too.
 
G

Greg Comeau

Static variables in a function are constructed and destroyed only on the
first invocation of the function.

No, this too is just one of many possibilities.
For instance, it may be done elsewhere. Or, it may be repeated
if "EH failed".
If the function never gets called the object is never created.
Perhaps.

Ofcourse this
means an extra overhead to check if the
object needs to be created each time the function is invoked.
But don't pay for things you don't use.

In a traditional implementation.
 
G

Greg Comeau

See static objects in a class are always constructed and destructed, no
matter whether they are ever used or not.

But that's different. (actually, technically, that can be
optimized away too in many cases)
The same is not true for static variables in a function. So shouldn't the
implementation defer the memory allocation also
for such variables till their first invocation just like construction?

The implementation has that freedom.
 
C

cody

When do static members allocate memory? (at load time)
How about static variables within a method?

Where is the memory for static variables - is it allocated on the heap or
does it have a region of its own?


statics are like global variables but reside in a class. they are already
contructed when the app starts and destroyed when the app ends so allocation
and deallocation of them is nothing you have to worry about. they reside in
their own memory location, where exactly this is is depends on the specific
implementation but that does not matter for programming.
 
G

Gianni Mariani

cody said:
statics are like global variables but reside in a class.

I think the OP asked for a static method variable.

static method variables are initialized the first time the statment
where is is defined is called and are destroyed upon program termination.

The code below shows how it all works.


#include <iostream>

struct foo
{
const char * str;

foo( const char * i_str )
: str( i_str )
{
std::cout << "making foo" << str << "\n";
}

~foo()
{
std::cout << "deleting foo" << str << "\n";
}

void useme()
{
std::cout << "using foo" << str << "\n";
}
};


struct zoo
{
void method( int v = 0 )
{
static foo static_method_variable( "First" );
static_method_variable.useme();

if ( v == 0 )
{
static foo static_method_variable( "Second" );
static_method_variable.useme();
}
else if ( v == 1 )
{
static foo static_method_variable( "Third" );
static_method_variable.useme();
}
else if ( v == 2 )
{
static foo static_method_variable( "Fourth" );
static_method_variable.useme();
}

}
};


int main()
{
std::cout << "in main \n";

zoo zoo_obj1;

zoo zoo_obj2;

zoo_obj1.method( 1 );

zoo_obj2.method( 1 );

zoo_obj1.method( 0 );

zoo_obj2.method( 0 );

std::cout << "leaving main \n";
}

---- produces ----

in main
making fooFirst
using fooFirst
making fooThird
using fooThird
using fooFirst
using fooThird
using fooFirst
making fooSecond
using fooSecond
using fooFirst
using fooSecond
leaving main
deleting fooSecond
deleting fooThird
deleting fooFirst



they are already
contructed when the app starts

.... they are initialized when the code containing the statment is first
passed.

and destroyed when the app ends so allocation
 

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

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top