[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Structure field concatenation



David Fanning wrote:
> 
> Martin Schultz (martin.schultz@dkrz.de) writes:
> 
> > Only if you want
> > to replace the data in a structure element, then you need to free
> > the pointer beforehand:
> >
> >    ;; Replace data of first structure
> >    IF Ptr_Valid(periodcube[0].freq) THEN Ptr_Free,
> > periodcube[0].freq
> >    periodcube[0].freq  = Ptr_New( DIndgen(200)*0.1 )
> 
> Actually, as I've been trying to point out for
> months now to no avail, it is NOT necessary to
> free the pointer in this instance. IDL *takes
> care of the memory management for you*. :-)
> 
>     IF Ptr_Valid(periodcube[0].freq) THEN $
>        *periodcube[0].freq  = newThingy
> 
> Cheers,
> 
> David
> 
> P.S. I'm sure Martin knows this. He is just being
> thorough. A trait I have noticed among Germans. :-)

Actually, I didn't know! Well, I've heard this before, but I never
believed it would be that easy. Maybe I am just old-fashioned, but I
always feel like an equilibrist with no safety net if I replace the
contents of a pointer before actually deallocationg the memory it
occupies. And I think I will stick to this habit if only for
compatibility reasons with FORTRAN. Just imagine I would allow our
models to deallocate memory automatically - ain't never gonna happen I
fear...

The second motive for doing it my way is that you will need to have
two statements anyhow. In your example: what happens if the pointer is
not valid? Well, then you need to allocate memory for it, so you
write:

  IF Ptr_Valid(periodcube[0].freq) THEN $
        *periodcube[0].freq  = newThingy  $
  ELSE  $
  
        periodcube[0].freq  = Ptr_New(newthingy)

The only reason to do this that I could accept without further
quirking is if you tell me there is a lot of penalty if you manually
deallocate and reallocate the memory instead of letting IDL do it.
Haven't tested, but I would doubt that it makes a big difference.

Cheers,
Martin


-- 
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[ Dr. Martin Schultz   Max-Planck-Institut fuer Meteorologie    [[
[[                      Bundesstr. 55, 20146 Hamburg             [[
[[                      phone: +49 40 41173-308                  [[
[[                      fax:   +49 40 41173-298                  [[
[[ martin.schultz@dkrz.de                                        [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[