Software Archive
Read-only legacy content
17061 Discussions

Pointers & Arrays, memory allocation/leaks

marcellocattaneo
Beginner
618 Views
I'm writing a tool that will help me find memory leaks.

After each allocate and deallocate statement I will paste a piece of code that uses LOC and SIZEOF to determine the address and size of the allocated block, and save info to a file.

I then sort the file and check for mismatches.

Am I correct to assume that this will work only for allocation of POINTERS and does not work with ALLOCATABLE arrays ?

After the first tests, it appears that it works ok for POINTER variables, but that allocatable arrays change address during execution.
Is this correct?

Why?

By the way, I'll be glad to share the tool with anyone interested after it's ready.

Thanks for any indications.

Marcello Cattaneo Adorno
0 Kudos
4 Replies
Steven_L_Intel1
Employee
618 Views
I know of no reason why these should be different. One an ALLOCATABLE array is allocated, it stays at the same address. It could be that the LOC intrinsic is returning the wrong thing.

Steve
0 Kudos
marcellocattaneo
Beginner
618 Views
Thanks for the answer. I'll work on it some more and get back to you.
I actually did two tests: the first on a simple linked list module, and it worked fine ( bust I had no allocatable arrays, only pointers).
Then I tried the utility on one module of a large simulation program that I'm writing, (with many allocatable arrays) and it failed to match LOC of allocation and deallocation in all cases!

If you confirm that ALLOCATABLE arrays behave like pointers in terms of staying in one place, then I'll have to find out what else vcan be happening.

regards, Marcello
0 Kudos
Jugoslav_Dujic
Valued Contributor II
618 Views
There is a significant difference: In F95 (not F90), local ALLOCATABLE arrays (unless explicit SAVE is specified) are AUTOMATIC by default, i.e. they got deallocated when they exit the scope regardless of whether DEALLOCATE is called or not. Of course, when routine is reentered, address of such array will probably be different.

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
618 Views
True - once deallocated, all bets are off. But while allocated, the address doesn't change.

Steve
0 Kudos
Reply