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

The Archive Base Latest Questions

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

This is a bit complicated; I’d welcome any comments on how to improve the

  • 0

This is a bit complicated; I’d welcome any comments on how to improve the clarity of the question.

Ok, say I have an array:

real, allocatable :: A(:,:,:)

and I want to allocate it before I use it. Is it possible for the size of third dimension to depend on the size of the second dimension?

E.g.

do i=1,n
allocate(A(3,i,i**2))
end do

Obviously the above doesn’t work. I’d like to end up with an array (or a set of arrays) with the shape(s)

(3,1,1), (3,2,4), (3,3,9), ... (3, n, n^2)

where the size of the third dimension is the square of the size of the second dimension.

My rule for the size of the dependent dimension is a bit more complicated, but if squaring is possible I can do the rest.

Is this possible? If so, how might I implement it in Fortran?

What would shape(A) return? That would be interesting.

My other alternative is to allocate to the maximum size required, and be careful to only use certain elements in calculations, i.e.

allocate(A(3,n,n**2))

Even though I’m not hard up on memory at the moment, I’d like to have good programming practices. This is an interesting problem anyway.

Thank you.

EDIT:

What about having the size of a dimension depend on the value of an element in another dimension?

In the answer below the size of array in both dimensions depended on the index of B. I’d like something along the lines of

type myarray
    real :: coord(3)
    integer,allocatable :: lev(:)
    integer, allocatable :: cell(:)
endtype myarray

type(myarray), allocatable :: data

allocate(data(m))
allocate(data%lev(n))

forall (j=1:n) !simple now, for argument's sake
    lev(j)=j
endforall

! I was thinking of using a FORALL loop here, but the errors returned 
! suggested that the compiler (gfortran) didn't expect IF blocks and ALLOCATE 
! statements in a FORALL block
do i=1,m
    do j=1,n
        allocate(data(i)%cell(lev(j)**2))
    enddo
enddo

You get what I mean? But the program falls over as it tries to allocate already allocated variables, e.g. when i=1 it allocates data(1)%cell(1), and then tries to allocate data(1)%cell(2)…uh oh. What I want is something like:

Each data(i) has an array lev(j) of values, with j running from 1 to n, and for each lev(j) value we have a cell of size lev^2. Note that these cell‘s are unique for each data(i) and each lev, and that the size of that particular cell depends on the corresponding lev value, and possibly the corresponding data(i) too.

Would I have to use a derived type within a derived type?

  • 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-27T18:17:19+00:00Added an answer on May 27, 2026 at 6:17 pm

    Yes, you can use a derived type to accomplish this:

    TYPE array
      REAL,DIMENSION(:,:,:),ALLOCATABLE :: A
    ENDTYPE array
    
    INTEGER :: i
    INTEGER,PARAMETER :: n=10
    
    TYPE(array),DIMENSION(:),ALLOCATABLE :: B
    
    ALLOCATE(B(n))
    
    DO i=1,n
      ALLOCATE(B(i)%A(3,i,i*i))
      WRITE(*,*)SHAPE(B(i)%A)
    ENDDO
    
    END
    

    This approach allows each element of array B to be a multi-dimensional array of a different shape.

    The output of the program is as expected:

            3            1            1
            3            2            4
            3            3            9
            3            4           16
            3            5           25
            3            6           36
            3            7           49
            3            8           64
            3            9           81
            3           10          100
    

    EDIT: To further answer OP’s edited question. Yes, it seems like you would need to do something like this, use nested derived type (compare to your code example to figure out what you did wrong):

    integer,parameter :: m=3,n=5
    
    type cellarray
      integer,dimension(:),allocatable :: c
    endtype cellarray
    
    type myarray
      integer,allocatable :: lev(:)
      type(cellarray),dimension(:),allocatable :: cell
    endtype myarray
    
    type(myarray),dimension(:),allocatable :: B
    
    allocate(B(m))
    
    ! Allocate and assign lev and cell:
    do i=1,m
      allocate(B(i)%lev(n))
      allocate(B(i)%cell(n))
      do j=1,n
        B(i)%lev(j)=j
      enddo
    enddo
    
    ! Based on value of lev, allocate B%cell%c:    
    do i=1,m
      do j=1,n
        allocate(B(i)%cell(j)%c(B(i)%lev(j)**2))
      enddo
    enddo
    
    ! Print out to check that it works:
    do j=1,n
      write(*,*)j,B(1)%lev(j),SIZE(B(1)%cell(j)%c)
    enddo
    
    end
    

    Tried this with gfortran 4.6.2. It produces expected output:

           1           1           1
           2           2           4
           3           3           9
           4           4          16
           5           5          25
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a bit complicated, but bear with me. I have a Windows
[I am using an example in this question, the real problem I have is
This is a bit complicated so bear with me: On FORM.php, I have 2
this is a little bit complicated question. I want to achieve the following behaviour
This question is a bit complicated, as there are alot of moving parts, but
The question is a bit complicated so I will try and explain. I have
This is a bit complicated but here goes. I have a Rails app that
OK, this might sound a bit confusing and complicated, so bear with me. We've
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have this bit of script to widen a text box on mouseover and

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.