"Victor Bazarov" <v.********@comAcast.net> wrote in message news:tZ****************@newsread1.dllstx09.us.to.v erio.net...
Alex Vinokur wrote: "Victor Bazarov" <v.********@comAcast.net> wrote in message news:Zt****************@newsread1.dllstx09.us.to.v erio.net...
[snip]
class Blah {
public:
-----------------------------------------
This is declaration of "operator new"
void* operator new(size_t);
-----------------------------------------
};
This is definition of "operator new"
void* Blah::operator new(size_t)
{
// stuff
}
If we don't define our "operator new" then default "operator new" is used?
Right.
int main() {
--------------------------------------
This is calling the "new operator".
Blah* blah = new Blah;
Basically. It's more appropriate to say "using new operator" instead of
"calling" it.
--------------------------------------
Can we directly call "operator new"?
We can.
When is it worth doing that (instesd of calling the "new operator")?
Probably never.
V
Here are some samples of using overloaded and original "operator new".
Done under impression by
http://www.icce.rug.nl/documents/cpl...lusplus09.html
(C++ Annotations Version 6.1.2 by Frank B. Brokken)
--------- C++ code : BEGIN ---------
// File foo.cpp
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
struct Foo
{
int var1;
int var2;
void show ()
{
cout << "Values: var1 = " << var1 << ", var2 = " << var2 << endl;
}
Foo() {}
Foo (int var1_i, int var2_i) : var1 (var1_i), var2 (var2_i) {}
static void* operator new(size_t size_i);
static void* operator new(size_t size_i, int var1_i, int var2_i);
};
void* Foo::operator new(size_t size_i)
{
cout << __PRETTY_FUNCTION__ << "; size = " << size_i << endl;
void *ptr = new char[size_i];
return ptr;
}
void* Foo::operator new(size_t size_i, int var1_i, int var2_i)
{
cout << __PRETTY_FUNCTION__ << "; size = " << size_i << "; var1_i = " << var1_i << ", var2_i = " << var2_i << endl;
void *ptr = new char[size_i];
memcpy (ptr, (char*)&var1_i, sizeof var1_i);
memcpy ((char*)ptr + sizeof var1_i, (char*)&var2_i, sizeof var2_i);
return ptr;
}
int main()
{
Foo* foo1 = new Foo;
foo1->show();
cout << endl;
Foo* foo2 = new Foo();
foo2->show();
cout << endl;
Foo* foo3 = new Foo(100, 200);
foo3->show();
cout << endl;
Foo* foo4 = new(300, 400) Foo;
foo4->show();
cout << endl;
Foo* foo5 = new(500, 600) Foo ();
foo5->show();
cout << endl;
cout << endl;
Foo* foo6 = ::new Foo;
foo6->show();
cout << endl;
Foo* foo7 = ::new Foo();
foo7->show();
cout << endl;
Foo* foo8 = ::new Foo(700, 800);
foo8->show();
cout << endl;
Foo* foo9 = ::new Foo(900, 1000);
foo9->show();
// --------------------------------------------------------
// Using/calling 'new' as it shown below is not recommended
// --------------------------------------------------------
cout << endl;
cout << endl;
Foo* foo10 = (Foo*)Foo::operator new(sizeof (Foo));
foo10->show();
cout << endl;
Foo* foo11 = (Foo*)Foo::operator new(sizeof (Foo), 1100, 1200);
foo11->show();
return 0;
}
--------- C++ code : END -----------
--------- Compilation & Run : BEGIN ---------
$ g++ -v
[omitted]
gcc version 3.3.3 (cygwin special)
$ g++ foo.cpp
// No errors/warnings
$ a
static void* Foo::operator new(unsigned int); size = 8
Values: var1 = 1628570148, var2 = 1628570148
static void* Foo::operator new(unsigned int); size = 8
Values: var1 = 0, var2 = 0
static void* Foo::operator new(unsigned int); size = 8
Values: var1 = 100, var2 = 200
static void* Foo::operator new(unsigned int, int, int); size = 8; var1_i = 300, var2_i = 400
Values: var1 = 300, var2 = 400
static void* Foo::operator new(unsigned int, int, int); size = 8; var1_i = 500, var2_i = 600
Values: var1 = 500, var2 = 600
Values: var1 = 0, var2 = 0
Values: var1 = 0, var2 = 0
Values: var1 = 700, var2 = 800
Values: var1 = 900, var2 = 1000
static void* Foo::operator new(unsigned int); size = 8
Values: var1 = 0, var2 = 0
static void* Foo::operator new(unsigned int, int, int); size = 8; var1_i = 1100, var2_i = 1200
Values: var1 = 1100, var2 = 1200
--------- Compilation & Run : END -----------
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn