Is there a "static" in Fortran like in C++? - Fortran

This is a discussion on Is there a "static" in Fortran like in C++? - Fortran ; class element { Vector *vsSubdivide; Matrix *fsSubdivide; static Vector *SsrSubdivide; static int maxNumSections; }; "static" statment is used for saving memory. I use Fortran Type and is there a "static" in Fortran like in C++? Type element real(8), pointer :: ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Is there a "static" in Fortran like in C++?

  1. Default Is there a "static" in Fortran like in C++?

    class element
    {
    Vector *vsSubdivide;
    Matrix *fsSubdivide;
    static Vector *SsrSubdivide;
    static int maxNumSections;
    };

    "static" statment is used for saving memory. I use Fortran Type and is
    there a "static" in Fortran like in C++?

    Type element
    real(8), pointer :: vsSubdivide
    real(8), pointer :: fsSubdivide
    ... ! here ??
    ... ! here ??
    End Type element

  2. Default Re: Is there a "static" in Fortran like in C++?


    > Type element
    > real(8), pointer :: vsSubdivide( => null()
    > real(8), pointer :: fsSubdivide( => null()
    > ... ! here ??
    > ... ! here ??
    > End Type element



  3. Default Re: Is there a "static" in Fortran like in C++?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hello,

    Simulate schrieb:
    > class element
    > {
    > Vector *vsSubdivide;
    > Matrix *fsSubdivide;
    > static Vector *SsrSubdivide;
    > static int maxNumSections;
    > };
    >
    > "static" statment is used for saving memory. I use Fortran Type and is
    > there a "static" in Fortran like in C++?
    >
    > Type element
    > real(8), pointer :: vsSubdivide
    > real(8), pointer :: fsSubdivide
    > ... ! here ??
    > ... ! here ??
    > End Type element


    This is not possible in exactly the same manner as in C++. However, using module encapsulation
    you can do

    module mod_element
    private
    implicit none
    integer, parameter :: rk = selected_real_kind(12,100)
    integer, save, target :: maxnumsections = -1
    real(rk), save, allocatable, target :: SsrSubdivide(
    type, public :: element
    real(rk), pointer :: vsSubdivide
    real(rk), pointer :: fsSubdivide
    integer, pointer :: maxNumSections => null()
    real(rk), pointer :: SsrSubdivide( => null()
    end type
    contains
    integer function init_element(this, ...)
    type(element) :: this
    :
    init_element = 0
    if (.not. associated(this%maxNumSections)) then
    this%maxNumSections => maxnumsections
    : ! possibly set starting values
    else
    : ! return error status to init_element
    return
    end if
    : ! allocate global SsrSubdivide if not yet done
    : ! and set starting values
    if (.not. associated(this%SsrSubdivide)
    this%SsrSubdivide => this%SsrSubdivide
    else
    ! return error status to init_element
    return
    end if
    end function
    : ! further module procedures
    end module

    As in C++, beware race conditions if updating globals in threaded / OpenMP
    parallel regions.

    Regards
    Reinhold
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD8DBQFHjhcmFVLhKuD7VgsRAiaCAKDROtM5EAC01WNVRJ0rlbB/Bf4sywCgwC+o
    bjRZwloOUCWcLK9PkuttoNU=
    =FTwG
    -----END PGP SIGNATURE-----

  4. Default Re: Is there a "static" in Fortran like in C++?

    Reinhold Bader wrote:
    > Hello,
    >
    > Simulate schrieb:
    >> class element
    >> {
    >> Vector *vsSubdivide;
    >> Matrix *fsSubdivide;
    >> static Vector *SsrSubdivide;
    >> static int maxNumSections;
    >> };

    >
    >> "static" statment is used for saving memory. I use Fortran Type and is
    >> there a "static" in Fortran like in C++?

    >
    >> Type element
    >> real(8), pointer :: vsSubdivide
    >> real(8), pointer :: fsSubdivide
    >> ... ! here ??
    >> ... ! here ??
    >> End Type element

    >
    > This is not possible in exactly the same manner as in C++. However, using module encapsulation
    > you can do
    >
    > module mod_element
    > private
    > implicit none
    > integer, parameter :: rk = selected_real_kind(12,100)
    > integer, save, target :: maxnumsections = -1
    > real(rk), save, allocatable, target :: SsrSubdivide(
    > type, public :: element
    > real(rk), pointer :: vsSubdivide
    > real(rk), pointer :: fsSubdivide
    > integer, pointer :: maxNumSections => null()
    > real(rk), pointer :: SsrSubdivide( => null()
    > end type
    > contains
    > integer function init_element(this, ...)
    > type(element) :: this
    > :
    > init_element = 0
    > if (.not. associated(this%maxNumSections)) then
    > this%maxNumSections => maxnumsections
    > : ! possibly set starting values
    > else
    > : ! return error status to init_element
    > return
    > end if
    > : ! allocate global SsrSubdivide if not yet done
    > : ! and set starting values
    > if (.not. associated(this%SsrSubdivide)
    > this%SsrSubdivide => this%SsrSubdivide
    > else
    > ! return error status to init_element
    > return
    > end if
    > end function
    > : ! further module procedures
    > end module
    >
    > As in C++, beware race conditions if updating globals in threaded / OpenMP
    > parallel regions.
    >
    > Regards
    > Reinhold

    Alternatively, the SAVE qualifier may interest you. As Reinhold says,
    it will be problematical in threaded parallel regions.

  5. Default Re: Is there a "static" in Fortran like in C++?

    "Simulate" <li.simula@gmail.com> wrote in message
    news:860bf226-91c2-45ed-8b7d-6351d1fb18cb@v29g2000hsf.googlegroups.com...
    > class element
    > {
    > Vector *vsSubdivide;
    > Matrix *fsSubdivide;
    > static Vector *SsrSubdivide;
    > static int maxNumSections;
    > };
    >
    > "static" statment is used for saving memory. I use Fortran Type and is
    > there a "static" in Fortran like in C++?
    >
    > Type element
    > real(8), pointer :: vsSubdivide
    > real(8), pointer :: fsSubdivide
    > ... ! here ??
    > ... ! here ??
    > End Type element


    Your understanding about the effect of the "static" keyword in C may be
    incorrect. According to the C99 Standard, ISO/IEC 9899:1999, the "static"
    keyword designates that an object has "static" storage class. Objects in the
    "static" storage class have duration during the entire execution of the program
    but are not visible outside of the block, procedure, or file in which they are
    declared.

    Thus, in C, "static" declares both duration and scope.

    Fortran has no exact equivalent, because, with some very limited exceptions,
    Fortran attributes are independent of each other. There is no one keyword that
    designates both persistent existence and limited scope. Instead, you have to
    use separate attributes to get this combination.
    Save - This attribute makes the object persistent during the entire
    execution of the program.
    Private - This attribute limits the scope or visibility of the object.
    I believe that you were looking for the Save attribute, but I am not sure, so I
    gave you this longer explanation.

    There is one important way to get the Save attribute without specifically
    declaring it. This is to initialize the object using either the Data statement
    or via an initialization expression. This is known as the "Data implies Save"
    rule and has been part of Fortran since 1977. It is counter-intuitive, it is a
    source of endless confusion, but unfortunately it is here to stay, because
    changing it would break too much existing code.

    The opposite of "Save" is "Automatic". Unfortunately, the "Automatic"
    attribute is not standardized, even though there have been efforts to
    standardize it. Many Fortran comilers offer it as an extension. If the "Save"
    attribute is missing from a data object declaration, then the persistence of
    that data object is processor dependent; it is **NOT** automatic by default.

    Hope this helps.

    --
    Craig Dedo
    17130 W. Burleigh Place
    P. O. Box 423
    Brookfield, WI 53008-0423
    Voice: (262) 783-5869
    Fax: (262) 783-5928
    Mobile: (414) 412-5869
    E-mail: <cdedo@wi.rr.com> or <craig@ctdedo.com>


  6. Default Re: Is there a "static" in Fortran like in C++?

    The "static" in C++ may save memory when serval objects are declared
    for they can share the same memory of variable with "static" of all
    declared obects.

    Reinhold Bader's advise is good. However, it is not convenience.

  7. Default Re: Is there a "static" in Fortran like in C++?

    On Jan 16, 3:04�pm, "Craig Dedo" <cd...@wi.rr.com> wrote:
    > "Simulate" <li.sim...@gmail.com> wrote in message
    >
    > news:860bf226-91c2-45ed-8b7d-6351d1fb18cb@v29g2000hsf.googlegroups.com...
    >
    >
    >
    >
    >
    > > class element
    > > {
    > > � �Vector *vsSubdivide;
    > > � �Matrix *fsSubdivide;
    > > � �static Vector *SsrSubdivide;
    > > � �static int maxNumSections;
    > > };

    >
    > > "static" statment is used for saving memory. I use Fortran Type and is
    > > there a "static" in Fortran like in C++?

    >
    > > Type element
    > > � real(8), pointer :: �vsSubdivide
    > > � real(8), pointer :: �fsSubdivide
    > > � ... ! here ??
    > > � ... ! here ??
    > > End Type element

    >
    > � � Your understanding about the effect of the "static" keyword in C may be
    > incorrect. �According to the C99 Standard, ISO/IEC 9899:1999, the "static"
    > keyword designates that an object has "static" storage class. �Objects in the
    > "static" storage class have duration during the entire execution of the program
    > but are not visible outside of the block, procedure, or file in which theyare
    > declared.
    >
    > � � Thus, in C, "static" declares both duration and scope.
    >
    > � � Fortran has no exact equivalent, because, with some very limited exceptions,
    > Fortran attributes are independent of each other. �There is no onekeyword that
    > designates both persistent existence and limited scope. �Instead, you have to
    > use separate attributes to get this combination.
    > � � Save - This attribute makes the object persistent during the entire
    > execution of the program.
    > � � Private - This attribute limits the scope or visibility of the object.
    > I believe that you were looking for the Save attribute, but I am not sure,so I
    > gave you this longer explanation.
    >
    > � � There is one important way to get the Save attribute without specifically
    > declaring it. �This is to initialize the object using either the Data statement
    > or via an initialization expression. �This is known as the "Data implies Save"
    > rule and has been part of Fortran since 1977. �It is counter-intuitive, it is a
    > source of endless confusion, but unfortunately it is here to stay, because
    > changing it would break too much existing code.
    >
    > � � The opposite of "Save" is "Automatic". �Unfortunately, the "Automatic"
    > attribute is not standardized, even though there have been efforts to
    > standardize it. �Many Fortran comilers offer it as an extension. �If the "Save"
    > attribute is missing from a data object declaration, then the persistence of
    > that data object is processor dependent; it is **NOT** automatic by default.
    >
    > � � Hope this helps.
    >
    > --
    > Craig Dedo
    > 17130 W. Burleigh Place
    > P. O. Box 423
    > Brookfield, WI � 53008-0423
    > Voice: �(262) 783-5869
    > Fax: � �(262) 783-5928
    > Mobile: (414) 412-5869
    > E-mail: <cd...@wi.rr.com> or <cr...@ctdedo.com>- Hide quoted text -
    >
    > - Show quoted text -


    This is a common misconception about F77; the relevant part of the
    standard states:

    15.8.4 Definition Status.
    Execution of a RETURN statement (or END statement) within a subprogram
    causes all entities within the subprogram to become undefined, except
    for the following:
    - Entities specified by SAVE statements
    - Entities in blank common
    - Initially defined entities that have neither been redefined or
    become undefined
    - Entities in a named common block that appears in the subprogram and
    appears in at least one other program unit that is referencing, either
    directly or indirectly, the subprogram
    Note that if a named common block appears in the main program, the
    entities in the named common block do not become undefined at the
    execution of any RETURN statement in the executable program.

    So that if a local variable in a subprogram is initialised, and then
    set to a different value, it becomes undefined on exit. I suspect that
    the reason for this was that different existing compilers behaved
    differently at the time the standard was written. No doubt, it
    practice, the variable was set to either the value in the DATA
    statement or the value in the assignment statement.

    Dave Flower




  8. Default Re: Is there a "static" in Fortran like in C++?

    Simulate wrote:
    > class element
    > {
    > Vector *vsSubdivide;
    > Matrix *fsSubdivide;
    > static Vector *SsrSubdivide;
    > static int maxNumSections;
    > };


    > "static" statment is used for saving memory. I use Fortran Type and is
    > there a "static" in Fortran like in C++?


    Static has about three different meanings in C89, more in C99, and
    some in C++ are even more different.

    http://msdn2.microsoft.com/en-us/library/s1sb61xd.aspx

    "saving memory" is not really the meaning of static in this case.
    It means that there is only one copy of the variable for the class,
    instead of one for each instance of the class. That might save memory
    (especially for a very large variable), but it is normally done because
    the data is needed between instances, and not to save memory.

    > Type element
    > real(8), pointer :: vsSubdivide
    > real(8), pointer :: fsSubdivide
    > ... ! here ??
    > ... ! here ??
    > End Type element


    One thing a class does is define some data structures. A type
    statement just defines a data structure. A variable of the
    specified type might be static, but the whole data structure
    (variable) would be static. To do what you ask, you need a
    separate TYPE for static (SAVEd) data and non-static data.

    Traditionally, Fortran only supplied static data. With the
    addition of recursion, it was necessary that local variables
    are allocated each time a routine is called. The SAVE attribute
    and statement allow one to ask for static storage (one copy
    for all instances of a function). In a non-recursive
    routine it is implementation dependent whether local variables
    are static or not.

    Type element
    real(8), pointer :: vsSubdivide
    real(8), pointer :: fsSubdivide
    End Type element

    Type element_static
    ... ! here ??
    ... ! here ??
    End Type element

    Apply the SAVE attribute to element_static, and not to
    element. Declare the routine RECURSIVE if there will be more
    than one instance active at once.

    -- glen


  9. Default Re: Is there a "static" in Fortran like in C++?

    David Flower wrote:

    > On Jan 16, 3:04�pm, "Craig Dedo" <cd...@wi.rr.com> wrote:

    (snip)

    >>� � The opposite of "Save" is "Automatic". �Unfortunately, the "Automatic"
    >>attribute is not standardized, even though there have been efforts to
    >>standardize it. �Many Fortran comilers offer it as an extension. �If the "Save"
    >>attribute is missing from a data object declaration, then the persistence of
    >>that data object is processor dependent; it is **NOT** automatic by default.


    This is true, but local variables without SAVE in a RECURSIVE routine
    pretty much have to be automatic.

    > This is a common misconception about F77; the relevant part of the
    > standard states:


    > 15.8.4 Definition Status.
    > Execution of a RETURN statement (or END statement) within a subprogram
    > causes all entities within the subprogram to become undefined, except
    > for the following:
    > - Entities specified by SAVE statements
    > - Entities in blank common
    > - Initially defined entities that have neither been redefined or
    > become undefined
    > - Entities in a named common block that appears in the subprogram and
    > appears in at least one other program unit that is referencing, either
    > directly or indirectly, the subprogram
    > Note that if a named common block appears in the main program, the
    > entities in the named common block do not become undefined at the
    > execution of any RETURN statement in the executable program.


    > So that if a local variable in a subprogram is initialised, and then
    > set to a different value, it becomes undefined on exit. I suspect that
    > the reason for this was that different existing compilers behaved
    > differently at the time the standard was written. No doubt, it
    > practice, the variable was set to either the value in the DATA
    > statement or the value in the assignment statement.


    As I understand it, this was mostly to allow for the behavior
    of overlays.

    http://en.wikipedia.org/wiki/Overlay_%28programming%29

    Local static variables allocated with the code would be overwritten
    by an overlay, and the standard is written to allow that.

    The OP seemed concerned with saving memory, and not keeping a
    value between calls. Still, in the absence of overlays it is
    usual for SAVEd variables to keep their value between calls.
    In those rare cases where it is needed (the state variables
    for a pseudo-random number generator for example) it is
    usual to expect SAVE to do it.

    -- glen


  10. Default Re: Is there a "static" in Fortran like in C++?

    Craig Dedo wrote:
    ....
    > Your understanding about the effect of the "static" keyword in C
    > may be incorrect. According to the C99 Standard, ISO/IEC 9899:1999,
    > the "static" keyword designates that an object has "static" storage
    > class. Objects in the "static" storage class have duration during
    > the entire execution of the program but are not visible outside of
    > the block, procedure, or file in which they are declared.
    >
    > Thus, in C, "static" declares both duration and scope.


    Which are actually two orthogonal properties. *ALL* "file
    scoped" symbols in C have persistent duration, and in that
    context "static" means what Fortran would call "private".
    On the other hand, *NO* symbols that don't have "file scope"
    are visible from outside, and in that context "static" confers
    persistent duration. This is basically an inconsistent use
    of the keyword. It just so happens that the distinct meanings
    don't ever appear in the same context so it can all be expressed
    in the above single sentence as if it's really consistent.

    The OP was using "static" in one of the new C++ ways. C++
    introduced two or three additional contexts where you can use
    the keyword "static" that also appear basically inconsistent.
    In the case in question here, the meaning of "static" is what
    Smalltalk would have called a "class variable" as opposed to
    an "instance variable":

    class element
    {
    Vector *vsSubdivide;
    Matrix *fsSubdivide;
    static Vector *SsrSubdivide;
    static int maxNumSections;
    };

    Here, the last two variables are shared by all instances of the
    class. If I have two objects of class element, A and B say, then
    A.maxNumSections is the *same* thing as B.maxNumSections.

    I can sympathize with the problem. The original intent was that
    C++ would remain backward compatible with C. But C has
    reserved keywords. So they were adding all kinds of new features
    while trying not to introduce any new keywords. Probably a really
    bad idea.

    --
    J. Giles

    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare



+ Reply to Thread
Page 1 of 2 1 2 LastLast