Re: storage locations for different data types

From: Allan W (allan_w_at_my-dejanews.com)
Date: 01/13/05


Date: 13 Jan 2005 17:20:00 -0500


> smith4894@excite.com wrote:
> > 1)Where are global non-const objects created?

Maciej Sobczak wrote:
> In some other place, which is a "static storage".
> Typically, the memory for all objects with static storage duration
> (collectively) is assigned to the process when the program is loaded
> into memory. Alternatively, it may be even part of the program (of
the
> file on disk) itself, but any other strategy is allowed, as long as
the
> lifetimes of the objects obey the rules. It all depends on
implementation.
>
> > 2)Where are global const objects created?
>
> Same as global non-const. This is static storage.
> The const qualifier affects only what you can do with them, not where
> they are.

I realize that you're answering a beginner's question, so you don't
want to get too technical. But I think it's important to point out
that const statics CAN be treated differently from non-const statics.
The definitive example is that embedded systems, where const statics
can be put into read-only memory, along with the program code. But it
isn't just embedded systems. A system with protected memory could
put a const object (even a const auto object) into a section of
memory, and then write-lock that section of memory until it's time
to run the destructor.

That's the main reason for the limitations on const_cast: If you cast
a pointer-to-const into a pointer-to-non-const, and the object truely
was a const object, you get undefined behavior.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated. First time posters: Do this! ]



Relevant Pages

  • Re: Are static array pointers thread safe?
    ... array. ... a separate area of memory dedicated to them. ... globals, static data members of classes, and local statics. ... observe all sorts of strange things in the absence of synchronization, ...
    (microsoft.public.vc.mfc)
  • Re: Use of static and extern
    ... >will reside in memory until the program terminates? ... forms of global object use memory allocated at program start-up. ... The reason that function scope statics provide functions with state ...
    (alt.comp.lang.learn.c-cpp)
  • Re: returning address of stack variable
    ... Hence some memory should be used to store this string. ... static storage (in some section of memory similar to other statics, ... initiailizer to a variable with its own declared storage. ...
    (comp.lang.c)
  • Re: Static Variables
    ... but how does static variable takes place in memory?? ... to place them on a stack, (except if it's declared within the main ... In most implementation statics and other file scope objects are ...
    (comp.lang.c)
  • Re: copymemory basic question
    ... a pre-amble usually allocates all the stack ... dynamic memory on exit, is very significant. ... >> Dim locals? ... One could think that Statics are faster as they don't ...
    (microsoft.public.vb.winapi)

Loading