By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

operator delete

P: n/a
Hi,

There are second form of allocation/deallocation functions
" void * operator new(std::size_t size, const std::nothrow_t&) throw(); "
" void operator delete(void* ptr, const std::nothrow_t&) throw(); "
in the C++ standard.

Operator "new" in second form can be used as:
" T * p = new(nothrow) T; "

But how to invoke operator "delete" in second form ?

Thanks in advance
George
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"George" <ag***@mail.ru> wrote in message news:bq**********@newsreader.mailgate.org...
Hi,

There are second form of allocation/deallocation functions
" void * operator new(std::size_t size, const std::nothrow_t&) throw(); "
" void operator delete(void* ptr, const std::nothrow_t&) throw(); "
in the C++ standard.

Operator "new" in second form can be used as:
" T * p = new(nothrow) T; "

But how to invoke operator "delete" in second form ?


You can't. The "placement" delete versions are only used in the event
of an exception during creating of the object.
Jul 22 '05 #2

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f**********************@news.newshosting.com ...
"George" <ag***@mail.ru> wrote in message

news:bq**********@newsreader.mailgate.org...
" void operator delete(void* ptr, const std::nothrow_t&) throw(); "
But how to invoke operator "delete" in second form ?


You can't. The "placement" delete versions are only used in the event
of an exception during creating of the object.


As I understand you, "delete" in second form can be called internaly by the
C++ compiler only.
So what does mean MS VC .NET 2003 (vc71) declaration:
void AFX_CDECL operator delete(void* p, LPCSTR lpszFileName, int nLine); //
afx.h line 1331
void __cdecl operator delete[](void* p, LPCSTR lpszFileName, int nLine); //
afx.h line 1337

Is it just MS extension and can be called only internally too ?

Thanks

George
Jul 22 '05 #3

P: n/a

"George" <ag***@mail.ru> wrote in message news:bq**********@newsreader.mailgate.org...

Is it just MS extension and can be called only internally too ?


Yes. Whenever you define a placement new allocation function, you
pretty much need to make the matching deallocation function to match,
even if it practically never gets called.
Jul 22 '05 #4

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f**********************@news.newshosting.com ...
Yes. Whenever you define a placement new allocation function, you
pretty much need to make the matching deallocation function to match,
even if it practically never gets called.


Well, if it is true - it is bad 'cause:

When I use my own 'operator new' for memory tracking purpose as following:

// my_declaration.h
void * operator new(size_t, MyInfo);
void operator delete(void * p, MyInfo);
#define MY_NEW new(myInfo)

// using_new.cpp
T1 * t1 = MY_NEW T1; // wanna track 't1', using my new
T2 * t2 = new T2; // don't care of 't2', using default new
....
delete t1; // very bad, my 'delete' can't be called
delete t2; // right, should call default 'delete'

And how can I organize my memory tracking ?

Thanks
George
Jul 22 '05 #5

P: n/a

"George" <ag***@mail.ru> wrote in message news:bq**********@newsreader.mailgate.org...
Well, if it is true - it is bad 'cause:

When I use my own 'operator new' for memory tracking purpose as following:


Sorry, delete doesn't work the way you want. You're memory allocator is going
to have to do the remembering. The "placement" form of the deallocation operator is
NEVER called from the delete expression. It's only used to clean up messes caused
by exceptions during construction.

You are going to have to overload the default deallocation function to see if it is one
you care about.
Jul 22 '05 #6

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f**********************@news.newshosting.com ...

Sorry, delete doesn't work the way you want. You're memory allocator is going to have to do the remembering. The "placement" form of the deallocation operator is NEVER called from the delete expression. It's only used to clean up messes caused by exceptions during construction.
That's fine!!!
You are going to have to overload the default deallocation function to see if it is one you care about.


But if I overload the default deallocation function I have a collision with:

delete t1; // very bad, my 'delete' can't be called
delete t2; // right, should call default 'delete'

May be the only way to solve this problem is to overload the default delete
and ignore all unknown memory blocks

George
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.