 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
sks_cpp Guest
|
Posted: Wed Jul 30, 2003 4:23 am Post subject: static allocation |
|
|
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?
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Jul 30, 2003 4:33 am Post subject: Re: static allocation |
|
|
"sks_cpp" <sksjava (AT) hotmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Josephine Schafer Guest
|
Posted: Wed Jul 30, 2003 4:49 am Post subject: Re: static allocation |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
"sks_cpp" <sksjava (AT) hotmail (DOT) com> wrote in message
news:hLHVa.105778$R92.24264 (AT) news2 (DOT) central.cox.net...
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.
|
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.
--
With best wishes,
J.Schafer
|
|
| Back to top |
|
 |
Josephine Schafer Guest
|
Posted: Wed Jul 30, 2003 5:00 am Post subject: Re: static allocation |
|
|
| Quote: |
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.
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Jul 30, 2003 5:04 am Post subject: Re: static allocation |
|
|
"Josephine Schafer" <jsf (AT) usa (DOT) net> wrote
| Quote: |
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:bg7hqi$lhg80$1 (AT) ID-196037 (DOT) news.uni-berlin.de...
"sks_cpp" <sksjava (AT) hotmail (DOT) com> wrote in message
news:hLHVa.105778$R92.24264 (AT) news2 (DOT) central.cox.net...
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.
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
|
|
| Back to top |
|
 |
Josephine Schafer Guest
|
Posted: Wed Jul 30, 2003 8:52 am Post subject: Re: static allocation |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
"Josephine Schafer" <jsf (AT) usa (DOT) net> wrote in message
news:bg7ip6$l0hte$1 (AT) ID-192448 (DOT) news.uni-berlin.de...
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:bg7hqi$lhg80$1 (AT) ID-196037 (DOT) news.uni-berlin.de...
"sks_cpp" <sksjava (AT) hotmail (DOT) com> wrote in message
news:hLHVa.105778$R92.24264 (AT) news2 (DOT) central.cox.net...
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.
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.
|
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 .
|
|
| Back to top |
|
 |
Greg Comeau Guest
|
Posted: Wed Jul 30, 2003 12:34 pm Post subject: Re: static allocation |
|
|
In article <bg7je1$loq11$1 (AT) ID-192448 (DOT) news.uni-berlin.de>,
Josephine Schafer <jsf (AT) usa (DOT) net> wrote:
| Quote: | 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.
|
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).
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
|
| Back to top |
|
 |
Greg Comeau Guest
|
Posted: Wed Jul 30, 2003 12:42 pm Post subject: Re: static allocation |
|
|
In article <bg7hqi$lhg80$1 (AT) ID-196037 (DOT) news.uni-berlin.de>,
John Harrison <john_andronicus (AT) hotmail (DOT) com> wrote:
| Quote: | "sks_cpp" <sksjava (AT) hotmail (DOT) com> wrote in message
news:hLHVa.105778$R92.24264 (AT) news2 (DOT) central.cox.net...
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.
|
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.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
|
| Back to top |
|
 |
Greg Comeau Guest
|
Posted: Wed Jul 30, 2003 12:47 pm Post subject: Re: static allocation |
|
|
In article <bg7ip6$l0hte$1 (AT) ID-192448 (DOT) news.uni-berlin.de>,
Josephine Schafer <jsf (AT) usa (DOT) net> wrote:
| Quote: | "John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:bg7hqi$lhg80$1 (AT) ID-196037 (DOT) news.uni-berlin.de...
"sks_cpp" <sksjava (AT) hotmail (DOT) com> wrote in message
news:hLHVa.105778$R92.24264 (AT) news2 (DOT) central.cox.net...
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.
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".
| Quote: | If the function never gets called the object is never created.
|
Perhaps.
| Quote: | 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.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
|
| Back to top |
|
 |
Greg Comeau Guest
|
Posted: Wed Jul 30, 2003 1:02 pm Post subject: Re: static allocation |
|
|
In article <bg8110$ktb28$1 (AT) ID-192448 (DOT) news.uni-berlin.de>,
Josephine Schafer <jsf (AT) usa (DOT) net> wrote:
| Quote: | 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)
| Quote: | 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.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
|
| Back to top |
|
 |
cody Guest
|
Posted: Sun Sep 28, 2003 10:08 pm Post subject: Re: static allocation |
|
|
| Quote: | 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.
--
cody
[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sun Sep 28, 2003 11:19 pm Post subject: Re: static allocation |
|
|
cody wrote:
| Quote: | 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.
|
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
| Quote: | 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
| Quote: | 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.
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|