On Jul 20, 8:50 am, Aarti <aarti.sa...@gmail.comwrote:
Hi,
Why does
void* p = new (1024); gives me a compilation error while
void* p = operator new (1024); works. What is the difference between
new and "operator new"? Please help.
Regards,
when you say new(1024), you are using C++ keyword new, which
represents the new operator. The job of new operator is :
1. to allocate the memory
2. to call the constructor.
The (1)st job is done using C++'s inbuilt 'operator new'. (Bad
overloading of terminologies!). The prototype of "operator new" is
void* operator new (size_t);
Thus, In the first example, the new operator expects the 'type' and
not the size, since it also needs to call the constructor on that
type. Hence the first line gives compilation error. (1024 is not a
type.)
On the second line, you are explicitly calling the 'operator new'. In
other words, what you are doing is only the first step (allocating the
memory). The 'operator new' requires size_t which you have provided
properly (1024), and hence the code compiles properly.
-N