Re: pointer question [message #24276] |
Mon, 26 March 2001 01:05  |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Ted Graves wrote:
>
> Hi all,
>
> Another lurker question ... let's say you define a pointer using the PTR_NEW
> function and assign to a variable x. As long as you keep track of x and don't
> reassign x and lose the pointer to the heap variable, things are great. You
> can remove the heap variable from memory using the PTR_FREE procedure.
>
> But now let's say i have a function TEST that takes a pointer as an argument,
> and i want to create a pointer on the fly to use in TEST. So i do something
> like
>
> result = TEST(PTR_NEW(value))
>
> where value is whatever i want the heap variable to be. What happens to the
> heap variable assigned in this statement after TEST returns? I'm assuming
> from that because of the way it was created, a heap variable now exists that i
> can't easily get rid of without using HEAP_GC.
>
> Me and my sloppy programming ...
>
> Ted Graves
> Magnetic Resonance Science Center, UCSF
Let me second Paul here. WHY? This is the real question here. As I
relearned only recently, IDL automatically passes variables by
reference unless you index them (or do whatever other weird things to
them). So from a program efficiency standpoint, you are passing a
pointer when you simply write
result = test(value)
Now if you really want to give test only the data of the first column
(or row or whatever), you really should, as Paul suggests, create and
destroy the pointer in the caller routine, i.e.:
x = Ptr_New(value[0,*])
result = test(x)
Ptr_Free, x
But even then: you don't even need a pointer here! The following is
exactly the same in terms of efficiency, and it doesn't require a
cleanup (if you write it inside a procedure or function).
x = value[0,*]
result = test(x)
So, why use pointers at all, the witty lurker might ask now? Let me
dare to say that you only need them within structures (or objects for
that purpose), i.e. if you need to access a variable at a certain
place but you don't know it's shape or size beforehand.
Cheers,
Martin
--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie [[
[[ Bundesstr. 55, 20146 Hamburg [[
[[ phone: +49 40 41173-308 [[
[[ fax: +49 40 41173-298 [[
[[ martin.schultz@dkrz.de [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
|
|
|
|
|
|
Re: pointer question [message #24307 is a reply to message #24287] |
Thu, 22 March 2001 20:15   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
John-David Smith <jdsmith@astro.cornell.edu> writes:
> On an only slightly related note, does everyone know that you can recover a
> pointer to a "lost" heap variable using ptr_valid? Here's an example:
>
...
> IDL> a=ptr_valid(4,/CAST)
> IDL> print,*a
> 1
So, is there a way to go the *other* direction? Which is to say, if
you have a pointer, can you get its index number? [ not using HELP of
course. ]
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
|
|
Re: pointer question [message #24314 is a reply to message #24313] |
Thu, 22 March 2001 08:56   |
John-David T. Smith
Messages: 384 Registered: January 2000
|
Senior Member |
|
|
Ted Graves wrote:
>
> Hi all,
>
> Another lurker question ... let's say you define a pointer using the PTR_NEW
> function and assign to a variable x. As long as you keep track of x and don't
> reassign x and lose the pointer to the heap variable, things are great. You
> can remove the heap variable from memory using the PTR_FREE procedure.
>
> But now let's say i have a function TEST that takes a pointer as an argument,
> and i want to create a pointer on the fly to use in TEST. So i do something
> like
>
> result = TEST(PTR_NEW(value))
>
> where value is whatever i want the heap variable to be. What happens to the
> heap variable assigned in this statement after TEST returns? I'm assuming
> from that because of the way it was created, a heap variable now exists that i
> can't easily get rid of without using HEAP_GC.
>
> Me and my sloppy programming ...
On an only slightly related note, does everyone know that you can recover a
pointer to a "lost" heap variable using ptr_valid? Here's an example:
IDL> a=ptr_new(1)
IDL> print,a
<PtrHeapVar4>
IDL> a='oh no, I overwrote my pointer variable'
IDL> help,/heap_variables
Heap Variables:
# Pointer: 1
# Object : 0
<PtrHeapVar4> INT = 1
IDL> a=ptr_valid(4,/CAST)
IDL> print,*a
1
You can also get a vector of pointers to every heap variable using:
IDL> pvec=ptr_valid()
While this isn't exactly useful programatically, it may get you out of a pinch.
JD
|
|
|
Re: pointer question [message #24315 is a reply to message #24314] |
Thu, 22 March 2001 08:24   |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
Mark Hadfield wrote:
> pro test, a
>
> ; Do something with a
>
> if not arg_present(a) then if ptr_valid(a) then ptr_free, a
>
> end
I am sorry, I have not had my first cup of coffe yet. How is that
supposed to work? If there is *no* argument present, *then* try to check
if the missing argument is a pointer? What am I missing? Should it be
if arg_present(a) then if ptr_valid(a) then ptr_free, a ;?
Also, in the example provided by Ted, the parameter is not going to be
recognized by Arg_present, because it is not passed by reference since
it is an expression.
But I think Paul answered already how to do this sort of thing properly.
Cheers,
Pavel
|
|
|
|
Re: pointer question [message #24319 is a reply to message #24316] |
Thu, 22 March 2001 06:01   |
Jaco van Gorkom
Messages: 97 Registered: November 2000
|
Member |
|
|
Mark Hadfield wrote:
>
> "Ted Graves" <egraves@socrates.Berkeley.EDU> wrote in message
> news:99blck$ko7$1@agate.berkeley.edu...
...
>> result = TEST(PTR_NEW(value))
>>
>> where value is whatever i want the heap variable to be. What happens toth e
>> heap variable assigned in this statement after TEST returns? I'm assuming
>> from that because of the way it was created, a heap variable now exists that i
>> can't easily get rid of without using HEAP_GC.
>
> Yes.
>
> But if you have access to the code of TEST you could do this:
>
> pro test, a
>
> ; Do something with a
>
> if not arg_present(a) then if ptr_valid(a) then ptr_free, a
>
> end
Very nice! However, what if you pass in 'a' by value, e.g., from an
array of pointers?
If I call test like
for i=0, n_elements(PointerArray)-1 do test(PointerArray[i])
then I lose all the heap variables, right?
I would prefer to avoid the problem altogether by making TEST accept
both pointers and values, something like:
pro test, a
if size(a, /type) ne 10 then begin
a = ptr_new(a, /no_copy)
a2ptr = 1
endif else a2ptr = 0
; Do something with a, pointer-based.
if a2ptr and ptr_valid(a) then begin
a_copy = a
a = temporary(*a)
ptr_free, a_copy
endif
end
If TEST is not your own code, this could easily be done in a wrapper
routine as well. The flexibility of not having to bother about
pointers-or-not is great for command-line use. But then again, using
heap_gc on the command line every once in a while is not a big problem
either...
Jaco
----------------
Jaco van Gorkom gorkom@rijnh.nl
FOM-Instituut voor Plasmafysica "Rijnhuizen", The Netherlands
|
|
|
|
Re: pointer question [message #24353 is a reply to message #24276] |
Mon, 26 March 2001 17:06  |
egraves
Messages: 9 Registered: January 2000
|
Junior Member |
|
|
Thanks to all who chimed in on my pointer question. The
background of me posing this dilemma was that i was under the
assumption that IDL passed variables by value. Since i am
dealing with a number of rather large data structures, i thought
it would be wise to pass pointers to these structures in
function calls instead of passing the structures themselves. And
naturally after coding for a while, i found situations where i
wanted to execute a function using a hybrid of several data
structures, and instead of creating one explicitly, i hastily
concocted one, used it to define a pointer with PTR_NEW, and
directly passed the result of this to my function.
I have gone back and corrected this naughty practice. Thanks
everyone!
Ted Graves
Magnetic Resonance Science Center, UCSF
|
|
|
Re: pointer question [message #24374 is a reply to message #24285] |
Mon, 26 March 2001 04:56  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
"Mark Hadfield" <m.hadfield@niwa.cri.nz> writes:
> "Craig Markwardt" <craigmnet@cow.physics.wisc.edu> wrote in message
> news:onelvpxgqt.fsf@cow.physics.wisc.edu...
>>
>> So, is there a way to go the *other* direction? Which is to say, if
>> you have a pointer, can you get its index number? [ not using HELP of
>> course. ]
>
> Err... parse the output of string(myptrvar, /PRINT)
Hey, that's kind of neat! (in an IDL-kludgey-sort-of-way)
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|