By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

static variable

P: n/a
When I define a static variable, where is the memory allocated for the
static variable?
Thanks.

Jack

Jul 10 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ju******@gmail.com wrote:
When I define a static variable, where is the memory allocated for the
static variable?
Somewhere in an implementation-defined location.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '06 #2

P: n/a
ju******@gmail.com wrote:
>When I define a static variable, where is the memory allocated for the
static variable?
This is one of the areas were there is no difference between C and
C++. The answers you got when you asked the same question in
comp.lang.c are still valid.
Jul 10 '06 #3

P: n/a

Roberto Waltman wrote:
ju******@gmail.com wrote:
When I define a static variable, where is the memory allocated for the
static variable?

This is one of the areas were there is no difference between C and
C++. The answers you got when you asked the same question in
comp.lang.c are still valid.
I think for a static variable defined out of any functions, i.e., it is
a global variable, it is located in the data segment of the program.
How about a static variable defined within a function? it is a local
variable. Is it located at the stack?

Thanks.

Jul 14 '06 #4

P: n/a
ju******@gmail.com wrote:
Roberto Waltman wrote:
>ju******@gmail.com wrote:
>>When I define a static variable, where is the memory allocated for
the static variable?

This is one of the areas were there is no difference between C and
C++. The answers you got when you asked the same question in
comp.lang.c are still valid.

I think for a static variable defined out of any functions, i.e., it
is a global variable, it is located in the data segment of the
program. How about a static variable defined within a function? it is
a local variable. Is it located at the stack?
There is no such thing in C++ as "data segment" or "stack" (except
std::stack, which is a standard container adaptor). There _is_ the
"free store" where dynamic objects are allocated. We know that static
or automatic objects are not allocated in the "free store", but that's
the extent of the *defined* part.

We also know that the storage for _all_ static objects is reserved
at the program start, function-local static objects included. The
storage is also known to be zero-initialised. *Where* that storage
is located is absolutely implementation- and platform-specific.

If you'd like to talk particulars of object storage implementation
("data segment", "stack", etc.), please do it in a relevant newsgroup,
like one for your OS or compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 14 '06 #5

P: n/a
ju******@gmail.com schrieb:
Roberto Waltman wrote:
>ju******@gmail.com wrote:
>>When I define a static variable, where is the memory allocated for the
static variable?
This is one of the areas were there is no difference between C and
C++. The answers you got when you asked the same question in
comp.lang.c are still valid.

I think for a static variable defined out of any functions, i.e., it is
a global variable, it is located in the data segment of the program.
How about a static variable defined within a function? it is a local
variable. Is it located at the stack?
There are no local or global variables in C or C++.

Variables habe a lifetime and a scope. The lifetime of static variables
is that of the programm.

The C++ standard doesn't talk about data segments or stacks, but the
implementation _can_ choose to put these variables (both "local" and
"global" statics) into the data segment, so that this lifetime is granted.

--
Thomas
Jul 15 '06 #6

P: n/a
Thomas J. Gritzan posted:

There are no local or global variables in C or C++.

When I hear "local variable", I think "variable defined within a function".
--

Frederick Gotham
Jul 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.