Re: static, global variable memory allocation



fdmfdmfdm@xxxxxxxxx wrote, On 06/02/07 16:00:
This is an interview question and I gave out my answer here, could you
please check for me?

Q. What are the memory allocation for static variable in a function,
an automatic variable and global variable?

My answer: static variable in function and global variable are
allocated in head, and automatic variable is allocated in stack.

Right?

Firstly the C standard does not define where the variable are allocated only how long they last and where their names are visible.

Secondly that could be a bit problematic on a ship where the head is the place you go to relieve yourself. You would just have to keep your legs crossed if someone is running a program with logs of globals and statics filling up the head.

If, on the other hand, you meant heap, then it would be wrong for a lot of common implementations, although it could certainly be implemented that way. Equally it could be implemented by allocating enough space on the stack (if there is one) at program startup for all the static and global data, and this might make sense on some systems. Then there is static const and "global" const data that on some systems will be programmed in to ROM and on others in to RAM marked as read only in a separate place from other static/global data. Also the machines without a stack will obviously not allocate automatic variables on the stack, although other implementations tend to.

In other words it depends entirely on the implementation but you are wrong for all the implementations I know of.
--
Flash Gordon
.



Relevant Pages

  • Re: Developing/compiling software
    ... is when people get obsessive about globals and think that it is somehow ... on smaller PICs, doesn't use global statics. ... extern unsigned int GetCurrentPID(void); ... extern unsigned int currpid; ...
    (comp.arch.embedded)
  • Re: Are static array pointers thread safe?
    ... 'static' that it is NOT allocated on the stack! ... how allocation works; I've even had one correspondent assure me that he would not use ... 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: VC++2008: STL access violation depending on runtime library being used -- bug or undefin
    ... So declaring a global object containing references to other globals is ... The trick of defining static objects at function scope (and returning ... construct examples of this random behaviour with two "function statics", ...
    (microsoft.public.vc.language)
  • Re: Halt execution procedure
    ... > deallocated on exit from a subprogram. ... > Fortran historically always did static allocation. ... It just seems like having STATIC locals could too easily waste ... Globals are variables that are not local, ...
    (comp.lang.fortran)
  • Re: Stack around variable rgval ist corrupted
    ... you make it global is because for globals the appropriate checks wouldn't ... > well I have a problem I guess with the memory allocation thing. ... > the subfunctions returns the error code the debugger says that the ...
    (microsoft.public.win32.programmer.messaging)

Loading