----- Original Message -----
From: "chandru srikan" <uttarafriends2003@yahoo.com>
To: "Vijay Kumar R Zanvar" <vijoeyz@hotpop.com>
Sent: Tuesday, November 11, 2003 2:01 PM
Subject: Re: [UTTARA] Static variable

>if static var decleared inside the function then
> where it is stored

This one didn't strike my mind.  I also didn't know where
the static varible declared inside a function would go?
Later, I set on studying the assembly code generated by
gcc.  Following is, according to my knowledge, the explaination:

If it is static, it is more likely that it will be placed
in the data section.  Now, how a compiler hides a static
identifier from the loader is upto itself.

Please note that the following discussion is GCC specific.

/*
 * static.c     -   `static' storage analysis
 * Date         -    Nov 12, 2003
 */

#include <stdio.h>

/* Avoid warning: `ident' defined but not used */
#ifdef __GNUC__
#define UNUSED  __attribute__ (( unused ))

/* Turbo C/C++ specific */
#elif __TCPLUSPLUS__ && __MSDOS__
#define UNUSED
#pragma warn -var

#else
#define UNUSED
#endif

static int i UNUSED;        /* 1  See explaination */
static int j UNUSED = 10;
int ii UNUSED;

int
main ( void )
{
    static int i UNUSED;        /* 2 */
    static int j UNUSED = 20;
    return 0;
}

void
fun ( void )
{
    static int i UNUSED;           /* 3 */
    static int j UNUSED = 30;
}

/* End of program */

The following is the assembly code generated by:

$ gcc static.c -S -Wall


    .file   "static.c"
    .section .data
    .p2align 2
_j:                         /* 1 */
    .long   10
.lcomm _i.0,16              /* 2 : main () */
    .p2align 2
_j.1:                       /* 2 : main () */
    .long   20
    .section .text
.globl _main
_main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    andl    $-16, %esp
    movl    $0, %eax
    subl    %eax, %esp
    movl    $0, %eax
    leave
    ret
.lcomm _i.2,16          /* 3 */
    .section .data
    .p2align 2
_j.3:                   /* 3 */
    .long   30
    .section .text
.globl _fun
_fun:
    pushl   %ebp
    movl    %esp, %ebp
    popl    %ebp
    ret
.lcomm _i,16        /* 1 */
.comm _ii,16
    .ident  "GCC: (GNU) 3.3.2"

/* End of static.s */


Explaination:

    Notice the following two assembler pseudo-ops:

     i) .lcomm
    ii) .comm

    Both are used to allocate space in the BSS.  The .lcomm
    (stands for local common) is used for static and .comm
    for non-static global variables.

    For more information, see "info as".

    Notice also that the gcc compiler prepends a global variable
    with an /underscore/.

    .globl directive of the assembler makes a symbol visible to
    the loader (ld).

    /* 1 */
    A global static indentifier falls into the data section, but
    doesn't come under .globl section.  This means the loader
    doesn't even know the existence of j and i here.  .locomm
    directive used for static variables.

    But, if you see .comm is used for ii, viz., the loader can
    see the symbol ii.

    /* 2 */
    i and j (_i.0 and _j.1) of main are represented as shown.
    Notice the postion with respect to .globl directive.

    /* 3 */
    i and j (_i.2 and _j.3) of fun () are again represented
    in the same way.



I hope this is not complex.

Thanks.
vijay-z.