473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

static, global variable memory allocation

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?

Feb 6 '07
53 26292
On Feb 7, 9:46 am, "santosh" <santosh....@gmail.comwrote:
The words "stack" and "heap" occur nowhere in the C standard. It's
(too) common to refer to the area used to allocate automatic objects
as the "stack", but I think most of us agree that this is misleading
and incorrect.

Can a perverse but conforming implementation use the "heap" for
automatic objects. As long as their properties, as dictated by the
standard, are honoured, I don't see why they should be allocated from
a stack.
IMO, stack suits the requirement of auto variables at best. As soon a
leaf function is called from parent function, stack is used to store
the status of registers, passing arguments, etc and this scope is
defined till the control stays in leaf function. Similar is the case
with auto/local variables of leaf function. Scope is defined till the
control is in leaf function.

You have to free (to tell system that you can re-write it) "Heap"
every time, you use it for local variable.
The word "stack", in this context, implies a
CPU-specific region of memory that grows linearly in one direction,
controlled by a "stack pointer" that is typically (but not always) a
dedicated CPU register.

I may be wrong, but it was my impression that a stack is simply a LIFO
data structure. It doesn't have to have any specific hardware support.
On stackless architectures, a LIFO may have to be simulated by the C
implementation, for auto objects.
It makes life easier. Simply one HW (may/maynot be memroy mapped)
stack pointer is required to refer it across all functions. (when you
are using single stack implementation)

Yes. It curious why so many students of C are bursting to know where C
objects are stored. I would've thought that an assembly language or
machine architecture course would be the proper place to recieve such
information.
I think, There is misnomer in C. C storage classes. I don't understand
why "storage" word is required when it just defines the life of
variable.

-Nishu

Feb 8 '07 #51
Keith Thompson wrote, On 08/02/07 05:49:
"Nishu" <na**********@gmail.comwrites:
>On Feb 7, 5:30 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>> One alternative arises from the fact that "the stack" can
be implemented in different ways. Nowadays it is common to use
sequential allocation: You reserve a special array somewhere,
initialize a pointer to point to one end of it, and let the
pointer wander up and down in the array as functions come and
go. Pretty simple design, and pretty cheap.
Yes, It is common. Systems can either have Top-down or bottom-up
approach for this kind of implementation. Top down approach is
preferred implementation for stack.
[...]

There is no reason (as far as standard C is concerned) to prefer a
top-down stack over a bottom-up stack. (There may be CPU-specific
reasons.)
On some processors I have used there was no HW reason to prefer one
direction over the other. There was no specific reason in terms of HW
for which of the 7 general purpose address registers you used either.
--
Flash Gordon
Feb 8 '07 #52
Nishu wrote:
On Feb 6, 8:41 am, Flash Gordon <s...@flash-gordon.me.ukwrote:

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

I don't see any other alternatives rather than register/stack
allocation for local variables. I would be happy to know alternative
implementations for it.
You could put them in whatever region of store was used for mallocated
memory.

--
Chris "/could/ doesn't mean /should/" Dollin
"Who are you? What do you want?" /Babylon 5/

Feb 8 '07 #53
On Thu, 08 Feb 2007 01:29:06 -0000, go***********@burditt.org (Gordon
Burditt) wrote:
Can a perverse but conforming implementation use the "heap" for
automatic objects.

I'm not so sure it would be that perverse. OS/360 <snip>
Now, when I was using this, C didn't exist. Other languages like
PL/1 and Pascal did. Pascal has a more complex setup than C (the
equivalent of an "auto" variable in an outer function can be referred
to in an inner function, even in the presence of recursion). There's
no reason C couldn't use this technique.
PL/I also had a68-style nesting with downward closure, and was much
more popular on S/360+. FORTRAN did not, nor did COBOL of the day,
although since '85 it does, although I'm not sure it has recursion.
Fortran since '90 has (only) one nesting level, and recursion.

<snip much good>

- formerly david.thompson1 || achar(64) || worldnet.att.net
Feb 26 '07 #54

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: sks_cpp | last post by:
When do static members allocate memory? (at load time) How about static variables within a method? Where is the memory for static variables - is it allocated on the heap or does it have a...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
3
by: Pavan | last post by:
Hi i would like to know abt the data variable storage in C like 1.where does global variables gets stored and why is it so .. 2.like wise static ,local variables, as for as i know i remember...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
18
by: Jack | last post by:
Thanks.
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.