Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > static, global variable memory allocation

Reply
Thread Tools

static, global variable memory allocation

 
 
fdmfdmfdm@gmail.com
Guest
Posts: n/a
 
      02-06-2007
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?

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      02-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> 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?
>


Maybe you mean "heap" not "head" ???

If that is the case, you are mostly right, it depends on the
implementation and OS running, etc.

A program has a fixed area with the initial data of the program.
This is not the heap in the traditional sense but just a RAM space
filled with tables, strings, double data, etc.

The heap is a variable area used to allocate space for data
dynamically during program execution. That data can be
local to a function, or be global. You are confusing the scope
of a variable, and the place where it is allocated, what is
not the same.

{
char *m = malloc(267;
// use of m
free(m);
}

m is allocated from the heap, but it is local to that block
since it is freed before the block is finished. The varible
"m", of pointer type, is local to that block, and is active
only when that block is active.

Mostly, local variables are allocated from the stack, but some
people here say there are machines without stack that implement
C. I do not know, I have never seen one of those. In any
case in most systems, the stack exists and it is used to
allocate local variables.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      02-06-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:

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


Many answers could possibly be right. This one is wrong.
--
Ben Pfaff
(E-Mail Removed)
http://benpfaff.org
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-06-2007
In article <(E-Mail Removed). com>,
(E-Mail Removed) <(E-Mail Removed)> wrote:

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


This is right for common implementations, but it doesn't have to work
like that. And auto variables are commonly stored in registers as
well as on the stack.

(I assume you mean "heap" rather than "head", though the idea of allocating
variables in the user's brain appeals.)

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-06-2007
jacob navia said:

<snip>

> {
> char *m = malloc(267;
> // use of m
> free(m);
> }
>
> m is allocated from the heap, but it is local to that block
> since it is freed before the block is finished.


No, that's wrong. In fact, m is an auto object, and it has function
scope. The memory allocated by malloc (if any) is taken from the free
store, for which the term "heap" may or may not be applicable on any
particular platform. Note also that m is not freed. What is freed is
the memory (if any) that was allocated by malloc.

> The varible
> "m", of pointer type, is local to that block, and is active
> only when that block is active.


Well, m only *exists* when that block is active.

> Mostly, local variables are allocated from the stack, but some
> people here say there are machines without stack that implement
> C.


Right.

> I do not know, I have never seen one of those. In any
> case in most systems, the stack exists and it is used to
> allocate local variables.


How do you know that "most systems" have a stack?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-06-2007
Richard Heathfield a écrit :
> jacob navia said:
>
> <snip>
>
>
>>{
>>char *m = malloc(267;
>>// use of m
>>free(m);
>>}
>>
>>m is allocated from the heap, but it is local to that block
>>since it is freed before the block is finished.

>
>
> No, that's wrong. In fact, m is an auto object, and it has function
> scope. The memory allocated by malloc (if any) is taken from the free
> store, for which the term "heap" may or may not be applicable on any
> particular platform. Note also that m is not freed. What is freed is
> the memory (if any) that was allocated by malloc.
>


Yes, I should have said:
"the memory area returned by malloc, and assigned to m"
In the next sentence I explained "m", and I should have
make it clear.

>>Mostly, local variables are allocated from the stack, but some
>>people here say there are machines without stack that implement
>>C.

>
>
> Right.
>
>
>>I do not know, I have never seen one of those. In any
>>case in most systems, the stack exists and it is used to
>>allocate local variables.

>
>
> How do you know that "most systems" have a stack?
>


Because there is a LONG list of very popular microprocessors,
from the Motorola to the x86, from the power pc to the 16 bit
Analog devices DSP, that all have a stack.

Since most modern microprocessors use some of those
processors, I would say that most systems have a stack.

A stack is a natural organization for building activation
records. Since C supports recursion, either the machine
has a stack or some behind the scenes software must implement
one.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      02-06-2007
(E-Mail Removed) 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
 
Reply With Quote
 
fdmfdmfdm@gmail.com
Guest
Posts: n/a
 
      02-06-2007
On Feb 6, 9:41 am, Flash Gordon <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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



Well, I meant to say "heap". OK, for your opinion, if you were asked
by this question, you will say all these variables will be allocated
on stack? Is there a "correct" answer for this question?



 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-06-2007
In article <(E-Mail Removed)-gordon.me.uk>,
Flash Gordon <(E-Mail Removed)> wrote:
>> My answer: static variable in function and global variable are
>> allocated in head, and automatic variable is allocated in stack.


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


I think you must be interpreting "heap" in a narrow sense then. On
many unix implementations for example, the memory returned by malloc()
follows on directly from the area in which static data is stored, and
it is not unreasonable to use the term "heap" to refer to that entire
area. Presumably that is what the original poster had in mind.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-06-2007
(E-Mail Removed) wrote On 02/06/07 12:26,:
> On Feb 6, 9:41 am, Flash Gordon <(E-Mail Removed)> wrote:
>
>>(E-Mail Removed) 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?

>
> [...]
>
> Well, I meant to say "heap". OK, for your opinion, if you were asked
> by this question, you will say all these variables will be allocated
> on stack? Is there a "correct" answer for this question?


One correct answer goes something like this: "The
implementation can allocate memory in any way it likes,
so long as the allocations satisfy the requirements of
the C language." In answering the obvious follow-up,
the phrases "static storage duration" and "automatic
storage duration" may be useful (pedantry: variables
don't use "dynamic storage duration").

<off-topic>

Is it my imagination, or has somebody unleashed a
flood of inane interview questions recently? Half the
newsgroups I follow are being submerged with postings a
lot like fdmfdmfdm's, posing questions of similar, er,
depth. Is there book with a title like "1001 Moronic
Interview Questions" in circulation?

And the bigger issue: If you faced and aced a string
of such questions in an interview and were offered the
job, would you take it? Or would you flee?

"It's not enough to keep the mind alive."
-- Peter Cook in "Beyond the Fringe"

</off-topic>

--
(E-Mail Removed)
 
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
Initialized and uninitialized global variable allocation Cristea Bogdan C Programming 9 04-15-2012 05:21 PM
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
Regarding global variables memory allocation and gtime funtion sunil C Programming 1 02-27-2008 08:56 PM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM



Advertisments