Freeing memory
- From: simon@xxxxxxxxxxxxxx
- Date: 4 Feb 2006 06:02:38 -0800
I've written a small example of a linked-list. This works fine as a
linked list but according to valgrind (linux) there is a memory leak.
Given that I only have a handle on the start of the list what is the
best was to properly deallocate memory?
Thanks,
Simon Geard
program example7
type l_entry
integer :: value = -1
type(l_entry), pointer :: next
end type l_entry
type(l_entry), pointer :: first, current
integer :: i
! Fill the list with odd numbers 1...9
allocate(current)
first => current
do i=1,9,2
current = l_entry(i,null())
allocate(current%next)
current => current%next
end do
! Step thought the list outputting as we go
current => first
do
if (.not. associated(current%next)) exit
print *,current%value
current => current%next
end do
deallocate(first)
end program example7
valgrind summary:
==16147==
==16147== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 19 from
2)
==16147== malloc/free: in use at exit: 40 bytes in 5 blocks.
==16147== malloc/free: 13 allocs, 8 frees, 5318 bytes allocated.
==16147== For counts of detected errors, rerun with: -v
==16147== searching for pointers to 5 not-freed blocks.
==16147== checked 118792 bytes.
==16147==
==16147== LEAK SUMMARY:
==16147== definitely lost: 32 bytes in 4 blocks.
==16147== possibly lost: 0 bytes in 0 blocks.
==16147== still reachable: 8 bytes in 1 blocks.
==16147== suppressed: 0 bytes in 0 blocks.
.
- Follow-Ups:
- Re: Freeing memory
- From: James Van Buskirk
- Re: Freeing memory
- From: robert . corbett
- Re: Freeing memory
- Prev by Date: Re: Pointers to procedures possible in g95?
- Next by Date: Re: Pointers to procedures possible in g95?
- Previous by thread: 2 formatting questions
- Next by thread: Re: Freeing memory
- Index(es):
Relevant Pages
|