Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with dynamic allocation

Status
Not open for further replies.

Ale63

Programmer
Nov 20, 2013
3
IT

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
 

(I wanted to say "when control returns to sub1")
 
You are going to have to post your code, or a minimal non-working code if it is too long.
 
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.
 

Ok, my module looks like this:

[tt]MODULE TYPEDEFS

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

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

END MODULE[/tt]


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:


[tt]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[/tt]


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:

[tt]Debug Assertion Failed!

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

Expression: _CrtIsValidHeapPointer(pUserData)[/tt]


Hope now the situation is clearer.
 
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.

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

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 [highlight]post a compilable piece of code, which produces this error[/highlight]. And don't forget to give as an example of your file [highlight]geoms.dat[/highlight] from which you read the data.
[attn]
Always post the code and data between the tags:
[pre]
Code:
[/pre] and [pre]
[/pre]
[/attn]
 
mikrom: On a side note, how do you manage to post the code tags?
 
Hi salgerman

I used
[pre]
Code:
[/pre] 
and 
[pre]
[/pre]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top