Static Declarations

Discussion in 'C Programming' started by AnandRaj, Aug 19, 2004.

  1. AnandRaj

    AnandRaj Guest

    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
    AnandRaj, Aug 19, 2004
    #1
    1. Advertising

  2. AnandRaj <> scribbled the following:
    > 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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "All that flower power is no match for my glower power!"
    - Montgomery Burns
    Joona I Palaste, Aug 19, 2004
    #2
    1. Advertising

  3. In article <>,
    (AnandRaj) wrote:

    > 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.
    Christian Bau, Aug 19, 2004
    #3
  4. AnandRaj

    Tim Rentsch Guest

    > 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.
    Tim Rentsch, Aug 19, 2004
    #4
  5. AnandRaj

    rsk Guest

    Tim Rentsch wrote:
    >
    > > 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.



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


    >
    > > 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.
    >
    rsk, Aug 20, 2004
    #5
  6. AnandRaj

    Sydney Faria Guest

    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

    "AnandRaj" <> wrote in message
    news:...
    > 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
    Sydney Faria, Aug 22, 2004
    #6
  7. AnandRaj

    Joe Wright Guest

    AnandRaj wrote:

    > 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.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 23, 2004
    #7
  8. AnandRaj

    Tim Rentsch Guest

    rsk <> writes:

    > Tim Rentsch wrote:
    > >
    > > > 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.

    >
    >
    > 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.
    Tim Rentsch, Aug 23, 2004
    #8
  9. Tim Rentsch <> scribbled the following:
    > rsk <> writes:
    >> Tim Rentsch wrote:
    >> > > 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.

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


    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 () ------------- 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
    Joona I Palaste, Aug 23, 2004
    #9
  10. In article <cgc9me$qoi$>,
    Joona I Palaste <> wrote:

    >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
    Richard Tobin, Aug 23, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff
    Replies:
    1
    Views:
    391
    Mark A. Odell
    Nov 14, 2003
  2. Minti

    Re: Static Declarations

    Minti, Aug 19, 2004, in forum: C Programming
    Replies:
    3
    Views:
    300
    Imanpreet Singh Arora
    Aug 19, 2004
  3. Replies:
    12
    Views:
    587
  4. artun
    Replies:
    2
    Views:
    288
    David Harmon
    Dec 5, 2005
  5. LC's No-Spam Newsreading account

    applets, applications and static declarations

    LC's No-Spam Newsreading account, Jan 20, 2010, in forum: Java
    Replies:
    23
    Views:
    743
    John B. Matthews
    Jan 24, 2010
Loading...

Share This Page