what is the difference between new and opeartor new.

S

sukhpal

Can any please let me know the difference between new and operator
new. i have written follwoing example in which i have overloaded new
operator.
When i am using "void *p = operator new(1);" it doesn't call the new
overloaded operator. So waht is operator new.When we use it?
#include <iostream>

#include <cstdlib>

#include <new>

using namespace std;

class MyClass {

int x, y;

public:

MyClass() {

x = y = 0;

}

MyClass(int lg, int lt) {

std::cout << "in a constructor";
x = lg;

y = lt;
}

void show() {

cout << x << " ";

cout << y << endl;

}

void *operator new(size_t size);

void operator delete(void *p);

void *operator new[](size_t size);

void operator delete[](void *p);
};

// overloaded new operator
void *MyClass::eek:perator new(size_t size)
{
std::cout << "in a operator new";
void *p;

cout << "In overloaded new.\n";

p = malloc(size);

if(!p) {

bad_alloc ba;

throw ba;
}
return p;
}

// delete operator overloaded
void MyClass::eek:perator delete(void *p)
{

cout << "In overloaded delete.\n";

free(p);
}

// new operator overloaded for arrays.
void *MyClass::eek:perator new[](size_t size)
{
void *p;

cout << "Using overload new[].\n";

p = malloc(size);

if( !p ) {

bad_alloc ba;

throw ba;
}

return p;
}

// delete operator overloaded for arrays.
void MyClass::eek:perator delete[](void *p)
{
cout << "Freeing array using overloaded delete[]\n";

free(p);
}

int main()
{
MyClass *objectPointer1, *objectPointer2;

int i;
void *p = operator new(1);


try {

objectPointer1 = new MyClass (10, 20);

} catch (bad_alloc xa) {

cout << "Allocation error for objectPointer1.\n";

return 1;;
}

try {


objectPointer2 = new MyClass [10]; // allocate an
array

} catch (bad_alloc xa) {

cout << "Allocation error for objectPointer2.\n";

return 1;;
}

objectPointer1->show();

for( i = 0; i < 10; i++)
objectPointer2.show();

delete objectPointer1; // free an object
delete [] objectPointer2; // free an array
int ruk;
std::cin >> ruk;

return 0;
}
 
A

alfchen2008

Can any please let me know the difference between new and operator
new. i have written follwoing example in which i have overloaded new
operator.
When i am using "void *p = operator new(1);" it doesn't call the new
overloaded operator. So waht is operator new.When we use it?
#include <iostream>

#include <cstdlib>

#include <new>

using namespace std;

class MyClass {

  int x, y;

public:

  MyClass() {

     x = y = 0;

  }

  MyClass(int lg, int lt) {

          std::cout <<  "in a constructor";
          x = lg;

    y = lt;
  }

  void show() {

    cout << x << " ";

    cout << y << endl;

  }

  void *operator new(size_t size);

  void operator delete(void *p);

  void *operator new[](size_t size);

  void operator delete[](void *p);

};

// overloaded new operator
void *MyClass::eek:perator new(size_t size)
{
  std::cout <<  "in a operator new";
        void *p;

  cout << "In overloaded new.\n";

  p =  malloc(size);

  if(!p) {

    bad_alloc ba;

    throw ba;
  }
  return p;

}

// delete operator overloaded
void MyClass::eek:perator delete(void *p)
{

  cout << "In overloaded delete.\n";

  free(p);

}

// new operator overloaded for arrays.
void *MyClass::eek:perator new[](size_t size)
{
  void *p;

  cout << "Using overload new[].\n";

  p =  malloc(size);

  if( !p ) {

    bad_alloc ba;

    throw ba;
  }

  return p;

}

// delete operator overloaded for arrays.
void MyClass::eek:perator delete[](void *p)
{
  cout << "Freeing array using overloaded delete[]\n";

  free(p);

}

