operator delete

G

George

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
 
R

Ron Natalie

George said:
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.
 
G

George

Ron Natalie said:
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
 
R

Ron Natalie

George said:
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.
 
G

George

Ron Natalie said:
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
 
R

Ron Natalie

George said:
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.
 
G

George

Ron Natalie said:
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
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top