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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:54:18+00:00 2026-05-17T19:54:18+00:00

I am somewhat puzzled by the following program module test implicit none type TestType

  • 0

I am somewhat puzzled by the following program

module test
   implicit none

   type TestType
      integer :: i
   end type

contains
   subroutine foo(test)
      type (TestType), intent(out) :: test
      test%i = 5 
   end subroutine

   subroutine bar(test)
      type (TestType), intent(out) :: test
      test%i = 6 
   end subroutine

end module

program hello
   use test
   type(TestType) :: t

   call foo(t)
   print *, t%i 
   call bar(t)
   print *, t%i 
end program hello

and its derivatives. More on those later. As we know, Fortran transfers routine arguments as a pass-by-reference, meaning that the entity emerging at the dummy argument test for both foo and bar is the same memory space granted on the stack in program hello. So far so good.

Suppose I define in program hello the type(TestType) :: t as a pointer, and allocate it.

program hello
   use test
   type(TestType), pointer :: t

   allocate(t)

   call foo(t)
   print *, t%i
   call bar(t)
   print *, t%i

   deallocate(t)
end program hello

The code works as before, the only difference being that the object was not allocated on the stack, but on the heap.

Now assume to go back to the stack-allocated program and that subroutine bar is instead defined as

 subroutine bar(test)
    type (TestType), pointer :: test
    test%i = 6 
 end subroutine

The program does not compile anymore because you must use the heap-allocated version to make it work, or to be more accurate it is mandatory to pass a pointer to the routine when the routine is defined to accept a pointer as a dummy argument. On the other hand, if the dummy argument does not contain the pointer keyword, the routine would accept both pointers and non-pointers.

This makes me wonder… what’s the point of declaring a dummy argument a pointer ?

  • 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-17T19:54:18+00:00Added an answer on May 17, 2026 at 7:54 pm

    Reposted from comp.lang.fortran, an answer by Tobias Burns:

    Now assume to go back to the stack-allocated program and that
    subroutine bar is instead defined as

    subroutine bar(test)
    type (TestType), pointer :: test
    test%i = 6
    end subroutine

    The program does not compile anymore because you must use the
    heap-allocated version to make it work,

    That’s not quite correct: You can also not pass an ALLOCATABLE variable
    to a dummy with POINTER attribute. I think one (practical) reason is
    that the pointer address can escape and you would thus cause alias
    problems. A formal reason is that an ALLOCATABLE is simply not a
    POINTER; additionally, the standard does not talk about heap vs. stack
    vs. static memory. And in fact, local arrays [with constant bounds] will
    often be created in static memory and not on the stack (unless you use
    OpenMP or the RECURSIVE attribute). Thus, your “stack” example could
    also be a “static memory” example, depending on the compiler and the
    used options.

    or to be more accurate it is
    mandatory to pass a pointer to the routine when the routine is defined
    to accept a pointer as a dummy argument.

    That’s also not completely true. In Fortran 2008 you can pass a
    non-POINTER, which has the TARGET attribute, to a pointer dummy which
    has the INTENT(IN) attribute. (Pointer intent is relative to the pointer
    association status; for non-pointer dummies the intents are about the
    value stored in the variable.)

    This makes me wonder… what’s the point of declaring a dummy argument
    a pointer ?

    Well, if the argument has the POINTER attribute, you can allocate and
    free the pointer target, you can associate the pointer with some target
    etc. Up to Fortran 95 it was not possible to have ALLOCATABLE dummy
    arguments thus a pointer had to be used if a (dummy) argument had to be
    allocated in a procedure.

    If you can, you should try to use rather ALLOCATABLEs than POINTERs –
    they are easier to use, do not leak memory and have pose no
    alias-analysis problems to the compiler. On the other hand, if you want
    to create, e.g., a linked list, you need a pointer. (Though, for a heap
    usage, also Fortran 2008’s allocatable components could be used.*)

    *I mean:
       type t
           type(t), allocatable :: next
       end type
    

    where the component is of the same type as the type being defined;
    before F2008 this was only allowed for pointers but not for allocatables.

    and by R. Maine

    As we know, Fortran transfers routine arguments as a
    pass-by-reference,

    We apparently know incorectly, then. The standard never specifies that
    and, indeed goes quite a lot out of its way to avoid such specification.
    Although yours is a common misconception, it was not strictly accurate
    even in most older compilers, particularly with optimization turned on.
    A strict pass-by-reference would kill many common optimizations.

    With recent standards, pass-by-reference is all but disallowed in some
    cases. The standard doesn’t use those words in its normative text, but
    there are things that would be impractical to implement with
    pass-by-reference.

    When you start getting into things like pointers, the error of assuming
    that everything is pass-by-reference will start making itself more
    evident than before. You’ll have to drop that misconception or many
    things wil confuse you.

    I think other people have answered the rest of the post adequately. Some
    also addressed the above point, but I wanted to emphasize it.

    Hope this answers your question.

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

Sidebar

Related Questions

Somewhat unclear to me are references (pointers?) to classes in VB.NET. The question I
Somewhat related to my question about integers instead of decimals; my vendor provides a
Although somewhat related to this question , I have what I think is a
This is a somewhat low-level question. In x86 assembly there are two SSE instructions:
I have somewhat interesting development situation. The client and deployment server are inside a
Working on a somewhat complex page for configuring customers at work. The setup is
I've been somewhat spoiled using Eclipse and java. I started using vim to do
I asked a somewhat related question but I want it to make it more
I have a somewhat messily-formatted Objective-C code base. Is there a way to have
I know this is somewhat of a server question, but I wanted to ask

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.