By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,602 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,602 IT Pros & Developers. It's quick & easy.

Static variable mapping !!

P: n/a
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;
}
/* in above case where is variable k and j mapped in memory layout ? */
main()
{
int i = 10 ;
printf("%d",func(i));
}

Could you plz tell me where exactly local , global , static variables mapped
in memory layout ?

Definitely above code is not re-enterent ? what else conditions need to be
taken care in writing re - enterent code (apart from making atomic
instructions )

Thanx.
Datta
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Datta Patil wrote:
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;
}
/* in above case where is variable k and j mapped in memory layout ? */
main()
{
int i = 10 ;
printf("%d",func(i));
}
#include <stdio.h>

int /* mha: the return type defaults to
'int' in C89. For the last 5 years,
the standard has required that you
specify it, since the implicit int
is no longer supported */
func( /* static */ int k)
{ /* mha: specifying a storage class for
the paramater 'k' is an error. If
you are not getting a diagnostic,
you probably do not have the
diagnostic level set appropriately,
or you are not invoking the compiler
as a compliant C compiler. */
int i = 10;
#if 0
static int j = &i; /* mha: It is a mistake to try to store
pointer values in ints. It is
allowed, but the guarantees as to
what it means are murky. It is
possible that the information in a
pointer value won't even fit in an
int, or, if it can, that its
interpretation is problematic.
Furthermore, the value of &i, the
address of an automatic variable, is
not constant. */
#endif
int *j = &i; /* mha: replacement */
return k;
}

int /* mha: see comment above */ main(void)
{
int i = 10;
printf("%d", func(i));
return 0; /* mha: It is a good idea to return
values from functions that claim to
return values. You rely on the
implicit int return type from
functions, so supply a return value */
}

Could you plz tell me where exactly local , global , static variables mapped
in memory layout ?
No, because such things are completely implementation-specific. Your
question has *no* meaning.
Definitely above code is not re-enterent ?


You code is hopelessly broken, so re-entrancy is hardly a major concern.
Nov 14 '05 #2

P: n/a
Datta Patil wrote:

Could you plz tell me where exactly local , global , static variables mapped
in memory layout ?


On Unix systems a program is contained in a process. The process is a
single thread of execution and a virtual memory space. The typical
runtime environment will place the program code at the low end of the
memory space, followed by static variables (which are part of the image
that is loaded from disk --- it is how they get initialized). After
this is the heap which builds up. The environmental varables are at the
high end of the memory space and just beneath them is a stack which
builds down. (Like I said this is typical, it could vary from system to
system.)

All malloc() calls add blocks of memory to the top of the heap. free()
releases blocks of memory from the heap. Note that free() could release
a block in the middle of the heap creating a hole (fragmentation). All
function calls add stack frames to the stack which builds down. When
the two meet, you are out of memory, and you may have to try to compact
the heap (garbage collection).

Every time a function is called a new stack frame is created on the
stack. Local variables are stored in the stack frame of the
corresponding function. This allows for re-entrent and recursive
functions. When a function returns, the stack frame is deallocated and
the local variables in it distroyed. So local (or as C calls them
"automatic") variables only live as long as the function does.

Static variables are not stored on the stack so they live between
function calls. The term "global" in C only referes to scope. All
global variables are static.

By default any variable decleared in a function is of storage class
"automatic" You can make this explicit by using the keyword "auto" but
since it is the default, nobody bothers.

If you qualify a variable definition with the keyword "static" inside of
a function, that variable is a static variable (not stored in the
stack frame) but its scope is restricted to the function that defined it
(no other function can refer to this variable, it is private to the
function which decleared it).

By default, if you define a variable outside of a function body its
storage class is static. It will also have global scope. Any function
can see it. A .c file, and all of its "#include"s is called a
"translation unit" A translation unit produces an object file which is
linked by the linker into an executable. If translation unit "A"
defines a variable outside of a function body, that variable will be
static. If translation unit "B" declears the same variable with an
"extern" declearation, then when the two object files are linked, the
code in translation unit "B" will manipulate the variable defined in
translation unit "A" (note the use of the words "define" and "declear").
If translation unit "A" qualifies the variable defination with the
keyword "static", then the variable will have a static storage class,
but its scope will be restricted to the current translation unit. That
is, the symbol is not published to the linker and other translation
units will not be able to access it. The same is true for function
definations which are qualified by the keyword "static" (they remain
local to the translation unit). A translation unit is what some other
languages call a "module."

You should read, "The C Programming Language" by Kernighan & Ritchie.
All of this is in there. O'Reilly's "C Pocket Refrence" is also
surprisingly good, and more update (includes the C99 extentions).

Nov 14 '05 #3

P: n/a
On Mon, 31 May 2004 01:06:20 GMT, David Stark <st****@sbcglobal.net>
wrote in comp.lang.c:
Datta Patil wrote:

Could you plz tell me where exactly local , global , static variables mapped
in memory layout ?


On Unix systems a program is contained in a process.


And where did the OP mention UNIX? Please don't pollute comp.lang.c
with off-topic answers that are totally ungrounded in the C standard.

Local and static variables (C has nothing at all named "global", and
linkage is not storage duration) are mapped in memory wherever the
implementation decides to map them.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.