int main()
{
  MyClass *objectPointer1, *objectPointer2;

  int i;
  void *p = operator new(1);

  try {

    objectPointer1 = new MyClass (10, 20);

  } catch (bad_alloc xa) {

    cout << "Allocation error for objectPointer1.\n";

    return 1;;
  }

  try {

    objectPointer2 = new MyClass [10];               // allocate an
array

  } catch (bad_alloc xa) {

    cout << "Allocation error for objectPointer2.\n";

    return 1;;
  }

  objectPointer1->show();

  for( i = 0; i < 10; i++)
    objectPointer2.show();

  delete objectPointer1;                             // free an object
  delete [] objectPointer2;                          // free an array
  int ruk;
  std::cin >> ruk;

  return 0;

}


When you using "void *p = operator new(1);",the global operator new is
called.As you do not write a global operator new, the standard one is
called.
 
A

Amit

The overloaded Operator new in your example has a limited scope for
the class so "void *p = operator new(1);" is calling the global
operator new.


Can any please let me know the difference between new and operator
new. i have written follwoing example in which i have overloaded new
operator.
When i am using "void *p = operator new(1);" it doesn't call the new
overloaded operator. So waht is operator new.When we use it?
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class MyClass {
  int x, y;

  MyClass() {
     x = y = 0;
  MyClass(int lg, int lt) {
          std::cout <<  "in a constructor";
          x = lg;
    y = lt;
  }
  void show() {
    cout << x << " ";
    cout << y << endl;
  void *operator new(size_t size);
  void operator delete(void *p);
  void *operator new[](size_t size);
  void operator delete[](void *p);

// overloaded new operator
void *MyClass::eek:perator new(size_t size)
{
  std::cout <<  "in a operator new";
        void *p;
  cout << "In overloaded new.\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;

// delete operator overloaded
void MyClass::eek:perator delete(void *p)
{
  cout << "In overloaded delete.\n";
  free(p);

// new operator overloaded for arrays.
void *MyClass::eek:perator new[](size_t size)
{
  void *p;
  cout << "Using overload new[].\n";
  p =  malloc(size);
  if( !p ) {
    bad_alloc ba;
    throw ba;
  }
  return p;

// delete operator overloaded for arrays.
void MyClass::eek:perator delete[](void *p)
{
  cout << "Freeing array using overloaded delete[]\n";
  free(p);

int main()
{
  MyClass *objectPointer1, *objectPointer2;
  int i;
  void *p = operator new(1);
    objectPointer1 = new MyClass (10, 20);
  } catch (bad_alloc xa) {
    cout << "Allocation error for objectPointer1.\n";
    return 1;;
  }
    objectPointer2 = new MyClass [10];               // allocate an
array
  } catch (bad_alloc xa) {
    cout << "Allocation error for objectPointer2.\n";
    return 1;;
  }
  objectPointer1->show();
  for( i = 0; i < 10; i++)
    objectPointer2.show();

  delete objectPointer1;                             // free an object
  delete [] objectPointer2;                          // free an array
  int ruk;
  std::cin >> ruk;
  return 0;

When you using "void *p = operator new(1);",the global operator new is
called.As you do not write a global operator new, the standard one is
called.
 
S

SG

class MyClass {
[...]
  void *operator new(size_t size);
  void operator delete(void *p);
  void *operator new[](size_t size);
  void operator delete[](void *p);
};

These ought to be static members. (!)

Unless you have a very good reason to mess with overloaded operators
new and delete you should avoid it. There are some subtleties to it
and you need to know how to do it correctly. For example: You need to
try allocating memory in a loop if there is a "new_handler" installed.

Cheers!
SG
 
J

James Kanze

Can any please let me know the difference between new and
operator new.

Not an easy question to answer, because the vocabulary is a bit
confusing. Basically, `new', on it's own, is a keyword
representing an operator in the language. As such, it occurs in
two contexts: in an expression, it introduces a new expression,
which calls an allocator function to allocate memory, and then
initializes it. The initialization can be a no-op in certain
degenerate cases, but if the object(s) created have class type,
it calls the constructor. The keyword can, however, appear
after the keyword `operator', in which case, the sequence
`operator new' designates an allocator function, which may be
called (if chosen by overload resolution) by a new expression
(but which can also be called explicitly, e.g.
void* p = operator new( sizeof( T ) ) ;
..)

In order to avoid ambiguity, I avoid the expression `operator
new'. If I'm speaking of a type of expression, I'll just say
'new', or `the new operator'; if I'm speaking of the allocator
function, I'll say so explicitly, e.g. `the operator new
function'.
i have written follwoing example in which i have overloaded
new operator.

Overloaded, or replaced the standard one?
When i am using "void *p = operator new(1);" it doesn't call
the new overloaded operator.

It should use overload resolution to select which `operator new'
function is called. If yours takes a single int, then it should
be selected. If it takes another arithmetic type, the call
should be ambiguous, and otherwise, it should call the standard
one (which can be replaced).
So waht is operator new.When we use it?
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class MyClass
{
int x, y;
public:
MyClass() {
x = y = 0;
}
MyClass(int lg, int lt) {
std::cout << "in a constructor";
x = lg;
y = lt;
}
void show()
{
cout << x << " ";
cout << y << endl;
}
void *operator new(size_t size);
void operator delete(void *p);
void *operator new[](size_t size);
void operator delete[](void *p);

Note that the above are static members of MyClass, and will only
be considered in a new expression if it has type MyClass. They
do not replace the standard operator new (but they can be called
like any static member function, i.e.:

void* p = MyClass::eek:perator new( 1 ) ;

..)
// overloaded new operator
void *MyClass::eek:perator new(size_t size)
{
std::cout << "in a operator new";
void *p;
cout << "In overloaded new.\n";

One additional remark: DON'T do this in a global operator new
which replaces the standard one. The operator<< may itself use
new, in which case, you could easily end up in an endless
recursion.

If you want to instrument the global operator new (and it can
be very useful for debugging), then use a static variable to
protect against recursion, e.g.:

static int recursing = 0 ;
if ( recursing ++ == 0 ) {
std::cout << ... ;
}
-- recursing ;
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete operator overloaded
void MyClass::eek:perator delete(void *p)
{
cout << "In overloaded delete.\n";
free(p);
}

[...]
int main()
{
MyClass *objectPointer1, *objectPointer2;

int i;
void *p = operator new(1);

This should call the global operator new.
try {
objectPointer1 = new MyClass (10, 20);

This should call your operator new.
} catch (bad_alloc xa) {
cout << "Allocation error for objectPointer1.\n";
return 1;;
}
try {
objectPointer2 = new MyClass [10]; // allocate an array

And this should call your operator new[].
} catch (bad_alloc xa) {
cout << "Allocation error for objectPointer2.\n";
return 1;;
}
objectPointer1->show();

for( i = 0; i < 10; i++)
objectPointer2.show();

delete objectPointer1; // free an object
delete [] objectPointer2; // free an array
int ruk;
std::cin >> ruk;
return 0;
}
 
J

James Kanze

class MyClass {
[...]
void *operator new(size_t size);
void operator delete(void *p);
void *operator new[](size_t size);
void operator delete[](void *p);
};
These ought to be static members. (!)

They are. Member allocation and deallocation functions are
always static, even if not declared as such. (Traditionally,
I've never seen them declared static, and wasn't even aware that
it was legal until recently. Logically, however, it's probably
a good idea to declare them static: you want them to be static,
and you know that they are, so why not say so?)
Unless you have a very good reason to mess with overloaded
operators new and delete you should avoid it. There are some
subtleties to it and you need to know how to do it correctly.
For example: You need to try allocating memory in a loop if
there is a "new_handler" installed.

You have to obey the required semantics. More or less; you're
supposed to use the loop, for example, but one reason you want
to provide a class specific allocator is to avoid the installed
new_handler for that class.
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top