Print

Print


Aleksandar Donev writes:
 > > The SAVE attribute is what you want.
 >
 > Not really. The example is trying to emulate the C malloc function--this
 > returns a fresh *new* block of memory each time it is called. With SAVE, the
 > allocatable will not be deallocated automatically, but then the allocatable
 > will keep it's allocation status to the same piece of memory.
 >
 > So really all I need is an array pointer, as this is the only way to avoid the
 > "self-cleaning-up" that F95 would otherwise do.

I'm a little confused...not that this is unprecedented.  It seems like
you are saying both that you don't want the automatic deallocation
and that you also do want it.

If you want it to automatically deallocate, then don't use SAVE.
If you do want it to deallocate, then do use SAVE.  If you
used SAVE (so as to avoid the automatic deallocation) and
you want to now change the allocation, then deallocate the old
one (since that won't have been automatically done) and
allocate a new one.  Yes, you will have to explicitly do the
deallocate in this case - it does have to be done one way or
another, either automatically or explicitly.

Now, if you want to have a new allocation in addition to the old
one, rather than replacing the old one, then yes, you'll have
to use pointers.  Perhaps this is the point that I missed.
This is a fundamental difference between pointers
and allocatables.  The difference is more basic than issues of
save.  Allocatables can *NEVER* have more than one allocation
at a time.  There are two choices - either it is allocated or
it is not.  There is never a possibility of having multiple
simultaneous allocations related to a single variable.

With pointers, you can freely do multiple allocations without
deallocating the old ones.  (This can lead to memory leaks if
you don't provide a way to access the old allocations via some
other pointer).  If that is what you want, then yes, you'll
need a pointer.

C's malloc acts like a pointer in this regard.  Indeed, it does
return a pointer, albeit a C one.  If you are trying to
emulate malloc, then yes, you'll have to use a pointer.

--
Richard Maine                |  Good judgment comes from experience;
[log in to unmask]   |  experience comes from bad judgment.
                             |        -- Mark Twain