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

Home » Public Forums » archive » Re: Pointers to a variable...
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: Pointers to a variable... [message #77372 is a reply to message #77371] Fri, 26 August 2011 06:44 Go to previous messageGo to previous message
H. Evans is currently offline  H. Evans
Messages: 18
Registered: December 2009
Junior Member
On Aug 26, 3:36 pm, David Fanning <n...@idlcoyote.com> wrote:
> H. Evans writes:
>> In other less friendly languages, e.g. C, the pointer points to an
>> area of memory, which can coincide with a variable. This gives two
>> methods to access the contents of the variable:
>
>> #include <stdio.h>
>> main() {
>>    int a=5;
>>    int *p;
>
>>    p = &a;
>>    printf("a=%i, *p=%i\n", a, *p);
>>    a= 10;
>>    printf("a=%i, *p=%i\n", a, *p);
>> }
>
>> outputs:
>> a=5, *p=5
>> a=10, *p=10
>
>> So, now that IDL has pointers...can a pointer be set to point to a
>> variable in the same way, i.e. to reference exactly the same memory
>> space as the variable?
>
> No, IDL pointers are NOT like C pointers.
>
>> From the examples, I am under the impression that these pointers don't
>> quite work in the same way, i.e. the pointers don't point to the same
>> memory space as the variables.
>
> This is correct.

I suspected as much. Oh well. off to code the scope_varfetch method...

>
>> The reason I ask is that there are some very large variables that I'd
>> rather not duplicate (waste of memory), but would like to group
>> serially via a pointer array.
>
>> As a trivial example:
>>     a = FINDGEN(10000000L)
>>     b = DINDGEN(200000L)
>>     c = REPLICATE( !P, 10000L)
>>     p = PTRARR(3, /ALLOC)
>>     *p[0] = a
>>     *p[1] = b
>>     *p[2] = c
>
>>    for i=0,n_ELEMENTS(p)-1 DO print,N_ELEMENTS(*p[i])
>
>> Is the only solution to create a,b, and c as heap variables in the
>> first instance and then point p[i] to the heap variable?
>
> IDL pointer variables are *exactly* like any other IDL
> variable:
>
>   http://www.idlcoyote.com/misc_tips/pointers.html
>
> To transfer without duplicating, you could do this:
>
>      a = FINDGEN(10000000L)
>      b = DINDGEN(200000L)
>      c = REPLICATE( !P, 10000L)
>      p = PTRARR(3, /ALLOC)
>      *p[0] = Temporary(a)
>      *p[1] = Temporary(b)
>      *p[2] = Temporary(c)
>
> This will undefine the variables a, b, and c in your program.

Unfortunately, I want to preserve them as they are used later in the
code.

For now, I'll just use

vars = ['a','b','c']
for i=0,n_elements(vars)-1 do
print,n_elements(scope_varfetch(vars[i]))

Oh well...

Ta.
Hugh
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Pointers to a variable...
Next Topic: Re: ImageMagick and Landscape Encapsulated PostScript Files

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

Current Time: Fri Oct 10 16:36:17 PDT 2025

Total time taken to generate the page: 0.00650 seconds