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? 11 8922
"sks_cpp" <sk*****@hotmail.com> wrote in message
news:hL********************@news2.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.
john
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de... "sks_cpp" <sk*****@hotmail.com> wrote in message news:hL********************@news2.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 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.
"Josephine Schafer" <js*@usa.net> wrote in message
news:bg************@ID-192448.news.uni-berlin.de... "John Harrison" <jo*************@hotmail.com> wrote in message news:bg************@ID-196037.news.uni-berlin.de... "sks_cpp" <sk*****@hotmail.com> wrote in message news:hL********************@news2.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
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de... "Josephine Schafer" <js*@usa.net> wrote in message news:bg************@ID-192448.news.uni-berlin.de... "John Harrison" <jo*************@hotmail.com> wrote in message news:bg************@ID-196037.news.uni-berlin.de... "sks_cpp" <sk*****@hotmail.com> wrote in message news:hL********************@news2.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 :-).
In article <bg************@ID-192448.news.uni-berlin.de>,
Josephine Schafer <js*@usa.net> wrote: 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?
In article <bg************@ID-196037.news.uni-berlin.de>,
John Harrison <jo*************@hotmail.com> wrote: "sks_cpp" <sk*****@hotmail.com> wrote in message news:hL********************@news2.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?
In article <bg************@ID-192448.news.uni-berlin.de>,
Josephine Schafer <js*@usa.net> wrote: "John Harrison" <jo*************@hotmail.com> wrote in message news:bg************@ID-196037.news.uni-berlin.de... "sks_cpp" <sk*****@hotmail.com> wrote in message news:hL********************@news2.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".
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.
--
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?
In article <bg************@ID-192448.news.uni-berlin.de>,
Josephine Schafer <js*@usa.net> wrote: 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.
--
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?
> 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
cody wrote: 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 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 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J Anderson |
last post by:
Greetings,
I was going through the quake2 source code (as you do) when I noticed
arrays are statically allocated on the stack instead of being allocated
dynamically. I’ve seen this before and...
|
by: Steven T. Hatton |
last post by:
In the following code, at what point is S::c fully defined?
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class C {
int _v;
|
by: Jonan |
last post by:
Hello,
For several reasons I want to replace the built-in memory management with
some custom built. The mem management itlsef is not subject to my question -
it's ok to the point that I have...
|
by: Jack |
last post by:
Thanks.
|
by: toton |
last post by:
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
|
by: Ken |
last post by:
In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array...
|
by: Jo |
last post by:
Hi,
How can i differentiate between static and dynamic allocated objects?
For example:
void SomeFunction1() {
CObject *objectp = new CObject;
CObject object;
|
by: Marvin Barley |
last post by:
I have a class that throws exceptions in new initializer, and a static
array of objects of this type.
When something is wrong in initialization, CGI program crashes
miserably. Debugging shows...
|
by: swornavidhya.mahadevan |
last post by:
Which allocation (Static / Dynamic) is suitable for the situation when
we are trying to allocate for a overloaded memory when the memory is
full and no space to allocate.
What will happen if both...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |