Static Declarations

A

AnandRaj

Hi guys,
I have a few doubts in C.
1. Why static declartions are not allowed inside structs?
eg
struct a {
static int i;
};

Throws an error ..

2.How does the complier diffrenciate between a Global Variable and
Static Variable as both of them will be in the same stack?

Any pointers wud be helpful!
Is there any book or site which gives details abt how the memory is handled for
diffrent variables?


TIA
Anand
 
J

Joona I Palaste

AnandRaj said:
Hi guys,
I have a few doubts in C.
1. Why static declartions are not allowed inside structs?
eg
struct a {
static int i;
};
Throws an error ..

What do you suppose it would *mean* to have a static declaration
inside a struct? How would it differ from a normal member? You might
just well be asking why this is not valid:

struct typedef(void a) {
if (volatile == b) &(c);
}
2.How does the complier diffrenciate between a Global Variable and
Static Variable as both of them will be in the same stack?

There is no concept of "stack" in C. When generating code, the
compiler knows, of course, which file-scope variables are static and
which aren't. It can then generate different code for access to both
variables. The generated object code doesn't, and doesn't need to,
know anything about "static".
Any pointers wud be helpful!

Here is one:
int *a;
Is there any book or site which gives details abt how the memory is handled for
diffrent variables?

Try a book abt, I mean about, implementing compilers, or a manual for
your own compiler.
 
C

Christian Bau

Hi guys,
I have a few doubts in C.
======

Questions, not doubts. "Question" means you don't know something, and
you ask people to find out. "Doubt" means you have some information, but
you don't quite believe it. For example, if I tell you that I can
multiply ten digit numbers by head then you will doubt that (you don't
believe it, but it might just be true). If I tell you that I can
multiply thousand digit numbers in my head then you don't just doubt it,
you will know it is not true.

1. Why static declartions are not allowed inside structs?
eg
struct a {
static int i;
};

Because it wouldn't be meaningful. "static" is used for functions and
variables that exist permanently, but whose names can be used only in
one file. A struct doesn't declare a variable, instead it gives a list
of components that will be there in any variable of that struct type.


2.How does the complier diffrenciate between a Global Variable and
Static Variable as both of them will be in the same stack?
=================

They won't be in a "stack" at all. They are there permanently while your
program runs; stacks are used for situations when things are added and
removed again in exactly the opposite order of adding them. For example,
if you call a function then all the local variables start existing when
the function is called and they stop to exist when the function returns;
these variables would likely be in a stack, but global and static
variables are not in a stack.

The difference between global and static: When you have a global
variable x, the compiler remembers "global variable x". If you use the
global variable x in two files, then it is "global variable x" each
time, so it is the same variable. When you have a static variable x in
file a, then the compiler remembers "static variable x in file a"; if
you use a static variable x in file b the compiler remembers "static
variable x in file b". Both are separate things.

Static variables are useful because they cannot be used in different
files by accident. Lets say you used a global int x in some code.
Someone else might need a global variable in his or her code and also
call it x. If both codes are used in the same program, then you will
have a problem because the other persons code will change your global
variable. With static variables, you don't have that problem.
 
T

Tim Rentsch

1. Why static declartions are not allowed inside structs?
eg
struct a {
static int i;
};

Throws an error ..

It needn't be an error. C++ allows 'static' struct (and class)
members. They are analogous to 'static' function variables.

2.How does the complier diffrenciate between a Global Variable and
Static Variable as both of them will be in the same stack?

Clearly the poster here is confused. It's unlikely (not impossible
but unlikely) that either 'extern' variables or 'static' variables
would be placed in "a stack". They are likely to be placed in a
region of memory that holds both 'extern' and 'static' variables.
That's not required though - different machine architectures might
motivate different choices for the two kinds of variables.

Any pointers wud be helpful!
Is there any book or site which gives details abt how the memory is
handled for diffrent variables?

It depends why you're asking. If you're looking for information
about a particular architecture (x86, for example), and a particular
operating system running on that architecture, probably most
of the compilers do the same thing, so a compiler reference manual
would likely be the place to look

