473,383 Members | 1,853 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,383 software developers and data experts.

Static variable mapping !!

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
3 3902
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
6
by: Michael B Allen | last post by:
I want to initialize a static variable to a "random" value like: static void * get_key(struct dnsreq *req) { static uint16_t next_txnid = (uint32_t)req & 0xFFFF; But gcc gives me an error: ...
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
9
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
3
by: Richard K Miller | last post by:
Here's an OOP question that perplexes me. It seems PHP doesn't treat static variables correctly in child classes. <?php class ABC { public $regular_variable = "Regular variable in ABC\n";...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
19
by: Steven Blair | last post by:
Hi, I have webservice which holds a static variable. I send my first message into the webservice and the static variable gets a value. If I queried the webservice at a later time (say after 1...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.