Re: Static Variables



Richard Heathfield wrote, On 20/02/07 09:57:
akhil.misra@xxxxxxxxx said:

where does a static variable inside a function get stored??

In random access memory, or "RAM" (or, if it's const, it might get put into read-only memory, or "ROM").

You mean my implementation using pairs of goldfish to store each bit (one a second the goldfish that knows the current value tells the other goldfish to "refresh" the bit and stop it from being forgotten) is not a conforming implementation? Damn.

<snip>

Local variable goes on Stack...

Assuming you mean automatic objects, they're stored in RAM, unless they're const, in which case they might go into ROM. They might be incorporated into some kind of a stack structure, or they might not. It's up to the implementation.

Or they might go in to non-random-access registers, or might be completely eliminated by the optimiser.

but how does static variable takes place in memory??

It goes into RAM (or, if it's const, it might go into ROM). The precise details depend on the implementation.

I do, of course, agree with the general thrust of your post :-)
--
Flash Gordon
.



Relevant Pages

  • Re: Larkin, Power BASIC cannot be THAT good:
    ... part of "const int" and therefore actually allocated memory and stored the value, fetching it again every time it was needed. ... A lot of the older compilers did that copy to ram even when they purported to generate code for embedded systems. ... And you can be sure that no attempt by the CPU to trash a ROM value will ever succeed. ... You could tell it was in big trouble if the user register bank was in ROM where incrementing the program counter doesn't work any more. ...
    (sci.electronics.design)
  • Re: Static Variables
    ... In random access memory, or "RAM" (or, if it's const, it might get put ... in which case they might go into ROM. ...
    (comp.lang.c)