On Fri, 8 Aug 2003 11:32:23 +1000, "Alan Ingleby" <[EMAIL PROTECTED]> wrote:

>
>"bullshark" <[EMAIL PROTECTED]> wrote in message
>news:[EMAIL PROTECTED]
>> Yes I am. Deallocating is:
>>
>> if(pListTxt != NULL) //pretty hard
>{
>>    memPtrFree(pListText); //huh?
>      pListText=NULL; //??
>}
>Sounds like you're using C++, 

No, I'm not. It's straight C.

Here is some psuedo-code. It's been years since I even compiled
my list builder code. It's in CVS someplace and I don't feel like
getting it out, but I did compile this. It gobbles down the 
INCREDIBLE sum of 208 bytes of code space.

char** buildList(int listWidth,int cnt){
    int amt=cnt*(sizeof(char*)+(listWidth+1));
    int i;
    char **theList=(char**)MemPtrNew(amt);
    char *dest=(char*)(&theList[cnt]);

    //If you want to be stingy:
    // WARNING: *Grossly* complicates the deallocation to TWO
    // statements:
    //    MemPtrFree(theList[0]);
    //    MemPtrFree(theList);
    //char **theList=(char**)MemPtrNew(cnt*sizeof(char**));
    //char *dest=(char*)MemPtrNew(cnt*listWidth+1);
    //UInt32 memUsed=0;
    
    for(i=0;i<cnt;i++){
        int itemLen;
        char *src=GetAListItem(i);        //this is simplified for brevity
        StrNCopy(dest,src,listWidth);
        theList[i]=dest;
        itemLen=StrLen(dest)+1;
        dest+=itemLen;
        //if you want to be stingy:
        //memUsed+=itemLen
    }
   // If you want to be stingy:
   // MemPtrResize(theList[0],memUsed);

    return theList;

}
If you want to get really ooky for no particular reason,
you can use handles and lock them.

>and also violates the coding practice of trying to unallocate
>memory from within the same function in which it was allocated.  

You evidently have no experience with 'Lazy evaluation'.
Never heard of your 'coding practice'.

The fact is, if the global is not null, there is memory allocated to the list.
You need to de-allocate before allocating again. If you like, I'll
move the clause to a separate function, OK?

Your '??' on the setting of pListText to NULL makes me think
you don't get it;

Leaving the de-allocation of the prior list to the list builder
function is what makes memory handling simple. Any time you build
a list, it cleans itself.

>I'm not
>saying either way is wrong... Just offering reasons why some people choose
>different methods.

What you *are* saying is that you don't have much experience with
allocating/deallocating memory for dynamic lists. 


bullshark

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to