[comp.sys.mac.programmer] Question About NewPtr...

ian@umiami.ir.miami.edu (01/25/91)

This is in regard to the question posted recently about HEAP compaction
and lost pointers....I writing a program in C (Think C) for the Mac and up till
now I was using malloc for dynamic allocation when I saw the posting (and 
spoke with Jason) I checked Inside Mac volume I the info about the memory
manager was interesting all that about relocatable blocks and whatnot...but
the information about NewPtr wasn't very specific...it says that NewPtr returns
a Ptr....I tried replacing all of my mallocs with NewPtr but now I get a
compile error saying that the varible and the Ptr returned by NewPtr aren't
the same...I'm not sure what I'm doing wrong...the varible I'm trying to set
is of type 
SomeRecord    *gAPointer;
could anyone help me out....Thanx 
-- 
Scruffy
Ian Sullivan
*******************************************************************************
**                             %   "Would that love were such a thing        **
**ian@umiami.ir.miami.edu      %    that one could hold, see, or touch       **
**                             %    and perhaps squash the life out of"      **
**UUU    UMUMMM     MMMMMM     %    -Anonymous                               **
**UUU    UMU MMM   MMM MMM     %                                             **
**uUU    UMU  MMM MMM  MMM     %   "Life's been good to me so far..." -J.W.  **
**UUUUUUUUMUof MMMMM   MMM     %   "Give it a while." -Me.                   **
*******************************************************************************

gv9b2c9z@umiami.ir.miami.edu (Ordinary Man) (01/26/91)

In article <1991Jan25.002913.7668@umiami.ir.miami.edu>, ian@umiami.ir.miami.edu writes:
> ...the information about NewPtr wasn't very specific...it says that NewPtr returns
> a Ptr....I tried replacing all of my mallocs with NewPtr but now I get a
> compile error saying that the varible and the Ptr returned by NewPtr aren't
> the same...I'm not sure what I'm doing wrong...the varible I'm trying to set
> is of type 
> SomeRecord    *gAPointer;
> could anyone help me out....Thanx 
> -- 
> Scruffy
> Ian Sullivan

Whenever you allocate memory with the memory manager routine NewPtr, it returns
a generic pointer of type Ptr. If you wish to use this allocation in your
program, and your pointer is of a different type than Ptr, you must typecast,
and use that pointer instead. For example in PASCAL, you would say:

	type
		myData=whateverType;
		aPointer=^myData;

var
	thisPtr:aPointer;

begin
	....
	thisPtr:=aPointer(NewPtr(sizeof(myData)));
	....
Now you can use this pointer anywhere you wish. But remeber that the memory
manager routines expect the generic pointer as a parameter so you must say:

	....
	DisposPtr(Ptr(thisPtr));
	...
when you dispose of your pointer.
Hope this helps,
		==Dan

-- 
/-------------------------------------------------------------------------\
|   Dan Weisman -  University of Miami - Florida   |  ||   ||   ||   ||   |
|--------------------------------------------------|  ||   ||   ||\ /||   |
|   INTERNET  -----> gv9b2c9z@umiami.IR.Miami.edu  |  ||   ||   || | ||   |
|     BITNET  -----> gv9b2c9z@umiami               |  |||||||   || | ||   |
|-------------------------------------------------------------------------|
|      "...bows it's head and prays to the mother of all machines."       |
\_________________________________________________________________________/