Re: referencing structure arrays [message #68000] |
Fri, 11 September 2009 07:50 |
nivedita.raghunath
Messages: 15 Registered: July 2006
|
Junior Member |
|
|
On Sep 10, 12:53 pm, "Jean H." <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
wrote:
> 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
Worked like a charm! Thank you.
|
|
|
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
|
|
|
Re: referencing structure arrays [message #68005 is a reply to message #68004] |
Thu, 10 September 2009 10:02  |
nivedita.raghunath
Messages: 15 Registered: July 2006
|
Junior Member |
|
|
On Sep 10, 12:36 pm, pp <pp.pente...@gmail.com> wrote:
> On Sep 10, 1:12 pm, niv <nivedita.raghun...@gmail.com> wrote:
>
>
>
>> Sorry, looks like what I typed looks gibberish in places. I hope this
>> one is more readable:
>
>> Hi All,
>
>> I have a problem referencingstructurearrays and assigning values to
>> them. So here is a snippet of my code:
>
>> junk1=update_state(junk) ;creates astructurecalled 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 state1structureto 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 thesestructurearrays? Any help
>> is greatly appreciated!
>
>> Thanks
>> Niv
>
> If I read it right, it sounds like state1.resvol_file_ptr is not a
> scalar, but is an array with 3 elements. Check it with n_elements
> (state1.resvol_file_ptr). Put it another way, I think what is
> happening is the same as
>
> IDL> a=intarr(3)
> IDL> print,a
> 0 0 0
> IDL> b=[1,1,1]
> IDL> a[0]=b
> IDL> print,a
> 1 1 1
>
> Which is that way because if you use an array element on the left side
> of an assignment, and assign to it an array with N elements, then N
> elements are assigned, starting at the one given on the left side.
I checked it and thats not the case. state1.resvol_file_ptr is a
scalar. I even assigned a different value:
IDL> *(*infoptr).statearr[0].resvol_file_ptr='junk'
IDL> print,*(*infoptr).statearr[0].resvol_file_ptr
junk
IDL> print,*(*infoptr).statearr[1].resvol_file_ptr
junk
IDL> print,*(*infoptr).statearr[2].resvol_file_ptr
junk
I dont' get it.
|
|
|
Re: referencing structure arrays [message #68007 is a reply to message #68004] |
Thu, 10 September 2009 09:36  |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
On Sep 10, 1:12 pm, niv <nivedita.raghun...@gmail.com> 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
If I read it right, it sounds like state1.resvol_file_ptr is not a
scalar, but is an array with 3 elements. Check it with n_elements
(state1.resvol_file_ptr). Put it another way, I think what is
happening is the same as
IDL> a=intarr(3)
IDL> print,a
0 0 0
IDL> b=[1,1,1]
IDL> a[0]=b
IDL> print,a
1 1 1
Which is that way because if you use an array element on the left side
of an assignment, and assign to it an array with N elements, then N
elements are assigned, starting at the one given on the left side.
|
|
|
Re: referencing structure arrays [message #68008 is a reply to message #68007] |
Thu, 10 September 2009 09:12  |
nivedita.raghunath
Messages: 15 Registered: July 2006
|
Junior Member |
|
|
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
|
|
|