NewPtr
NewPtr
Allocate a nonrelocatable block of memory Size theSize ; desired allocation, in bytes (a 32-bit value) returns address of allocated block; NIL if error
NewPtr allocates a nonrelocatable block of memory at the lowest possible position in the current heap zone.
theSize specifies how much memory you wish to allocate.
Returns: a Ptr; the address of the allocated space or NIL (0) if there wasn't room in the heap zone for the allocation. The MemError function noErr (0) No error
memFullErr (-108) No room in heap
Notes: NewPtr is analogous to the familiar malloc() library function. The returned pointer points directly to the data, which you can be sure will not
be moved around. Use DisposPtr to release the allocation (analogous to the free() function). Use SetPtrSize to shrink or expand the allocation. The Memory Manager attempts to place nonrelocatable blocks at the lowest
possible memory address. It will move relocatable blocks higher in
memory, compact the heap, grow the heap zone, and even discard purgeable
blocks.
Where possible, it is advisable to use NewHandle to obtain memory, rather than NewPtr. Otherwise, the heap will possibly become fragmented, and space allocation will be less efficient.
For allocating an instance of a structure, you will need to coerce the
returned value into the desired data type. You can then access the data via
normal in direction, e.g.:
struct MyStruct *myStructPtr;
myStructPtr = (struct myStruct *)NewPtr( sizeof(MyStruct) ); x = myStructPtr->myField; /* is the same as ... */
x = (* myHandle).myField;