Re: referencing structure arrays [message #68004 is a reply to message #68000] |
Thu, 10 September 2009 09:53   |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
niv wrote:
> Sorry, looks like what I typed looks gibberish in places. I hope this
> one is more readable:
>
>
> Hi All,
>
> I have a problem referencing structure arrays and assigning values to
> them. So here is a snippet of my code:
>
> junk1=update_state(junk) ;creates a structure called junk1
> statearr=replicate(junk1,3) ;creates an array of structures
>
> infoptr=ptr_new({statearr:statearr}) ;need this to pass variables
> between programs
>
> align_images,state=state1 ;some program that aligns 2 images and
> stores the alignment results in state1
>
> ;assign values from the state1 structure to infoptr.statearr[0]
> structure
> (*infoptr).statearr[0].resvol_file_ptr=state1.resvol_file_pt r
> (*infoptr).statearr[0].air_file_ptr=state1.air_file_ptr
>
> Now, here is the problem. With the above code, it turns out that...
>
> (*infoptr).statearr[0].resvol_file_ptr, (*infoptr).statearr
> [1].resvol_file_ptr, (*infoptr).statearr[2].resvol_file_ptr
> all have the same values. Similarly with air_file_ptr. Even though I
> haven't assigned anything to the statearr 1 and 2 yet.
>
> Is there a specific way to reference these structure arrays? Any help
> is greatly appreciated!
>
> Thanks
> Niv
Hi Niv,
the "problem" is with replicate (or your structure). If you have a
pointer in it, you copy the pointer N times, so each of the copies still
point to the same memory space... here is an example of what is happening:
IDL> a = {b:ptr_new(indgen(10))}
IDL> c = replicate(a,5)
IDL> help,c[0].b
<Expression> POINTER = <PtrHeapVar2>
IDL> help,c[3].b
<Expression> POINTER = <PtrHeapVar2>
so if you modify one, you modify all!
What you can do is to create your structure with empty pointers,
replicate the structure and set the pointer's value then.
a = {b:ptr_new()}
c = replicate(a,5)
...
c[0].b = state1.resvol_file_ptr
Jean
|
|
|