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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic allocation of arrays within subroutines

Status
Not open for further replies.

srikanthnz

Programmer
Oct 3, 2010
1
NZ
Hi all,


I know this question has been asked before and I confess I didn't spend enough time searching for an answer.

Here is what I want to do:

Program Main
--------------
--------------
do i = 1, N
CALL UPDATE
end do
--------------
--------------
End Program Main

Subroutine UPDATE
-----------------
------------------
Allocate(head(mxmy))
--------------
--------------
Allocate(list(NxNy))
--------------
--------------
Deallocate(head,list)
end subroutine update

Where the arrays are declared in a separate module

module link
implicit none
integer, allocatable :: head:)),list:))
end module link

I compile the program with necessary modules and when I try to execute it I get this following error

exe: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

When I traced for the source of error, it looks like the execution is halted near the second allocate statement.

Cheers
Srikanth
 
This looks like a memory corruption. I suggest the use of a tool like valgrind to find where the mistake occurred.

Another cause could be a too large memory request, but I think that the error message would be different in that case. Of course you have to check carefully the values of mxmy and NxNy...

At least, activate all debugging options of your compiler, especially the one which checks array out of bounds.

About your code structure, it seems OK. If it was not (a return before the DEALLOCATE statement for instance), then again, the error message would be different and much more understandable with something like "cannot allocate an array already allocated".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top