Re: Pointers to a variable... [message #77373 is a reply to message #77372] |
Fri, 26 August 2011 06:36   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
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.
> 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.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|