Heriberto Delgado V�squez wrote:

Hi!

I have an PalmOS C++ application with code like the
following:

//-------------------------------------------------------------

void TPrinterUtil::ReadConfiguration()
{
   TPrinterData& Data = * new TPrinterData(*dbName);

// do something with Data

delete &Data;

}

//-------------------------------------------------------------
I think that actually works, however, normally it is written as follows

TPrinterData *Data = new TPrinterData( *dbName );

        // Now when you access data you must do
        Data->memberVariable vs. Data.memberVariable.

delete Data;


I know that these two lines seem somewhat strange, but the fact is, they compile and run and, apparently, do their work as they should. However, one of my coworkers pointed to me it was easier, and safer, to write that code like this:

//-------------------------------------------------------------

void TPrinterUtil::ReadConfiguration()
{
   TPrinterData Data(*dbName);

// do something with Data

   // Data need not be explicitly destroyed
}

//-------------------------------------------------------------
Writing it this way is better from a maintenance point of view but consumes stack space. In this case it probably does not matter, but in the case of the following

unsigned char buffer[ 2048 ]; // 2048 bytes on the stack.
vs.
unsigned char *buffer = new unsigned char[ 2048 ]; // 2048 bytes from the heap and 4 bytes from the stack.


it makes a very big difference.


At first, I didn't see what could be the difference,
but he
(my coworker) told me my original code might not be
destroying
the memory block pointed to by Data, but something
else, like
system memory or unallocated space. Obviously, that
freaked me
out completely.

I need your opinion on this. Does this code do what my
coworker
says? If it does, I'm afraid I will need to rewrite
most of the
code in my application, and that scares me a lot!

I think your code works, but it will be very confusing for anyone who comes in after you to change the code. IMHO, you should re-write all of your code where you access memory in this fashion.

 - Heriberto Delgado
   ([EMAIL PROTECTED],
    [EMAIL PROTECTED])








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

Reply via email to