allocating and deallocating

This is a discussion on allocating and deallocating within the C++ Programming forums, part of the General Programming Boards category; hi can sombody please tell me which is safer to use malloc and free functions or to use new and ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    allocating and deallocating

    hi

    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
    Posts
    219
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Read the standard
    7.20.3.2 The free function

    Synopsis

    [#1]

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

    Description

    [#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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21