×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Problem with dynamic allocation

Problem with dynamic allocation

Problem with dynamic allocation

(OP)

I have the following problem:

- I use an allocatable structure, which is defined inside a module;
- the module is used in the main program, in "subroutine 1" and in "subroutine 2";
- from the main program sub1 is called, and in turn sub 1 calls sub2;
- inside sub2 I have lines like these:

if (allocated(structure)) deallocate (structure)
allocate (structure(N))
do i=1,N
allocate structure(i).elem(k)
enddo

- when control returns to sub2, the content of the structure is messed up (and I get an error)
- actually this works under Solaris (with f95), but I have this problem using Compaq Visual Fortran (6.6) under windows.

Any suggestion? Thank you.

Alessandro

RE: Problem with dynamic allocation

(OP)

(I wanted to say "when control returns to sub1")

RE: Problem with dynamic allocation

You are going to have to post your code, or a minimal non-working code if it is too long.

RE: Problem with dynamic allocation

Please use the CODE and /CODE tags to enclose your code. These CODE words, need to be surrounded with square brackets, just like it would do if you click on the 'bold' button.

RE: Problem with dynamic allocation

(OP)

Ok, my module looks like this:

MODULE TYPEDEFS

type geoms
character*80 name
integer ntot, material
integer, allocatable :: id(:)
end type

type (geoms), allocatable :: geom(:)

END MODULE


As said, this module is used in the main program and in all subroutines.
From main program sub1 is called, and from sub1 sub2 is called in turn.

Inside sub2 the structure geom is allocated and filled:


open (100,file='INPUT\geoms.dat')
read (100,*) ngeoms

if (allocated(geom)) deallocate (geom)
allocate (geom(ngeoms))

do n=1,ngeoms
read (100,'(a)') geom(n).name
read (100,'(a)') geom(n).ntot
call stringscan(string,nf,ival)

allocate (geom(n).id(geom(n).ntot))

read (100,*) (geom(n).id(i),i=1,geom(n).ntot)
enddo


After exiting from sub2 and returning to sub1, the content of geom is lost.
Moreover, if I try to deallocate geoms in sub1, I get the error:

Debug Assertion Failed!

Program: D:\...
File: dbgheap.c
Line: 1044

Expression: _CrtIsValidHeapPointer(pUserData)


Hope now the situation is clearer.

RE: Problem with dynamic allocation

Nope...it is not clearer.

In forums like this one, it is our experience that people who are in trouble can't always be trusted with what they are saying or what they think is going on; after all, if they knew exactly what is going on, they wouldn't be in trouble, in the first place. No offense.

Please use CODE tags.

Please post your entire code or a minimal code that can be compiled (by us) and that reproduces your problem.

RE: Problem with dynamic allocation

Standard syntax for referencing components of the data structure is using the % sign - and not dot .

I tried this little piece of code, but it seems to work without problems:

CODE

MODULE TYPEDEFS

type geoms
character*80 name
integer ntot, material
integer, allocatable :: id(:)
end type

type (geoms), allocatable :: geom(:)

END MODULE

program alec63
  use typedefs
  implicit none
  integer :: i, n, ngeoms
  character(2) :: n_char

  ngeoms = 5

  if (allocated(geom)) deallocate (geom)

  allocate (geom(ngeoms))

  do n=1,ngeoms
    write(n_char, '(i2)') n
    geom(n)%name = 'Name_' //  n_char
    geom(n)%ntot = n

    allocate (geom(n)%id(geom(n)%ntot))

    do i=1, geom(n)%ntot
      geom(n)%id(i) = i
    end do
  enddo

  ! Print results
  do n=1,ngeoms
    write(*,*) 'name = ', geom(n)%name 
    write(*,*) 'ntot = ', geom(n)%ntot
    write(*,*) 'elements of ID(:):'
    do i=1, geom(n)%ntot
      write(*,*) geom(n)%id(i)
    end do
    write(*,*) '*****************************'
  end do  

end program 

Output:

CODE

$ gfortran ale63.f95 -o ale63

$ ale63
 name = Name_ 1                                                                         
 ntot =            1
 elements of ID(:):
           1
 *****************************
 name = Name_ 2                                                                         
 ntot =            2
 elements of ID(:):
           1
           2
 *****************************
 name = Name_ 3                                                                         
 ntot =            3
 elements of ID(:):
           1
           2
           3
 *****************************
 name = Name_ 4                                                                         
 ntot =            4
 elements of ID(:):
           1
           2
           3
           4
 *****************************
 name = Name_ 5                                                                         
 ntot =            5
 elements of ID(:):
           1
           2
           3
           4
           5
 ***************************** 

As salgerman said, if you want to help post a compilable piece of code, which produces this error. And don't forget to give as an example of your file geoms.dat from which you read the data.

Always post the code and data between the tags:
[code] 
and
[/code] 

RE: Problem with dynamic allocation

mikrom: On a side note, how do you manage to post the code tags?

RE: Problem with dynamic allocation

Hi salgerman

I used
[pre][code][/pre]
and
[pre][/code][/pre]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close