Re: static, global variable memory allocation
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 06 Feb 2007 16:41:42 +0000
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
.
- Follow-Ups:
- Re: static, global variable memory allocation
- From: Nishu
- Re: static, global variable memory allocation
- From: Richard Tobin
- Re: static, global variable memory allocation
- From: fdmfdmfdm@xxxxxxxxx
- Re: static, global variable memory allocation
- References:
- static, global variable memory allocation
- From: fdmfdmfdm@xxxxxxxxx
- static, global variable memory allocation
- Prev by Date: Re: IS this a proper way of freeing memory with free()
- Next by Date: Re: C net oriented
- Previous by thread: Re: static, global variable memory allocation
- Next by thread: Re: static, global variable memory allocation
- Index(es):
Relevant Pages
|
Loading