difference between new and operator new

A

Aarti

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,
 
C

contactmayankjain

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,

Hi,

void* p = new (1024) is wrong

In the second line I think you have overloaded the new operator with
the keyword operator.


--
Regards
Mayank Jain
Niksun
9818390836
www.mayankjain.110mb.com
 
N

Neelesh Bodas

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top