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

Re: Structure field concatenation

Amara Graps wrote:
> (at the risk of being the only one here who still hasn't
> figured this out)


Now try again from scratch:

   ;; create template structure and structure array
   template = {orbit:'',freq:ptr_new()}
   periodcube = replicate(template, 20)

   ;; fill first element with data
   periodcube[0].orbit = 'G2'
   periodcube[0].freq  = Ptr_New( DIndgen(100) )

   ;; Work with data
   plot, *(periodcube[0].freq), title=periodcube[0].orbit

   ;; Free all pointers
   Ptr_Free, periodcube.freq

You probably didn't want to use a second Ptr_New statement out of
fear that would allocate extra memory and create a memory leak.
This is no problem, because the Ptr_New() statement with no
argument only "declares" a pointer but does not allocate any
memory for the data it will eventually point to. 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  = Ptr_New( DIndgen(200)*0.1 )

Hope this will clear your mind,

>       "Never fight an inanimate object." - P. J. O'Rourke
No, it's far better to write them ;-)

[[ 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