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

X

xmllmx

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!
 
F

Fred Zwarts

xmllmx said:
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.
 
X

xmllmx

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);
 
H

Hang Dog

xmllmx said:
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.
 
V

Victor Bazarov

xmllmx said:
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
 
F

Fred Zwarts

Stephen Howe said:
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.
 
X

xmllmx

"Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message





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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top