I've adopted Roger's subject change for this thread.

Greg Winton <[EMAIL PROTECTED]> wrote:

>>VoidHand hString = MemHandleNew( MAX_STRING_LENGTH );
>>pString = MemHandleLock( hString );
>>...do something with the string
>>MemHandleUnlock( hString );
>>MemHandleFree;
>>
>
>In this case, the memory is allocated from the dynamic heap.  This too is
>limited, but less so.  Also, here you are giving the OS a chance to tell
>you you're out of room, by returning NULL from MemHandleNew.

Right.  Got it.

>>which is the preferred method?
>I prefer to allocate the memory myself for the reasons mentioned.

I suppose experience comes into play on some choices.

>> Why would I choose one method over the other?
>The first one is way easier to program, less prone to accidental leaking,
>and is clearer in examples which are not trying to show how to allocate
>memory.  The latter is more efficient in terms of stack space and gives the
>OS more leeway in where the memory comes from, and what to do if it's not
>there.

Is the handle only on the stack while locked?  

>If you are only going to use the memory for a short stretch, you could save
>yourself a lock and unlock call:
>
>CharPtr pString = MemPtrNew (MAX_STRING_LENGTH)
>if (NULL != pString)
>{
>   // .. do something with the string
>   MemPtrFree (pString)
>}

Right.  Create a non-movable chunk.  I'm not doing this anywhere, but I
might be able to save a line or two doing this.

>Hope this helps.

Immensely.  I've printed it out and punched it into the API binders.  Thank
you.

Cheers,
--
Andrew Ball
[EMAIL PROTECTED]

Reply via email to