Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > about memory model

Reply
Thread Tools

about memory model

 
 
kumar
Guest
Posts: n/a
 
      05-26-2008
hi
i want to know where & how the C variables gets stored
i mean like volatile , pointer and string variables gets stored ,
whether it is on stack or some other places
if is there any clear document , plz suggest the link
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-26-2008
kumar wrote:
> hi
> i want to know where & how the C variables gets stored
> i mean like volatile , pointer and string variables gets stored ,
> whether it is on stack or some other places
> if is there any clear document , plz suggest the link


The word is "please".

The best place to find the answer is in your compiler or platform
documentation. The details of where variables are stored are
implementation specific.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Szabolcs Borsanyi
Guest
Posts: n/a
 
      05-26-2008
On Mon, May 26, 2008 at 12:29:21AM -0700, kumar wrote:
> hi
> i want to know where & how the C variables gets stored
> i mean like volatile , pointer and string variables gets stored ,
> whether it is on stack or some other places


The nice thing with C is that you do not have to think about this
when you program in standard C. Whenever you think about these details
you loose the portability of your program, since the storage of the
variables is left for the discretion of the implementors.

The concept of stack is not part of the language, but variables with
automatic storage (local variables without the static keyword) are
stored by most systems on some sort of stack. The global variables
and those declared with the static keyword are stored in a designated
section of memory, and they are initialised before main() is called.
The memory for dynamical variables are asked from the operating system,
whenever you call malloc(). There is an other storage class specifier,
register, which suggests that the variable should be stored in a cpu register,
but compilers are free to ignore that keyword (but all have to document
the effect of the register keyword.)
The qualifiers (e.g. const, volatile) do not affect the storage, but they
do have an impact on the access to those variables.

It is difficult to tell more without knowing your system and your intentions.
It is not polite to ask if you really need the information you have asked for,
but it is difficult to resist.

Szabolcs

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-26-2008
Szabolcs Borsanyi wrote, On 26/05/08 10:18:
> On Mon, May 26, 2008 at 12:29:21AM -0700, kumar wrote:
>> hi
>> i want to know where & how the C variables gets stored
>> i mean like volatile , pointer and string variables gets stored ,
>> whether it is on stack or some other places

>
> The nice thing with C is that you do not have to think about this
> when you program in standard C. Whenever you think about these details
> you loose the portability of your program, since the storage of the
> variables is left for the discretion of the implementors.
>
> The concept of stack is not part of the language, but variables with
> automatic storage (local variables without the static keyword) are
> stored by most systems on some sort of stack. The global variables


On a lot of systems at least some of them are stored in registers and
never written to if the compiler can avoid it.

<snip>

> The qualifiers (e.g. const, volatile) do not affect the storage, but they
> do have an impact on the access to those variables.


Incorrect. On a lot of systems const will cause "variables" to be stored
in some form of read-only memory, either actual ROM or a page that the
OS will mark as read only when it loads the program.

> It is difficult to tell more without knowing your system and your intentions.
> It is not polite to ask if you really need the information you have asked for,
> but it is difficult to resist.


Any questions about the specifics of how/where an implementation stores
variables belong on a group dedicated to that implementation rather than
here.
--
Flash Gordon
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-26-2008
Malcolm McLean wrote, On 26/05/08 12:30:

<snip>

Ignoring the lack of requirement for a stack or heap the following is
just plain WRONG

> volatile variables can be modified outside the C program. So all these
> optimisations have to be turned off. A volatile variable will always be
> kept in the same place in memory so the outside routine - usually an
> interrupt - can find it to modify it.


If it is a local non-static volatile variable (i.e. an automatic
volatile object) then there is absolutely NO requirement that it is kept
in the same place in memory, and in general it will be created where
ever happens to be convenient when the scope is entered. If the scope is
a function that is called recursively it is almost impossible for the
variable to be always created at the same location!
--
Flash Gordon
 
Reply With Quote
 
kumar
Guest
Posts: n/a
 
      05-26-2008
On May 26, 4:30 pm, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> "kumar" <raman....@gmail.com> wrote in message news:
> > hi
> > i want to know where & how the C variables gets stored
> > i mean like volatile , pointer and string variables gets stored ,
> > whether it is on stack or some other places
> > if is there any clear document , plz suggest the link

>
> In C you have a stack and a heap. When you call a function, local variables
> are pushed on the stack. The return address might also be pushed on the
> stack, or there might be a special stack for it.
> When you call malloc() you take a chunk for memory from the heap. This
> doesn't get reused automatically, and persists until you explicitly call
> free().
>
> pointers are just ordinary variables. There's no special storage space for
> them.
>
> Global variables go into a special area of memory created at program
> startup. They persist for the entire life of the program. Local variables
> with "static" are really global variables in disguise. They also persist the
> entire life of the program, and are stored in the same place as the globals.
>
> However be aware that optimisers can produce any code whatsoever, as long as
> it has the same effect as the code you would "naturally" expect from a
> translation of C into assembly. So variables might be kept in registers, or
> optimised away entirely, or funny things might be done to make cache usage
> more efficient.
>
> volatile variables can be modified outside the C program. So all these
> optimisations have to be turned off. A volatile variable will always be kept
> in the same place in memory so the outside routine - usually an interrupt -
> can find it to modify it.
>
> --
> Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1m



i am practicing the system programming , that's why i am concerned
about variables storage
and now i got about it
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      05-26-2008
On Mon, 26 May 2008 07:28:06 -0700 (PDT), kumar <>
wrote:

>i am practicing the system programming , that's why i am concerned
>about variables storage
>and now i got about it


But there is no requirement for Compiler 1 to use the same approach to
storing variables as Compiler 2. The same is true for different
versions of Compiler 1. The answer to your original question remains
implementation specific.


Remove del for email
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-26-2008
Flash Gordon <> writes:
> Szabolcs Borsanyi wrote, On 26/05/08 10:18:

[...]
>> The qualifiers (e.g. const, volatile) do not affect the storage, but they
>> do have an impact on the access to those variables.

>
> Incorrect. On a lot of systems const will cause "variables" to be
> stored in some form of read-only memory, either actual ROM or a page
> that the OS will mark as read only when it loads the program.

[...]

That can happen only if the initial value can be determined at
compilation time.

For example, this is a valid declaration (if it appears within a
function, and assuming the required headers have been #included):

const time_t now = time(NULL);

For that matter, if an object's initial value can be determined at
compilation time and the compiler can determine that it's never
modified, the compiler is free to store it in ROM even if it's not
declared const (though in that case it *should* have been declared
const).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-26-2008
"Malcolm McLean" <> writes:
[...]
> In C you have a stack and a heap.

[...]

Wrong, and I'm sure you know better.

In most C *implementations* you have a stack and a heap. The C
language itself (i.e., the standard) doesn't refer to either. It
states how certain objects are required to behave; the structures
known as a "stack" and as a "heap" are usually, but by no means
always, the most convenient way to meet those requirements.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-26-2008
Keith Thompson wrote:
>
> For that matter, if an object's initial value can be determined at
> compilation time and the compiler can determine that it's never
> modified, the compiler is free to store it in ROM even if it's not
> declared const (though in that case it *should* have been declared
> const).
>

String literals being one example.

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Java Model to Java Model without XML erinbot@gmail.com Java 1 10-06-2006 09:00 PM
Java 1.4 memory model and hyper threading usenet@kikobu.com Java 2 10-03-2006 02:01 AM
Some quick c++ memory-model concerns... Chris Thomasson C++ 0 09-03-2006 02:22 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Plz send me some good online link for memory model in C PKJ C++ 1 10-22-2004 01:20 AM



Advertisments