Here's simple class overloads operator new and operator delete.
class test_class
{
public:
test_class()
{
cout << "ctor" << endl;
}
~test_class()
{
cout << "dtor" << endl;
}
void* operator new( size_t sz )
{
cout << "operator new" << endl;
// Of course, I have return allocated memory blocks but,
// this is just a test. Forgive me...:-)
}
void operator delete( void* p )
{
cout << "operator delete" << endl;
}
};
And I create this_class using operator new.
int main()
{
test_class* t = new test_class;
return 0;
}
Result is,
>operator new
>ctor
Shiiit ! Who called test_class::test_class() !!!
Okay, calm down...
Temporary object can be created somewhere I didn't notice.
If then, why destructor doesn't be called ?
Help newbie plz...
I'm waiting your powerful answer.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
hongseok.yoon (19)
|
7/6/2005 10:53:46 AM |
|
<hongseok.yoon@gmail.com> ha scritto nel messaggio
news:1120631021.863659.321170@f14g2000cwb.googlegroups.com...
> Here's simple class overloads operator new and operator delete.
>
> class test_class
> {
> public:
> test_class()
> {
> cout << "ctor" << endl;
> }
>
> ~test_class()
> {
> cout << "dtor" << endl;
> }
>
> void* operator new( size_t sz )
> {
> cout << "operator new" << endl;
> // Of course, I have return allocated memory blocks but,
> // this is just a test. Forgive me...:-)
> }
>
> void operator delete( void* p )
> {
> cout << "operator delete" << endl;
> }
> };
>
> And I create this_class using operator new.
>
> int main()
> {
> test_class* t = new test_class;
>
> return 0;
> }
>
> Result is,
>
>>operator new
>>ctor
>
> Shiiit ! Who called test_class::test_class() !!!
> Okay, calm down...
>
operator new() is a different thing from the new-expression. Your line
"test_class* t = new test_class; " is implemented in two steps:
1. a call to operator new() is made to gather the necessary memory to
contruct the object.
2. if operator new() returns successfully a block of memory the compiler
invokes the constructor of your class;
So you see that overloading operator new doesn' t prevent the call of the
constructor, which I think cannot be prevented in any case, at least when
using the new expression.
HTH
Gianluca
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Gianluca
|
7/6/2005 12:31:36 PM
|
|
hongseok.yoon@gmail.com wrote:
> int main()
> {
> test_class* t = new test_class;
>
> return 0;
> }
>
> Result is,
>
>>operator new
>>ctor
>
> Who called test_class::test_class() !!!
You need to distinguish between "operator new()" and "new T".
The purpose of the former is solely to allocate memory for an object
while the latter first calls "operator new()" and next construct an
object in the allocated memory. That is, the expression
'new test_class' first calls your 'operator new()' and next invokes
the constructor. Since you never use 'delete t' the object is never
destructed and the corresponding memory is never released with
'operator delete()'.
There is an item on this exact issue in one of Scott Meyers' books.
I think it is "More Effective C++" but I'm not sure.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Dietmar
|
7/6/2005 1:29:36 PM
|
|
On 6 Jul 2005 06:53:46 -0400, <hongseok.yoon@gmail.com> wrote:
[...]
>
> void* operator new( size_t sz )
> {
> cout << "operator new" << endl;
> // Of course, I have return allocated memory blocks but,
> // this is just a test. Forgive me...:-)
> }
>
[...]
i wonder what the return value of your operator new would be, since you
are omitting the return statement...
it seems that it can be an arbitrary value (although this is flaw of the
compiler, imho), so your app should crash sooner or later when costructing
an object using new.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
ulrich
|
7/6/2005 9:57:39 PM
|
|
Thanks.
But I'm still wondering...
about your answer #1
operator new() gathers the necessary memory even if I didn't do it in
my overloaded operator new() ?
about your answer #2
what do you mean that operator new( returns sucessfully a block of
memory?
If I return NULL or throw no exceptions( throw() ) in my operator new()
function, still the constructor was invoked.
( Of course there's a warining about it if i returned NULL )
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
hongseok
|
7/7/2005 12:21:06 PM
|
|
You mean that *t* has any allocated memory blocks?
If I tried to delete it, my app was halted leaving core file.
If my app allocated memory block to *t*, when it has done?
If so, who overload operator new to use their own memory allocator?
thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
hongseok
|
7/7/2005 12:21:29 PM
|
|
hongseok.yoon@gmail.com schreef:
> You mean that *t* has any allocated memory blocks?
> If I tried to delete it, my app was halted leaving core file.
Well, the compiler skips comments, so it probably missed your plea:
// Of course, I have to return allocated memory blocks but,
// this is just a test. Forgive me...:-)
Or the compiler did read it, but didn't understood english. Anyway,
you're just seeing what happens if you confuse the compiler.
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
msalters
|
7/8/2005 12:34:09 AM
|
|
<hongseok.yoon@gmail.com> ha scritto nel messaggio
news:1120700491.450094.113140@g47g2000cwa.googlegroups.com...
> Thanks.
> But I'm still wondering...
>
> about your answer #1
> operator new() gathers the necessary memory even if I didn't do it in
> my overloaded operator new() ?
No. The system only invokes *your* operator new() to allocate memory. So if
you decide to not allocate any memory, the new-expression will fail.
Remember to follow requirements specified in the standard that is:
if you replace operator new(std::size_t) throw(std::bad_alloc) either you
return a valid address of memory or you must throw a std::bad_alloc
exception, or a derived one.
if you replace operator new(std::size_t) throw() either you return a valid
address of memory or you return a null pointer.
>
> about your answer #2
> what do you mean that operator new( returns sucessfully a block of
> memory?
> If I return NULL or throw no exceptions( throw() ) in my operator new()
> function, still the constructor was invoked.
if you replace operator new() throw(std::bad_alloc) you MUST throw an
exception to indicate a failure. Otherwise you must return a non-null
pointer. Anything else is undefined behaviour.
HTH
Gianluca
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Gianluca
|
7/8/2005 12:37:29 AM
|
|
hongseok.yoon@gmail.com wrote:
> But I'm still wondering...
> about your answer #1
> operator new() gathers the necessary memory even if I didn't
> do it in my overloaded operator new() ?
No. What he meant is that any operator new() function must
return a pointer to a block of memory which is 1) correctly
aligned, and 2) big enough to construct the object in. If you
return NULL, or some invalid value, you have undefined
behavior. If you don't actually use the value later (the
constructor is a no-op, and you don't do anything else with the
object), then you probably won't see the error, but it is there
anyway.
> about your answer #2
> what do you mean that operator new( returns sucessfully a
> block of memory? If I return NULL or throw no exceptions(
> throw() ) in my operator new() function, still the constructor
> was invoked.
Exactly what he said: the operator new() function must return a
pointer to a valid block of memory. The compiler counts on it;
it invokes the constructor with the pointer operator new
returns. Without any verification whether this pointer is valid
or not. (I you return a random value, say by falling off the
end of the function, there is no way to determine that it is not
a valid pointer.)
--
James Kanze GABI Software
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kanze
|
7/8/2005 12:45:32 AM
|
|
|
8 Replies
151 Views
(page loaded in 0.116 seconds)
Similiar Articles: Swing uses bad features - comp.lang.java.guiWell, I COULD call setText() in MY constructor, but that does the ... it is only the call to the constructor that tells Java that a new ... here init to nothing and //call ... Preventing A New Window From Grabbing Focus - comp.lang.java ...Then I clicked onthe text editor and typed nothing but ... void initComponents() { jButton1 = new ... Attempt to call constructor image with incorrect letter case ... Calling a method - comp.lang.java.helpHi can some one tell me why I can call union and ... i++) { arraySet[i] = false; } } //overloading constructor ... IntergerSet set = new IntergerSet(); set ... STL allocators, global new/delete using the heap and shared memory ...... containers never make a single call to the ... but rather giving the allocator as a constructor ... Overloading operators new and delete - comp.lang.c++.moderated ... Const constructor - comp.lang.c++.moderated... You can invoke a const constructor for both ... The 'const char *' constructor. 4. operator<< overload ... the operator new in the throw statement OR make the X constructor ... header's included but still get 'implicit declaration' - comp.unix ...... or at least its best impersonation of one) and nothing more. ... might be defined with any of a number of values, invoke ... would still be ... stdlib.h with references to the new ... Should I use C++ or Java for Numeric Intensive Calculations - comp ...I am new to both C++ and Java and I will like to know ... Very important: Ability to call existing fortran ... between the two languages has been operator overloading and ... Dos and don'ts in C++ unit testing? - comp.lang.c++.moderated ...The new projects are always more interesting than ... otherwise you don't have to forget to call the proper ... that the method is > correctly implemented, but does nothing ... Array of something - comp.lang.java.help... thanks to the call to the accessor: == Tableau tableau = new ... setOfCells = new Cell[n][n]; > } > } > == What does the constructor 's ... so you have > nothing ... left-hand operand of comma has no effect - comp.lang.c++ ...Hi all, I like to implement a new syntax in C++ ... the meaning is a+b, then why don't you simply overload ... In this particular case, 1 and 2 do absolutely nothing ... C++: constructor overloading, copy constructors, copy constructor... at all and does nothing, and a copy constructor that creates a new ... problems with constructor overloading ... // Explicitly call the Vector::Vector(unsigned int) constructor AmbiguousMatchException Constructor (String, Exception)AmbiguousMatchException Constructor (String, Exception) ... Class Myambiguous 'The first overload is ... Int32 integer Mymethodinfo32.Invoke(Nothing, New ... 7/16/2012 12:50:47 PM
|