Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:43:14+00:00 2026-05-14T23:43:14+00:00

I’ve read about the save statement in the (Intel’s) language reference document, but I

  • 0

I’ve read about the save statement in the (Intel’s) language reference document, but I cannot quite grasp what it does. Could someone explain to me in simple language what it means when the save statement is included in a module ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:43:15+00:00Added an answer on May 14, 2026 at 11:43 pm

    In principal when a module goes out-of-scope, the variables of that module become undefined — unless they are declared with the SAVE attribute, or a SAVE statement is used. “Undefined” means that you are not allowed to rely on the variable having the previous value if you again use the module — it might have the previous value when you re-access the module, or it might not — there is no guarantee. But many compilers don’t do this for module variables — the variables probably retain their values — it isn’t worth the effort for the compiler to figure out whether a module remains in scope or not and probably module variables are treated as global variables — but don’t rely on that! To be safe, either use “save” or “use” the module from the main program so that it never goes out of scope.

    “save” is also important in procedures, to store “state” across invocations of the subroutine or function (as written by @ire_and_curses) — “first invocation” initializations, counters, etc.

    subroutine my_sub (y)
    
    integer :: var
    integer, save :: counter = 0
    logical, save :: FirstCall = .TRUE.
    
    counter = counter + 1
    
    write (*, *) counter
    
    if (FirstCall) then
       FirstCall = .FALSE.
       ....
    end if
    
    var = ....
    

    etc.

    In this code fragment, “counter” will report the number of invocations of subroutine x. Though actually in Fortran >=90 one can omit the “save” because the initialization in the declaration implies “save”.

    In contrast to the module case, with modern compilers, without the save attribute or initialization-on-a-declaration, it is normal for local variables of procedures to lose their values across invocations. So if you attempt to use “var” on an later call before redefining it in that call, the value is undefined and probably won’t be the value calculated on a previous invocation of the procedure.

    This is different from the behavior of many FORTRAN 77 compilers, some of which retained the values of all local variables, even though this wasn’t required by the language standard. Some old programs were written relying on this non-standard behavior — these programs will fail on the newer compilers. Many compilers have an option to use the non-standard behavior and “save” all local variables.

    LATER EDIT: update with a code example that shows incorrect usage of a local variable that should have the save attribute but doesn’t:

    module subs
    
    contains
    
    subroutine asub (i, control)
    
       implicit none
    
       integer, intent (in) :: i
       logical, intent (in) :: control
    
       integer, save :: j = 0
       integer :: k
    
       j = j + i
       if ( control ) k = 0
       k = k + i
    
       write (*, *) 'i, j, k=', i, j, k
    
    end subroutine asub
    
    end module subs
    
    program test_saves
    
       use subs
       implicit none
    
       call asub ( 3, .TRUE. )
       call asub ( 4, .FALSE. )
    
    end program test_saves
    

    Local variable k of the subroutine is intentionally misused — in this program it is initialized in the first call since control is TRUE, but on the second call control is FALSE, so k is not redefined. But without the save attribute k is undefined, so the using its value is illegal.

    Compiling the program with gfortran, I found that k retained its value anyway:

     i, j, k=           3           3           3
     i, j, k=           4           7           7
    

    Compiling the program with ifort and aggressive optimization options, k lost its value:

     i, j, k=           3           3           3
     i, j, k=           4           7           4
    

    Using ifort with debugging options, the problems was detected at runtime!

     i, j, k=           3           3           3
    forrtl: severe (193): Run-Time Check Failure. The variable 'subs_mp_asub_$K' is being used without being defined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 393k
  • Answers 393k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use absolute positioning. HTML <div id="content"> <div id="header">… May 15, 2026 at 1:53 am
  • Editorial Team
    Editorial Team added an answer - (void) applicationDidFinishLaunching: (UIApplication*) application { [_window addSubview: [_firstView view]];… May 15, 2026 at 1:53 am
  • Editorial Team
    Editorial Team added an answer Use normal Java File IO operations(see class-documentation) Here is the… May 15, 2026 at 1:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.