Re: allocate heap? yes or no? [message #39318] |
Mon, 17 May 2004 06:26  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Marc Schellens writes:
Marc pretty much covers all the ground related to pointers.
I was trying to think if I use a rule of thumb about when
and where to allocate heap. Then I realized I *used* to
do a lot of heap allocating, but these days I hardly do
it at all.
I think when I started working with pointers (and especially
when they were associated with objects) I had several
"entry" points in the program where pointers could be
set (INIT, SetProperty methods, etc.) It seemed smart
to allocate heap in the INIT method, then I didn't have
to worry about checking if the pointer was valid in the
SetProperty method.
But I think what happened is that I found it too easy to
leak pointer memory with this approach. These days I
do my "checking" with PTR_VALID instead of N_ELEMENTS
and if the pointer isn't valid, I just make a new one.
This makes the pointer easy to destroy at appropriate
times without worrying about whether I have to also
allocate a new pointer with heap memory. It just makes
the pointer accounting a LOT easier for me if I *don't*
allocate heap.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|