PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > about memory model
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
about memory model

Réponse
 
LinkBack Outils de la discussion
Vieux 26/05/2008, 08h29   #1
kumar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut about memory model

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
  Réponse avec citation
Vieux 26/05/2008, 08h43   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

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.
  Réponse avec citation
Vieux 26/05/2008, 11h23   #3
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

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
  Réponse avec citation
Vieux 26/05/2008, 12h30   #4
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model


"kumar" <raman.emb@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/~bgy1mm

  Réponse avec citation
Vieux 26/05/2008, 13h09   #5
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

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
  Réponse avec citation
Vieux 26/05/2008, 15h28   #6
kumar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

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
  Réponse avec citation
Vieux 26/05/2008, 18h26   #7
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

On Mon, 26 May 2008 07:28:06 -0700 (PDT), kumar <raman.emb@gmail.com>
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
  Réponse avec citation
Vieux 26/05/2008, 19h21   #8
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

Flash Gordon <spam@flash-gordon.me.uk> 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-u@mib.org <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"
  Réponse avec citation
Vieux 26/05/2008, 19h24   #9
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

"Malcolm McLean" <regniztar@btinternet.com> 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-u@mib.org <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"
  Réponse avec citation
Vieux 26/05/2008, 20h06   #10
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

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.
  Réponse avec citation
Vieux 26/05/2008, 20h45   #11
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

On Mon, 26 May 2008 12:30:24 +0100, "Malcolm McLean"
<regniztar@btinternet.com> wrote in comp.lang.c:

>
> "kumar" <raman.emb@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.


There are, of course, several platforms where this is just plain
completely incorrect.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
Vieux 26/05/2008, 21h23   #12
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model


"Jack Klein" <jackklein@spamcop.net> wrote in message
> There are, of course, several platforms where this is just plain
> completely incorrect.
>

The OP wants an explanation of how C's memory is laid out, not a formal
definition of the memory model.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

  Réponse avec citation
Vieux 26/05/2008, 21h37   #13
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

"Malcolm McLean" <regniztar@btinternet.com> writes:
> "Jack Klein" <jackklein@spamcop.net> wrote in message
>> There are, of course, several platforms where this is just plain
>> completely incorrect.
>>

> The OP wants an explanation of how C's memory is laid out, not a
> formal definition of the memory model.


And you gave him an explanation of how *some* C implementations do it.

It's important to understand the distinction between the language and
an implementation of the language. Please don't blur that
distinction.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"
  Réponse avec citation
Vieux 26/05/2008, 22h14   #14
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

Szabolcs Borsanyi wrote, On 26/05/08 20:34:
> On Mon, May 26, 2008 at 11:23:02AM +0100, Flash Gordon wrote:
>> Szabolcs Borsanyi wrote, On 26/05/08 10:18:
>>> 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.

>
> Thanks, you are right, indeed. Optimising compilers use registers extensively,
> unless the address of the variable is asked for. (Or could they use registers
> even then?...)


If it is not declared volatile the compiler may be able to keep it in a
register for long stretches of code, an easy example being any stretch
of code where no pointers are used.

>>> 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.

>
> Thank you for correcting me.
> I wonder if the volatile qualifier could have an impact on the way of storage...


That's easy. If the variable is declared volatile const rather than just
const is may well *not* me placed in read-only memory :-)
--
Flash Gordon
  Réponse avec citation
Vieux 26/05/2008, 22h18   #15
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

Keith Thompson wrote, On 26/05/08 19:21:
> Flash Gordon <spam@flash-gordon.me.uk> 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.


True.

> 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).


True, but rather harder for the implementation to prove where there are
multiple compilation units. I know for definite of one compiler for
embedded systems which puts all const qualified variable with static
storage duration in a seperate memory section specifically so that you
can allocate it to ROM but it does not put non-const qualified variables
in that section even if they are never modified. I suspect (without
proof) that this is probably quite common.
--
Flash Gordon
  Réponse avec citation
Vieux 26/05/2008, 22h35   #16
Chris Torek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about memory model

>On Mon, May 26, 2008 at 11:23:02AM +0100, Flash Gordon wrote:
>>On a lot of systems at least some [local variables] are stored
>>in registers and never written to if the compiler can avoid it.


In article <20080526193405.GA15452@florence.pact.cpes.susx.ac .uk>
Szabolcs Borsanyi <s.borsanyi@sussex.ac.uk> wrote:
>Thanks, you are right, indeed. Optimising compilers use registers
>extensively, unless the address of the variable is asked for. (Or
>could they use registers even then?...)


They could, although exactly how depends on both the compiler and
the target architecture. Consider, e.g., the following C code
fragment:

for (i = 0; i < n; i++)
do_something(i);
maybe_alter(&i);
if (i != n)
do_something_else();
for (i = 0; i < n; i++)
do_third_thing(i);

Here, "i" is a likely candidate for "register-ization" to make
the two loops run faster. In between the two loops, however, we
pass &i to a function that might change it.

A reasonably clever compiler could easily rewrite this as:

for (i1 = 0; i1 < n; i1++)
do_something(i1);
i2 = i1;
maybe_alter(&i2);
if (i2 != n)
do_something_else();
for (i3 = 0; i3 < n; i3++)
do_third_thing(i3);
i2 = i3; /* if needed -- actually "i4" (if i is used below) */

It is now clear that i1 and i3 can be stored in a machine register
(the same machine register, in fact) even if i2 must live in RAM.

If the CPU has the ability to take the address of a register --
this is rare these days but was not uncommon once, and could perhaps
become popular again someday -- one need not even rewrite the
variable names to make this optimization possible.

>I wonder if the volatile qualifier could have an impact on the
>way of storage...


Yes, it often does. (In fact, cases where it fails to prevent
"registerization" tend to be compiler bugs. These bugs do happen
though.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26242 seconds with 24 queries