473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

difference between new and operator new

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,

Jul 20 '07 #1
3 8999
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,
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

Jul 20 '07 #2
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

Jul 20 '07 #3
On Jul 20, 9:35 am, "contactmayankj...@gmail.com"
<contactmayankj...@gmail.comwrote:
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,

Hi,

void* p = new (1024) is wrong

In the second line I think you have overloaded the new operator with
the keyword operator.
Wrong.
>
Jul 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
47
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
6
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.