Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

STATIC variable

38 views
Skip to first unread message

bj...@my-deja.com

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to

In a class, I define a static variable. Where is it stored? In stack or
heap? It seems unreasonable in either of them?

-chen


Sent via Deja.com http://www.deja.com/
Before you buy.

Victor Bazarov

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
<bj...@my-deja.com> wrote...

>
>
> In a class, I define a static variable. Where is it stored? In stack or
> heap? It seems unreasonable in either of them?

I am not sure what to tell you. C++ doesn't specify WHERE the variables'
memory is allocated, it's up to implementation. C++ specifies only the
lifetime of different type of objects. A static class data member has
storage duration for as long as the program is in memory. Just like any
other static object. On a particular OS, like MS-DOS it's stored in a so
called DATA SEGMENT.

Victor
--
Please remove capital A's from my address when replying by mail


Adam McKee

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
bj...@my-deja.com wrote:


> In a class, I define a static variable. Where is it stored? In stack or
> heap? It seems unreasonable in either of them?

> -chen

Right. It's stored in the same place as global data. Basically, it's a
region of memory whose size is determined at compile-time.

-Adam

bj...@my-deja.com

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to

Thanks for the answer. But I am confused now. In C++, do the stack and
heap still exist? How does C++ deal with activation record? For example,
there are two class A and B.

class A{
static int m;
a { } // a is member function of A

}
class B{

b { // b is a member function of B
c();
}

c { // c is another member function of B
A aObject;

aObject.a();
}

}

In this example, if B.b() is called. How the activation record is
organized? And how static data member A.m is maintained?

Thanks for your ideas!

-chen

>
> I am not sure what to tell you. C++ doesn't specify WHERE the
variables'
> memory is allocated, it's up to implementation. C++ specifies only
the
> lifetime of different type of objects. A static class data member has
> storage duration for as long as the program is in memory. Just like
any
> other static object. On a particular OS, like MS-DOS it's stored in a
so
> called DATA SEGMENT.
>
> Victor
> --
> Please remove capital A's from my address when replying by mail
>
>

R124c4u2

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
chen writes:

>In a class, I define a static variable. Where is it stored? In stack or
>heap? It seems unreasonable in either of them?

This goes back a while; but I'll take a shot at it. Historically, assembler
language programs have had three kinds of 'things': instruction space, blank
space (such as for large I/O data transfers) and a place for data with initial
values specified. That area was, IIRC, often called BSS. Compilers are
written at least partly by assembly language programmers. They may write large
parts in something else, but they, at least the lead programmers, are at least
*capable* of writing assembly language. So they tend to emulate assembly
language ways and habits.

Since static variables are permitted to have initial values other than 0, and
last for the duration of the program, I would expect them to be stored in that
BSS segment. (Not like an 8086 segment, a different concept. Perhaps thinking
of a logical segment would be helpful)

The stack and heap can be created in the unitialized area and grow towards each
other.

None of this is specified by the language which has already been pointed out
and will probably be pointed out several more times now that I have posted
this. :-)

Paul Lutus

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
> In C++, do the stack and heap still exist?

Formally, heap and stack are implementation details. The language doesn't
depend on their existence.

--

Paul Lutus
www.arachnoid.com


<bj...@my-deja.com> wrote in message news:8abiet$te3$1...@nnrp1.deja.com...

Alexei Betin

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
bj...@my-deja.com wrote in message <8abe97$pvf$1...@nnrp1.deja.com>...

>
>
>In a class, I define a static variable. Where is it stored? In stack or
>heap?

Neither. It's in another area of program memory that I would call data
segment. These are implementation details though, C++ only defines
characteristics like storage and linkage for an identifier, not where it is
stored

Sidharth

unread,
Mar 11, 2000, 3:00:00 AM3/11/00
to

bj...@my-deja.com wrote:

> In a class, I define a static variable. Where is it stored? In stack or

> heap? It seems unreasonable in either of them?
>

> -chen


>
> Sent via Deja.com http://www.deja.com/
> Before you buy.

i dont think the standard says where it should be stored.
instead, it tells what the life time of such a variable should be.

sidharth


Yura Koblents-Mishke

unread,
Mar 13, 2000, 3:00:00 AM3/13/00
to

In the usual implementations the data would be stored
not in stack nor heap, but in the static data area.
I.e. together with the static data of functions (both
methods and external to all classes), the static data
in source files outside all function and the extern
data.

0 new messages