comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: [Q] structure definition with variable array size
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: [Q] structure definition with variable array size [message #12097 is a reply to message #12085] Thu, 25 June 1998 00:00 Go to previous messageGo to previous message
Martin Schultz is currently offline  Martin Schultz
Messages: 515
Registered: August 1997
Senior Member
dEdmundson@Bigfoot.com wrote:
>
> I wish to create a structure for holding a slice from a 3d volume along
> with domain info, e.g.,
>
> pro slice__define
> tmp = {slice, data:fltarr(100,100), x:fltarr(100), y:fltarr(100), z:0.0}
> end
>
> This definition is fine if the data dimensions are known. However, what
> if the array sizes are only known at runtime when the data file is opened
> and brought into IDL. Is it possible to have structures with arbitrary
> length arraysa la F90, C, or Pascal?
>
> Cheers,
> Darran.


Sure is! There are basically two ways (that I know of) to accomplish
this goal:
(1) since IDL does not tag a variable name with a once and forever
defined type information, you can re-create your (anonymous) structure
whenever you need to change something. E.g.:
a = -1.
stru = { a:a, b:'none' } ; float and string
a = fltarr(100)
stru = { a:a, b:n_elements(a) } ; float array and int
; note that struct.a = a would have failed !

(2) you can use pointers in your structure instead of the actual array:
; initialize dummy structure
stru = { a:ptr_new(), b:-1 }
; create pointer to data array
a = ptr_new(fltarr(100))
stru.a = a
stru.b = n_elements(*a)
help,stru,/stru ; gives you some information
help,*stru.a ; derefences the pointer

; don't forget to free pointer at the end:
if (ptr_valid(stru.a)) then begin
ptr_free,stru.a
stru.b = -1 ; in case you access stru later
endif

; ... or do a garbage collection when you exit your program
remains = ptr_valid(count=count)
if (count gt 0) then ptr_free,remains



Hope this helps,
Martin.


--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA

phone: (617)-496-8318
fax : (617)-495-4551

e-mail: mgs@io.harvard.edu
Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/
------------------------------------------------------------ -------
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: Keyword_Set vs N_Elements Re: Problems with CW_FSLIDER
Next Topic: Re: [Q] IDL 5.1, passing keyword params to subprocedures

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sun Apr 26 15:11:52 PDT 2026

Total time taken to generate the page: 1.83716 seconds