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

Home » Public Forums » archive » losing pointers when concatenating array of structures
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
losing pointers when concatenating array of structures [message #94050] Sun, 08 January 2017 22:35 Go to next message
MarioIncandenza is currently offline  MarioIncandenza
Messages: 231
Registered: February 2005
Senior Member
Hi IDL Wizards,

That topic is a mouthful, but I haven't encountered a tricky problem like this in a while, and I wanted your help to understand what's really going on here. Here's the code sample:

for i=0,1 do begin
undefine,struct
struct=create_struct('array',ptr_new(lindgen(i+1)))
if n_elements(structarray) eq 0 then structarray=struct else structarray=[structarray,struct]
help,*structarray[i].array ; looking for error
; this is where the error will happen
if(i gt 0) then help,*structarray[i-1].array
endfor
end

Here's the result:
% Compiled module: $MAIN$.
<PtrHeapVar23543>
LONG = Array[1]
<PtrHeapVar23544>
LONG = Array[2]
% Invalid pointer: <POINTER (<PtrHeapVar23543>)>.
% Execution halted at: $MAIN$ 9


Here's the fix:
if n_elements(structarray) eq 0 then structarray=temporary(struct) else structarray=[structarray,temporary(struct)]

If you use TEMPORARY(), this works. If you do not, it fails. Can someone help me understand this?

Happy New Year's everyone,

--Edward H.
Re: losing pointers when concatenating array of structures [message #94051 is a reply to message #94050] Mon, 09 January 2017 02:10 Go to previous messageGo to next message
Heinz Stege is currently offline  Heinz Stege
Messages: 189
Registered: January 2003
Senior Member
Well, pointers are tricky sometimes. I will try to explain. The
statement
structarray=struct
in the first loop-path creates a new structure with a new pointer
variable pointing to the _same_ heap variable as struct.array. The
heap variable is _not_ copied.

Undefine is not an IDL routine. I assume you have downloaded this
procedure from the Coyote library. In the second loop path undefine
destroys the struct.array pointer. I.e. it deletes the heap variable
pointed to by struct.array as well as structarray.array.

Your fix with temporary(struct) moves the structure from struct to
structarray. The undefine procedure can't delete the heap variable
anymore, because struct is undefined at that time.

I'm not sure, that I use the right terms in my explanation. I hope, it
gets clear for you yet.

Heinz
Re: losing pointers when concatenating array of structures [message #94052 is a reply to message #94051] Mon, 09 January 2017 08:44 Go to previous messageGo to next message
MarioIncandenza is currently offline  MarioIncandenza
Messages: 231
Registered: February 2005
Senior Member
On Monday, January 9, 2017 at 2:09:58 AM UTC-8, Heinz Stege wrote:
> Well, pointers are tricky sometimes. I will try to explain.

That is a good answer, thank you Heinz. So here's a question: what if I want to make a *copy* of a structure-containing-pointers, creating new heap variables for all the pointers in the structure? Maybe this cannot be done automatically? This is the problem that had me searching the IDL help for a STRUCT_COPY procedure.
Re: losing pointers when concatenating array of structures [message #94053 is a reply to message #94052] Mon, 09 January 2017 11:05 Go to previous messageGo to next message
Jim  Pendleton is currently offline  Jim Pendleton
Messages: 165
Registered: November 2011
Senior Member
On Monday, January 9, 2017 at 9:44:30 AM UTC-7, Edward Hyer wrote:
> On Monday, January 9, 2017 at 2:09:58 AM UTC-8, Heinz Stege wrote:
>> Well, pointers are tricky sometimes. I will try to explain.
>
> That is a good answer, thank you Heinz. So here's a question: what if I want to make a *copy* of a structure-containing-pointers, creating new heap variables for all the pointers in the structure? Maybe this cannot be done automatically? This is the problem that had me searching the IDL help for a STRUCT_COPY procedure.

A quick trick for making a *deep copy* is to SAVE the structure to an output file, then RESTORE the data, making sure you don't accidentally overwrite your original variable when you restore.

All included heap variable references (as well as all their dependencies) will be duplicated.

If you need a procedure that is not as deep, you will need to write your own logic. In ENVI, this is accomplished through dehydrate/rehydrate techniques.

Jim P.
Re: losing pointers when concatenating array of structures [message #94054 is a reply to message #94053] Mon, 09 January 2017 12:33 Go to previous message
Heinz Stege is currently offline  Heinz Stege
Messages: 189
Registered: January 2003
Senior Member
There is a deep_copy function in the JHU/APL IDL Library:
http://fermi.jhuapl.edu/idl/. Seems to me, that this is what Edward is
looking for.

More practicable than saving to a file and restoring it. ;-)

Good luck, Heinz
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Mismatch between video frame dimensions and stream using Coyote graphics
Next Topic: how to overplot a vertical line in the existing plot made using plot function?

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

Current Time: Wed Oct 08 09:13:46 PDT 2025

Total time taken to generate the page: 0.00484 seconds