new vs. malloc

Discussion in 'C++' started by Asapi, Dec 16, 2003.

  1. Asapi

    Asapi Guest

    Compared to malloc(), why would one say that the 'new' operator is type-safe
    in a book? Thanks!
     
    Asapi, Dec 16, 2003
    #1
    1. Advertisements

  2. Asapi wrote:
    > Compared to malloc(), why would one say that the 'new' operator is type-safe
    > in a book? Thanks!


    1. malloc() returns void*, but new returns the proper type.

    2. malloc'd objects must be manually {con,de}structed.
     
    Jeffrey Schwab, Dec 16, 2003
    #2
    1. Advertisements

  3. Asapi

    tom_usenet Guest

    On Tue, 16 Dec 2003 18:50:34 GMT, "Asapi" <> wrote:

    >Compared to malloc(), why would one say that the 'new' operator is type-safe
    >in a book? Thanks!


    malloc returns void*, new returns a pointer of the type of the object
    created. e.g.

    int* ip = malloc(sizeof(short));
    //whoops! no compile time error, but runtime one

    vs
    int* ip = new short;
    //compile time error, short* not convertible to int*

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Dec 16, 2003
    #3
  4. Asapi

    Adam Fineman Guest

    Asapi wrote:
    > Compared to malloc(), why would one say that the 'new' operator is type-safe
    > in a book? Thanks!
    >
    >

    "Type-safe" loosely means that if you use the wrong type, the compiler
    can tell you about it. Look at the following examples:

    int* ip1 = static_cast<int*>(malloc(10 * sizeof(char)));

    int* ip2 = static_cast<int*>(new char[10]);

    The second line will fail to compile, whereas the first one is just fine
    (at least according to the compiler).

    - Adam

    --
    Reverse domain name to reply.
     
    Adam Fineman, Dec 16, 2003
    #4
  5. Asapi

    tom_usenet Guest

    On Tue, 16 Dec 2003 19:05:13 +0000, tom_usenet
    <> wrote:

    >On Tue, 16 Dec 2003 18:50:34 GMT, "Asapi" <> wrote:
    >
    >>Compared to malloc(), why would one say that the 'new' operator is type-safe
    >>in a book? Thanks!

    >
    >malloc returns void*, new returns a pointer of the type of the object
    >created. e.g.
    >
    >int* ip = malloc(sizeof(short));
    >//whoops! no compile time error, but runtime one


    Note that the above is C code (where malloc is idiomatic), the C++
    would be:

    int* ip = static_cast<int*>(malloc(sizeof(short)));
    //whoops! no compile time error, but runtime one

    which is even more painful to write.

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Dec 16, 2003
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    777
  2. ravi
    Replies:
    0
    Views:
    509
  3. Peter
    Replies:
    34
    Views:
    2,158
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    543
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    382
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page