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 1018711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:53:56+00:00 2026-05-16T10:53:56+00:00

I’m working on a public release if a model using Fortran 9x and I’d

  • 0

I’m working on a public release if a model using Fortran 9x and I’d like to design the code to support both static or dynamic memory management.

I’ve only seen one example of a code that supports something like this. It is implemented using preprocessors that look something like this:

The code would include some memory management control file with the following:

#ifdef STATIC
  define NMEM_ N
  define PTR_      # blank
#else
  define NMEM_ :
  define PTR_ ,pointer

The user supplies the following file to configure static memory:

#define N 100      # Example array size
#define STATIC

In the core code, you declare your variable as such:

real PTR_, dimension(NMEM_) :: x

Set STATIC during compilation, and it becomes

real, dimension(100) :: x

Unset STATIC (implicitly setting it to dynamic) and it becomes

real, pointer, dimension(:) :: x

and some later code allocates the memory.

This works perfectly fine, but I like to avoid using preprocessors if I can, and it does feel like a kludge to me. Is there a more standard solution to this problem, or is this how it is normally handled? Is there even much difference between static and dynamic compilations these days (keeping in mind that I will probably want to use as much memory as I can)?

  • 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-16T10:53:57+00:00Added an answer on May 16, 2026 at 10:53 am

    Having both static and dynamic memory modes seems to me an unnecessary complication, making your code harder to read and having two paths, both of which have to be tested. It also creates more complicated instructions for the user, and a burden to follow when they “make” the program. I recommend just using dynamic allocation if the maximum array size isn’t known at compile time. Furthermore, I recommend using allocatable arrays rather than pointers unless you need one of the few features that can only be achieved with pointers, such as arrays with non-unit strides. Allocatables are safer, with memory leaks essentially impossible. Fortran 95 compilers (dump any compiler than is only Fortran 90 at this point) have long supported dynamic memory allocation — don’t have any concerns about using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.