Re: NewPtr vs malloc
Re: NewPtr vs malloc
- Subject: Re: NewPtr vs malloc
- From: "Clark S. Cox III" <email@hidden>
- Date: Sun, 9 Dec 2001 11:17:02 -0500
On Sunday, December 9, 2001, at 06:26 , Ondra Cada wrote:
Jr.,
Tomas Zahradnicky, Jr. (TZJ) wrote at Sat, 8 Dec 2001 21:57:21
+0100:
TZJ> Althrough they might seem the same, they are not.
That well might be, but not for the reasons noted below.
TZJ> ptr = NewPtr(size)
TZJ> is mac os toolbox function which allocates free pointer block. You
TZJ> may want to allocate ptr = NewPtrClear(size) --- a cleared pointer
TZJ> block.
If you want to do that the standard way, just use calloc.
TZJ> You can get size of that block GetPtrSize(ptr) or tell user
"malloc_size" of standard API.
No, malloc_size tells you the amount allocated including padding, and
bookkeeping space, *not* the actual space that is available for your use.
GetPtrSize, on the other hand, returns the *logical* size of the pointer.
For example:
some_ptr = malloc( ... );
some_ptr[ malloc_size(some_ptr) - 1 ]; //This is a bad idea,
malloc_size() can be more than what you passed to malloc()
free( some_ptr );
some_ptr = NewPtr( ... );
some_ptr[ GetPtrSize(some_ptr) - 1 ]; //This is a OK, as GetPtrSize()
always returns the exact value you passed to NewPtr()
DisposePtr( some_ptr );
--
Clark S. Cox, III
email@hidden
http://www.whereismyhead.com/clark/