Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What are the key differences between operator new and operator new[]?

Reply
Thread Tools

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

 
 
xmllmx
Guest
Posts: n/a
 
      02-03-2010
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!
 
Reply With Quote
 
 
 
 
Fred Zwarts
Guest
Posts: n/a
 
      02-03-2010
"xmllmx" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> 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.
 
Reply With Quote
 
 
 
 
xmllmx
Guest
Posts: n/a
 
      02-03-2010
On Feb 3, 8:57*pm, "Fred Zwarts" <(E-Mail Removed)> wrote:
> "xmllmx" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)
>
>
>
>
>
> > 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);
 
Reply With Quote
 
Hang Dog
Guest
Posts: n/a
 
      02-03-2010
xmllmx wrote:
> On Feb 3, 8:57 pm, "Fred Zwarts" <(E-Mail Removed)> wrote:
>> "xmllmx" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)
>>
>>
>>
>>
>>
>>> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-03-2010
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
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      02-03-2010
"Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message
news:(E-Mail Removed)
>> 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.

 
Reply With Quote
 
xmllmx
Guest
Posts: n/a
 
      02-03-2010
On Feb 3, 11:48*pm, "Fred Zwarts" <(E-Mail Removed)> wrote:
> "Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message
>
> news:(E-Mail Removed)
>
>
>
>
>
> >> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-14-2009 03:51 PM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-08-2009 04:31 PM
Key Differences Between Ansi C and C++ blangela C++ 11 03-05-2007 08:29 PM
differences between malloc and operator new.. BekTek C++ 22 11-29-2004 08:45 PM



Advertisments