What are the key differences between operator new and operator new[]?

Discussion in 'C++' started by xmllmx, Feb 3, 2010.

  1. xmllmx

    xmllmx Guest

    Dear all,

    As we know, the C++ standard defines at least four special global
    functions as follows:

    1) void* operator new(size_t);

    2) void* operator new[](size_t);

    3) void operator delete(void*);

    4) void operator delete[](void*);

    In Visual C++, 2) and 4) simply forward their respective call to 1)
    and 3). Obviously, 1) and 3) are equivalents respectively to malloc
    and free in C. However, when and why should we call 2) and 4)?

    Though 2) and 4) are not harmful, but I think them rather ugly.
    Because I can not find any necessity of them.

    I hope someone can give me a convincing explanation? Thanks in
    advance!
     
    xmllmx, Feb 3, 2010
    #1
    1. Advertising

  2. xmllmx

    Fred Zwarts Guest

    "xmllmx" <> wrote in message
    news:
    > Dear all,
    >
    > As we know, the C++ standard defines at least four special global
    > functions as follows:
    >
    > 1) void* operator new(size_t);
    >
    > 2) void* operator new[](size_t);
    >
    > 3) void operator delete(void*);
    >
    > 4) void operator delete[](void*);
    >
    > In Visual C++, 2) and 4) simply forward their respective call to 1)
    > and 3). Obviously, 1) and 3) are equivalents respectively to malloc
    > and free in C.
    >
    > However, when and why should we call 2) and 4)?


    Normally, you don't call these operators explicitly, but you use
    new and new [] to create new objects and delete and delete [] to
    destroy objects. In these cases there is much more than just malloc
    and free. In addition to allocating memory, new and new [] call
    constructors to create objects and in addtion to freeing memory,
    delete and delete[] call destructors to destroy objects.
    2) and 4) should be used for arrays. They call the constructors,
    resp. destructors for all array elements, whereas new and delete
    call only one constructor, resp. destructor.

    > Though 2) and 4) are not harmful, but I think them rather ugly.
    > Because I can not find any necessity of them.


    That is probabbly, because you think they only allocate/free memory,
    but they are used for many more functionality.

    > I hope someone can give me a convincing explanation? Thanks in
    > advance!


    I hope this is convincing.
     
    Fred Zwarts, Feb 3, 2010
    #2
    1. Advertising

  3. xmllmx

    xmllmx Guest

    Re: What are the key differences between operator new and operatornew[]?

    On Feb 3, 8:57 pm, "Fred Zwarts" <> wrote:
    > "xmllmx" <> wrote in message
    >
    > news:
    >
    >
    >
    >
    >
    > > Dear all,

    >
    > > As we know, the C++ standard defines at least four special global
    > > functions as follows:

    >
    > > 1) void* operator new(size_t);

    >
    > > 2) void* operator new[](size_t);

    >
    > > 3) void operator delete(void*);

    >
    > > 4) void operator delete[](void*);

    >
    > > In Visual C++, 2) and 4) simply forward their respective call to 1)
    > > and 3). Obviously, 1) and 3) are equivalents respectively to malloc
    > > and free in C.

    >
    > > However, when and why should we call 2) and 4)?

    >
    > Normally, you don't call these operators explicitly, but you use
    > new and new [] to create new objects and delete and delete [] to
    > destroy objects. In these cases there is much more than just malloc
    > and free. In addition to allocating memory, new and new [] call
    > constructors to create objects and in addtion to freeing memory,
    > delete and delete[] call destructors to destroy objects.
    > 2) and 4) should be used for arrays. They call the constructors,
    > resp. destructors for all array elements, whereas new and delete
    > call only one constructor, resp. destructor.
    >
    > > Though 2) and 4) are not harmful, but I think them rather ugly.
    > > Because I can not find any necessity of them.

    >
    > That is probabbly, because you think they only allocate/free memory,
    > but they are used for many more functionality.
    >
    > > I hope someone can give me a convincing explanation? Thanks in
    > > advance!

    >
    > I hope this is convincing.


    Many thanks to your quick response.

    I think you may misunderstand what I mean. I fully know the difference
    between malloc/free and new/delete. I don't know if you know the fact:
    1) operator new(size_t); totally differs from a new expression. Let me
    illustrate that in source code:

    struct CTest
    {
    CTest()
    {
    std::cout << "Call CTest::ctor();" << std::endl;
    }

    ~CTest()
    {
    std::cout << "Call CTest::dtor(); " << std::endl;
    }
    };

    int main()
    {
    CTest* p = new CTest; // 1)
    delete p; // 2)

    void* p2 = operator new(sizeof(CTest)); // 3)
    operator delete(p2); // 4)

    return 0;
    }

    The statements 1) and 2) will implicitly call the constructor and
    destructor of CTest. However, statements 3) and 4) will never
    implicitly call any other member functions of CTest. 3) is totally
    equal to void* p2 = malloc(sizeof(CTest)); and 4) to free(p2);
     
    xmllmx, Feb 3, 2010
    #3
  4. xmllmx

    Hang Dog Guest

    Re: What are the key differences between operator new and operatornew[]?

    xmllmx wrote:
    > On Feb 3, 8:57 pm, "Fred Zwarts" <> wrote:
    >> "xmllmx" <> wrote in message
    >>
    >> news:
    >>
    >>
    >>
    >>
    >>
    >>> Dear all,
    >>> As we know, the C++ standard defines at least four special global
    >>> functions as follows:
    >>> 1) void* operator new(size_t);
    >>> 2) void* operator new[](size_t);
    >>> 3) void operator delete(void*);
    >>> 4) void operator delete[](void*);
    >>> In Visual C++, 2) and 4) simply forward their respective call to 1)
    >>> and 3). Obviously, 1) and 3) are equivalents respectively to malloc
    >>> and free in C.
    >>> However, when and why should we call 2) and 4)?

    >> Normally, you don't call these operators explicitly, but you use
    >> new and new [] to create new objects and delete and delete [] to
    >> destroy objects. In these cases there is much more than just malloc
    >> and free. In addition to allocating memory, new and new [] call
    >> constructors to create objects and in addtion to freeing memory,
    >> delete and delete[] call destructors to destroy objects.
    >> 2) and 4) should be used for arrays. They call the constructors,
    >> resp. destructors for all array elements, whereas new and delete
    >> call only one constructor, resp. destructor.
    >>
    >>> Though 2) and 4) are not harmful, but I think them rather ugly.
    >>> Because I can not find any necessity of them.

    >> That is probabbly, because you think they only allocate/free memory,
    >> but they are used for many more functionality.
    >>
    >>> I hope someone can give me a convincing explanation? Thanks in
    >>> advance!

    >> I hope this is convincing.

    >
    > Many thanks to your quick response.
    >
    > I think you may misunderstand what I mean. I fully know the difference
    > between malloc/free and new/delete. I don't know if you know the fact:
    > 1) operator new(size_t); totally differs from a new expression. Let me
    > illustrate that in source code:
    >
    > struct CTest
    > {
    > CTest()
    > {
    > std::cout << "Call CTest::ctor();" << std::endl;
    > }
    >
    > ~CTest()
    > {
    > std::cout << "Call CTest::dtor(); " << std::endl;
    > }
    > };
    >
    > int main()
    > {
    > CTest* p = new CTest; // 1)
    > delete p; // 2)
    >
    > void* p2 = operator new(sizeof(CTest)); // 3)
    > operator delete(p2); // 4)
    >
    > return 0;
    > }
    >
    > The statements 1) and 2) will implicitly call the constructor and
    > destructor of CTest. However, statements 3) and 4) will never
    > implicitly call any other member functions of CTest. 3) is totally
    > equal to void* p2 = malloc(sizeof(CTest)); and 4) to free(p2);


    ????

    As void doesn't have a ctor or dtor why would one expect the compiler to
    generate any other function to be called when you pass them a void*.
    That of course isn't the case when you are dealing with pointers to
    objects. In that case the compiler knows to call the ctor and dtor.
     
    Hang Dog, Feb 3, 2010
    #4
  5. Re: What are the key differences between operator new and operatornew[]?

    xmllmx wrote:
    > As we know, the C++ standard defines at least four special global
    > functions as follows:
    >
    > 1) void* operator new(size_t);
    >
    > 2) void* operator new[](size_t);
    >
    > 3) void operator delete(void*);
    >
    > 4) void operator delete[](void*);
    >
    > In Visual C++, 2) and 4) simply forward their respective call to 1)
    > and 3). Obviously, 1) and 3) are equivalents respectively to malloc
    > and free in C. However, when and why should we call 2) and 4)?


    *We* should *never* [have to] call them directly. They will be called
    when you allocate an array of objects of some class type (if they are
    overloaded for that class) or delete[] a pointer.

    > Though 2) and 4) are not harmful, but I think them rather ugly.
    > Because I can not find any necessity of them.


    Have you ever tried overloading those? Read about custom allocators and
    class-wide memory management, and you might find separating those useful.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 3, 2010
    #5
  6. xmllmx

    Fred Zwarts Guest

    "Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message
    news:
    >> Normally, you don't call these operators explicitly, but you use
    >> new and new [] to create new objects and delete and delete [] to
    >> destroy objects. In these cases there is much more than just malloc
    >> and free. In addition to allocating memory, new and new [] call
    >> constructors to create objects and in addtion to freeing memory,
    >> delete and delete[] call destructors to destroy objects.
    >> 2) and 4) should be used for arrays. They call the constructors,
    >> resp. destructors for all array elements, whereas new and delete
    >> call only one constructor, resp. destructor.

    >
    > No they do not.
    >
    > You are not distinguishing between a new expression and operator new,
    > The new expression does exactly what you describe above and you
    > cannot alter its behaviour.
    >
    > But the OP is asking about operator new and all that does is aquire
    > raw memory.
    > operator new is called by the new expression to obtain memory.


    I know I was talking about new and new [] expressions.
    It was not clear to me that the OP knew that these expression do more than the corresponding operators.
    As the expressions new and new [] are very different,
    it is reasonable that they also use different operators.
    As explained in another response by someone else,
    this gives the flexibility to overload these two operators in a different way,
    if the application needs a different approach to acquire memory
    in these two diferent cases.
     
    Fred Zwarts, Feb 3, 2010
    #6
  7. xmllmx

    xmllmx Guest

    Re: What are the key differences between operator new and operatornew[]?

    On Feb 3, 11:48 pm, "Fred Zwarts" <> wrote:
    > "Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message
    >
    > news:
    >
    >
    >
    >
    >
    > >> Normally, you don't call these operators explicitly, but you use
    > >> new and new [] to create new objects and delete and delete [] to
    > >> destroy objects. In these cases there is much more than just malloc
    > >> and free. In addition to allocating memory, new and new [] call
    > >> constructors to create objects and in addtion to freeing memory,
    > >> delete and delete[] call destructors to destroy objects.
    > >> 2) and 4) should be used for arrays. They call the constructors,
    > >> resp. destructors for all array elements, whereas new and delete
    > >> call only one constructor, resp. destructor.

    >
    > > No they do not.

    >
    > > You are not distinguishing between a new expression and operator new,
    > > The new expression does exactly what you describe above and you
    > > cannot alter its behaviour.

    >
    > > But the OP is asking about operator new and all that does is aquire
    > > raw memory.
    > > operator new is called by the new expression to obtain memory.

    >
    > I know I was talking about new and new [] expressions.
    > It was not clear to me that the OP knew that these expression do more than the corresponding operators.
    > As the expressions new and new [] are very different,
    > it is reasonable that they also use different operators.
    > As explained in another response by someone else,
    > this gives the flexibility to overload these two operators in a different way,
    > if the application needs a different approach to acquire memory
    > in these two diferent cases.


    Thank you for your this answer. I think this answer is rather
    convincing. Just now, I searched another C++ news group, and got some
    useful story about the ancient history of C++. In graphics driver
    development, it is common to dynamically allocate huge-size array. In
    such cases, it becomes very crucial to handle different allocation/
    deallocation situations in different ways.

    Thank you again, Fred.
     
    xmllmx, Feb 3, 2010
    #7
    1. Advertising

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. BekTek
    Replies:
    22
    Views:
    5,209
    Old Wolf
    Nov 29, 2004
  2. blangela
    Replies:
    11
    Views:
    3,307
    Marcus Kwok
    Mar 5, 2007
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    509
    Home_Job_opportunity
    Jan 8, 2009
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    596
    Home_Job_opportunity
    Jan 14, 2009
  5. M P
    Replies:
    1
    Views:
    500
Loading...

Share This Page