[comp.lang.c++] Portable C++ pointer/handle or memory management

chiu@efi.com (Chan Chiu) (10/11/90)

I am going to develope a portable software using C++. The target systems are
Mac, PC/Windows and Unix.

One thing I found out particularly troubling is memory reference/dereference.
Mac and PC/Windows use handle (i.e. moveable heap memory) model. Unix use 
pointer model.

There are two things come to my mind. 
1, Should I have "handle-based" object and overload "->" and "." ?
2, If not, do I then use 
   "handle" model ? (i.e. I have to lock/unlock to convert from/to pointer)

   In general, I like to know your experience developing portable program in the
   above environments.

   /CC

jgro@lia (Jeremy Grodberg) (10/15/90)

In article <1990Oct10.170534.1162@efi.com> chiu@efi.com (Chan Chiu) writes:
>I am going to develope a portable software using C++. The target systems are
>Mac, PC/Windows and Unix.
>
>One thing I found out particularly troubling is memory reference/dereference.
>Mac and PC/Windows use handle (i.e. moveable heap memory) model. Unix use 
>pointer model.
>[How should I handle this?]

If you use MPW C++ on the Macintosh, it will provide for you a base class
called HandleObject, which overloads *, ->, [], etc. as appropriate.  The
interface and implementation of this class are built into the compiler, so
you can't copy them, but you can use its functionality to serve as a model
for the PC version.  

So, in all environments, all your dynamically allocated objects (i.e. objects
allocated with "new") would be descendants of HandleObject.  Then for each
environment, you provide the appropriate implementation of HandleObject.  On
the Mac, it is provided for you by MPW C++.  On Unix, HandleObject is an
empty class, since you want the default language behavior (pointer oriented).
I don't know exactly what you would do for Windows, since I know little about
that environment, but you would either use an existing memory management
class or write your own.

Basically, though, it is a difficult problem, because there are limits to
the handle mechanisms on the Mac and PC, and to the pointer mechanism in
Unix, and they are often not the same limitations, so your code must be
very carefully written to avoid memory problems on all machines (unless
the amount of use of new and delete is very limited in your code).
-- 
Jeremy Grodberg      "I don't feel witty today.  Don't bug me."
jgro@lia.com          

ronb@burklabs (Ron Burk ) (10/20/90)

chiu@efi.com (Chan Chiu) writes:

> I am going to develope a portable software using C++. The target systems are
> Mac, PC/Windows and Unix.
> ...
>    In general, I like to know your experience developing portable program in 
>    above environments.
> 
My experience is:  I gave up.  My goals were the same as yours and I
eventually decided there was no way to make my application clean and still
use both Windows, Mac, and "ordinary" memory management.  While the Mac
has a simple double-indirection scheme, Windows forced you to execute several
instructions to "lock" a piece of memory in order to obtain a pointer.
Recently, however, Microsoft documented the more efficient "segment table"
scheme that they apparently had been reserving to give their own applications
a competitive edge.  That method is also difficult to adapt to C++ objects
in a clean way (are you willing to make your Unix version significantly
slower because it is portable?).

My conclusion:  I decided I'd rather bet that everybody is going to move
to Windows 3.0 with a 386 machine and 2 Meg of memory.  Then they can run
my application, which will assume virtual memory.

Let me know if you have more success than I did.