NewPtr vs malloc

  • Hi all,

    I'm wondering if there are differences between NewPtr and malloc,
    DisposePtr and free. Which ones are better? or just same?

    Thanks.
    Joe.
  • On Thursday, December 6, 2001, at 10:12  AM, Joe Chang (l%l,m8) wrote:

    > I'm wondering if there are differences between NewPtr and malloc,
    > DisposePtr and free. Which ones are better? or just same?
    >

    There's not much difference. NewPtr simply calls malloc, and DisposePtr
    calls free.

    -eric
  • On Thursday, December 6, 2001, at 10:12 am, Joe Chang (l%l,m8) wrote:

    > Hi all,
    >
    > I'm wondering if there are differences between NewPtr and malloc,
    > DisposePtr and free. Which ones are better? or just same?
    >

    They are both the same, i would use malloc vs NewPtr, since its more
    standard (exact same code will work everywhere)

    vince
  • > On Thursday, December 6, 2001, at 10:12 am, Joe Chang (l%l,m8) wrote:
    >
    >> Hi all,
    >>
    >> I'm wondering if there are differences between NewPtr and malloc,
    >> DisposePtr and free. Which ones are better? or just same?
    >>
    >
    > They are both the same, i would use malloc vs NewPtr, since its more
    > standard (exact same code will work everywhere)

    Hello,

    Althrough they might seem the same, they are not. ptr = NewPtr(size)
    is mac os toolbox function which allocates free pointer block. You
    may want to allocate ptr = NewPtrClear(size) --- a cleared pointer
    block. You can get size of that block GetPtrSize(ptr) or tell user
    why the allocation failed err = MemError() right after a call to
    NewPtr. You may also resize the block using SetPtrSize and MemError
    afterwards.

    -Tomas
    --
    # Tomas Zahradnicky, Jr
    # The Czech Technical University
    # FEL-CTU, Prague
  • On Saturday, December 8, 2001, at 03:57  PM, Tomas Zahradnicky, Jr.
    wrote:

    >> On Thursday, December 6, 2001, at 10:12 am, Joe Chang (l%l,m8)
    >> wrote:
    >>
    >>> Hi all,
    >>>
    >>> I'm wondering if there are differences between NewPtr and malloc,
    >>> DisposePtr and free. Which ones are better? or just same?
    >>>
    >>
    >> They are both the same, i would use malloc vs NewPtr, since its more
    >> standard (exact same code will work everywhere)
    >
    > Althrough they might seem the same, they are not. ptr = NewPtr(size) is
    > mac os toolbox function which allocates free pointer block. You may
    > want to allocate ptr = NewPtrClear(size) --- a cleared pointer block.
    > You can get size of that block GetPtrSize(ptr) or tell user why the
    > allocation failed err = MemError() right after a call to NewPtr. You
    > may also resize the block using SetPtrSize and MemError afterwards.

    However, all these have equivalents.
    NewPtr = malloc
    NewPtrClear = calloc
    GetPtrSize = malloc_size
    SetPtrSize = realloc
    and MemError() is not needed as badly on a system with good virtual
    memory.  If the pointer is nil, there is simply no memory to be had.
    As far as actual implementation details, NewPtr currently calls malloc
    and DisposePtr calls free and I would assume the other functions did the
    same.

    Brendan Younger
  • > However, all these have equivalents.
    > NewPtr = malloc
    > NewPtrClear = calloc
    > GetPtrSize = malloc_size
    > SetPtrSize = realloc
    > and MemError() is not needed as badly on a system with good virtual
    > memory.  If the pointer is nil, there is simply no memory to be had.

    Sure, you may get memerror if your heap gets corrupted for example or
    if you pass bad handle into a system function.

    > As far as actual implementation details, NewPtr currently calls
    > malloc and DisposePtr calls free and I would assume the other
    > functions did the same.

    Sure, you can use whichever you like. If you are used to use malloc,
    you may do so and the same for NewPtr :-)

    -Tomas
    --
    # Tomas Zahradnicky, Jr
    # The Czech Technical University
    # FEL-CTU, Prague
  • 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.

    TZJ> why the allocation failed err = MemError() right after a call to
    TZJ> NewPtr.

    "malloc_error" does more or less the same, but by a more convenient way (you
    install your own "alloc error handler").

    TZJ> You may also resize the block using SetPtrSize and MemError
    TZJ> afterwards.

    "realloc" would be, I guess, what you are after.

    Well, I actually _DON'T KNOW_, but I would bet those NewPtr et al functions
    are just a compatibility layer available so as old Classic code can be
    compiled "as is", and I guess the implementation would be a very trivial
    "void *NewPtr(size_t size) {return malloc(size);}", etc.

    Anyway, for a new code, the standard malloc family is the best one, since it
    is highly portable.
    ---
    Ondra Cada
    OCSoftware:    <ocs...>              http://www.ocs.cz
    2K Development: <o.cada...>  http://www.2kdevelopment.cz
    private        <ondra...>            http://www.ocs.cz/oc
  • On Sunday, December 9, 2001, at 06:26 AM, Ondra Cada wrote:

    > TZJ> You can get size of that block GetPtrSize(ptr) or tell user
    >
    > "malloc_size" of standard API.

    They do not do the same thing.

    The documentation for GetPtrSize says "The GetPtrSize function returns
    the logical size, in bytes, of the nonrelocatable block pointed to by p.
    In case of an error, GetPtrSize returns 0."

    The documentation for malloc_size says

            Malloc_size  returns  the  size of the block pointed to by
            the argument. The size is always at  least  the  requested
            size, and is often a few words larger.

    Jim
  • 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
    <clarkcox3...>
    http://www.whereismyhead.com/clark/
previous month december 2001 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today