On the other hand, if you're looking for choices made for different
architectures, a good compiler book that considers choices for
lots of different kinds of architectures is a better bet.

Just a suggestion - posting questions like this one, it often
helps to say why you want the information as well as what information
you're looking for.
 
R

rsk

Tim said:
It needn't be an error. C++ allows 'static' struct (and class)
members. They are analogous to 'static' function variables.


No.It is an error in C . I thing the question is why the above snippet is not
allowed in C not C++?
 
S

Sydney Faria

as far as considering a static member a way to create a 'namespace' that was
already done by a global static variable limited by file scope and a static
member of a struct would be doing the same thing with more semantics being
involved! A struct in meant to be a collection of related, but different
data types which are realized when the struct is 'instantiated'. The
definition of the struct creates a new data type and can be considered as a
primitive beginning to more complete data abstraction when classes were
created in C++. It is also worthwhile mentioning that structs in C++ exist
and can have methods defined within structs..
scf
 
J

Joe Wright

AnandRaj said:
Hi guys,
I have a few doubts in C.
1. Why static declartions are not allowed inside structs?
eg
struct a {
static int i;
};

Throws an error ..

2.How does the complier diffrenciate between a Global Variable and
Static Variable as both of them will be in the same stack?

Any pointers wud be helpful!
Is there any book or site which gives details abt how the memory is handled for
diffrent variables?


TIA
Anand

I'm sorry you haven't gotten a better answer until now. Let me try..

The static keyord is overloaded in C. It has to do with storage
class and with linkage.

1. Storage class: All object definitions outside of any function in
a translation unit have static storage. That means the compiler
makes room for it in a data segment somewhere at compile time,
before run time. Within a function, a definition qualified with
'static' also places the object in the data segment at compile time.

2. Linkage: All object (and function) definitions outside of any
function in a translation unit have external linkage. They can be
'seen' and accessed by code in other translation units of the
program. If they are qualified as 'static' they will not be
broadcast to the linker and will disappear to all but the current
translation unit.

To your question, a struct is an object of its own. Outside any
function is has static storage class. The members of a struct share
its storage class.
 
T

Tim Rentsch

rsk said:
No.It is an error in C . I thing the question is why the above
snippet is not allowed in C not C++?

I didn't say it wasn't an error. I said it needn't be an error.
There's a clear and reasonable interpretation for what such a
declaration would mean, and the cost of implementation is fairly
small. What perhaps I should have added, but didn't because it seems
obvious, is that 'static' struct members aren't in C because most
people in the C community don't think they are important enough to put
in.
 
J

Joona I Palaste

I didn't say it wasn't an error. I said it needn't be an error.
There's a clear and reasonable interpretation for what such a
declaration would mean, and the cost of implementation is fairly
small. What perhaps I should have added, but didn't because it seems
obvious, is that 'static' struct members aren't in C because most
people in the C community don't think they are important enough to put
in.

Correct me if I'm wrong here, but isn't this just syntactic sugar? How
would this:

struct a {
static int i;
int j;
};
struct a a;

be any different from this?

static int a_i;
struct a {
int j;
};
struct a a;

Assuming a static member isn't counted towards the struct size, a.i in
the first example and a_i in the second would have identical scoping and
compile to identical object code. Is there a real need to be able to
write "a.i" instead of "a_i"?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 
R

Richard Tobin

Joona I Palaste said:
Assuming a static member isn't counted towards the struct size, a.i in
the first example and a_i in the second would have identical scoping and
compile to identical object code. Is there a real need to be able to
write "a.i" instead of "a_i"?

In a language that allowed dynamic typing, you might be able to do it
with a pointer to a object of unknown type, which would be useful.
This works in some object-oriented languages, but not in Java, since
it's the lexical type rather than the real type that determines the
slot value: only methods are dynamic.

The nearest you can get to that sort of dynamic typing in C is having
multiple struct types with the same members, and you couldn't make
static members work without adding a lot of mechanism.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top