Thread: allocating and deallocating

  1. #1
    Registered User
    Join Date
    Oct 2002

    allocating and deallocating


    can sombody please tell me which is safer to use malloc and free functions or to use new and delete operators? and why?
    please set free our POWs

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    new and delete. They are the standard C++ operators and they are type safe compaired to malloc and free. Also with C++ you can delete a null pointer safely. free() has unpredictable results when freeing a null pointer.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Read the standard The free function



    #include <stdlib.h>
    void free(void *ptr);


    [#2] The free function causes the space pointed to by ptr to
    be deallocated, that is, made available for further
    allocation. If ptr is a null pointer, no action occurs.
    Otherwise, if the argument does not match a pointer earlier
    returned by the calloc, malloc, or realloc function, or if
    the space has been deallocated by a call to free or realloc,
    the behavior is undefined.
    The substantial difference is that new/delete call class constructors and destructors, and malloc/free don't
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed