Board index » cppbuilder » Re: Overloading operator "delete".

Re: Overloading operator "delete".


2003-10-02 10:44:32 AM
cppbuilder27
"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
In C++, you should be able to declare and define the overload. It seems
that
the C++ Builder compiler has problems with it, though. FWIW, g++ and
Comeau
don't have a problem with

void* operator new ( unsigned int _uiSize, int, int, int );
void operator delete( void *_pvMemPtr, int, int, int);
Thanks for your help, Thomas. That's exactly what I was trying to
understand.
Quote
I shouldn't have commented about the help file because I don't know which
Help topic you are referring to. I relied on my understand on what you
posted only.
I took it from online help "C++ Specific Operators" ->"delete".
Holy Punk.
 
 

Re:Re: Overloading operator "delete".

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
You shouldn't need to override anything in order to accomplish that. Just
use the existing "placement new" operator to pass a pre-allocated block of
memory to 'new' so that it does not allocate its own memory using the
default allocator. That lets the application code, not the language,
decide
how to handle the memory for the object.
I might be wrong, but I think it's RTL memory manager (implementation
depended) that handles object allocation, not the language (supposed to be
implementation independed). From my point of view, memory manager,
effectively, is part of my application code, considering binary.
Quote
>Why can I not declare, say, delete(void*, int,int,int),
>why should it be only one parameter (two)?
Because that is simply the way 'delete' work. You simply give 'delete' a
block of memory and it frees it, that's it. The 'delete' operator is not
designed to accept extra parameters. The help file specifically states as
much:

"The global operators, ::operator delete(), and ::operator delete[]()
cannot be overloaded. However, you can override the default version of
each
of these operators with your own implementation. Only one instance of the
each global delete function can exist in the program."
Sorry if I was unclear and misled you. What I was trying to say is that I
should be able to overload placement form of operator "delete" with
different from default signature same way as I can do it for operator "new".
This is only logical, I think. After all, what IS so special about "new" or
"delete"? It appears to me that nothing, from the language point of view.
This is actually confirmed by online help that comes with BCB:
(about "new")
"The default placement forms of operator new are reserved and cannot be
redefined. You can, however, overload the placement form with a different
signature (i.e. one having a different number, or different type of
arguments)."
(about "delete")
"If you overload the placement version of operator new, it is a good idea
(though not strictly required) to provide the overload the placement delete
operator with the corresponding signature."
Thomas Maeder wrote in reply to this question, I quote:
"In C++, you should be able to declare and define the overload. It seems
that
the C++ Builder compiler has problems with it, though. FWIW, g++ and Comeau
don't have a problem with
void* operator new ( unsigned int _uiSize, int, int, int );
void operator delete( void *_pvMemPtr, int, int, int);"
So it seems that it's indeed some kind of "misbehaviour" of BCB.
Quote
If you need to control how the memory is deallocated, then you need to
either 1) preallocate the memory separately and then use the standard
placement new/delete operators to use that memory, or else 2) override
'new'
in the class itself to allocate the memory with extra room for additional
values where you can store memory-specific information, which your
overriden
'delete' in the class can then read back later and deallocate the memory
appropriately. That is how memory managers work.
I guess, I just want this world to be perfect ;). Given that overloaded
"new" with my custom parameters behaves as normal "new" in respect to
execution of my code (memory allocation) and then calling constructors, I
wanted to get overloaded version of "delete" to accept custom parameters,
call destructors and execute my code (deallocation). However, it turned out
that I cannot even declare overloaded "delete" with my custom parameters in
BCB. And it seems only BCB, as Thomas clarified.
Thanks for spending your time and writing these sniplets. I liked the way
you changed structure packing size to reduce memory usage. That was somewhat
new to me, I was stuck with memory and I will definatelly try to use it.
Does it also overwrite default ( I think) 4-byte stack alignment? Isn't it
faster to use 4byte alignment on 32bits processors when whole thing goes
into register?
The code you've provided solves the problem, of course. But I was looking
for more... hmm... general way of doing it? However, I guess, I'm somewhat
limited by Borland implementation.
Thanks again,
Holy Punk.
 

Re:Re: Overloading operator "delete".

"Holy Punk" < XXXX@XXXXX.COM >wrote in message
Quote
I might be wrong, but I think it's RTL memory manager
(implementation depended) that handles object allocation,
not the language (supposed to be implementation independed).
The point I was trying to make is that the standard 'new' operator has some
form of memory manager that is used behind the scenes by default, whatever
that happens to be, but if you use the 'placement new' operator instead,
then your own code are in control of the memory that is used for the new
object instance, not whichever memory manager 'new' uses internally.
Quote
This is actually confirmed by online help that comes with BCB:
The help file is self-conflicting. In one place, it says to overload
'delete'. In another, it says that 'delete' cannot be overloaded. The two
are direct opposites of each other, they can't both be right.
Quote
I guess, I just want this world to be perfect ;)
Good luck!
Quote
Does it also overwrite default ( I think) 4-byte stack alignment?
It sets the alignment only for the structure itself and then resets it back
to its previous value afterwards. The rest of the code is uneffected by the
the alignment change.
Quote
Isn't it faster to use 4byte alignment on 32bits processors when
whole thing goes into register?
Even though the structure is byte-aligned in my example, the members will
still be DWORD-aligned when stuffed into memory/registers since a pointer
and a DWORD are both 4 bytes in size. I always use byte alignment for my
structures when dealing with custom memory management, files, or TCP
sockets. That's just my programming style.
Gambit
 

{smallsort}

Re:Re: Overloading operator "delete".

"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
This text doesn't say that you can't add placement overloads for operator
new
and operator delete.
Exactly!
Holy Punk.
 

Re:Re: Overloading operator "delete".

"Holy Punk" < XXXX@XXXXX.COM >writes:
Quote
>I shouldn't have commented about the help file because I don't know which
>Help topic you are referring to. I relied on my understand on what you
>posted only.

I took it from online help "C++ Specific Operators" ->"delete".
This text doesn't say that you can't add placement overloads for operator new
and operator delete. It only says that the *default* forms are reserved and
can't be provided user-defined implementations.
You are free to add *non-default* forms.