Thread View: comp.std.c++
4 messages
4 total messages
Started by John Hickin
Fri, 04 Aug 1995 21:13
Operator new Question
Author: John Hickin
Date: Fri, 04 Aug 1995 21:13
Date: Fri, 04 Aug 1995 21:13
34 lines
1307 bytes
1307 bytes
I accidently discovered that the following code compiles and executes using my
favourite compiler:
#include <iostream.h>
#include <stddef.h>
void* operator new(unsigned sz,const char*,int)
{
return ::operator new(sz);
}
struct Foo { Foo(); ~Foo(); };
Foo::Foo() { cerr << "Foo::Foo[this==" << (void*)this << ']' << endl; }
Foo::~Foo() { cerr << "Foo::~Foo[this==" << (void*)this << ']' << endl; }
main()
{
Foo* p = new(__FILE__,__LINE__) Foo[ 42 ];
delete[] p;
}
(1) Is it legal C++?
(2) If so, what should the behavior be?
In my environment (HP-UX 9.x, C++ A.03.05) the Foo::Foo's are called but the
Foo::~Foo's are not called. If i change new(__FILE__,__LINE__) to just new the
destructors get called as expected.
--
John Hickin Bell-Northern Research, Montreal, Quebec
(514) 765-7924 hickin@bnr.ca
Re: Operator new Question
Author: Steve Clamage
Date: Fri, 04 Aug 1995 22:45
Date: Fri, 04 Aug 1995 22:45
31 lines
1243 bytes
1243 bytes
In article kav@bmtlh10.bnr.ca, John Hickin <hickin@bnr.ca> writes:
>I accidently discovered that the following code compiles and executes using my
>favourite compiler:
>
>#include <iostream.h>
>#include <stddef.h>
>
>void* operator new(unsigned sz,const char*,int)
>{
> return ::operator new(sz);
>}
>
>struct Foo { Foo(); ~Foo(); };
>
>Foo::Foo() { cerr << "Foo::Foo[this==" << (void*)this << ']' << endl; }
>Foo::~Foo() { cerr << "Foo::~Foo[this==" << (void*)this << ']' << endl; }
>
>main()
>{
> Foo* p = new(__FILE__,__LINE__) Foo[ 42 ];
> delete[] p;
>}
>
>(1) Is it legal C++?
>(2) If so, what should the behavior be?
The results of using "delete" on a pointer value obtained from a placement-
new are undefined. Any result is allowed. It is invalid C++, but no
diagnostic is required.
---
Steve Clamage, stephen.clamage@eng.sun.com
Re: Operator new Question
Author: Ramesh Seshadri
Date: Sun, 06 Aug 1995 19:19
Date: Sun, 06 Aug 1995 19:19
50 lines
1569 bytes
1569 bytes
In <3vu2gv$kav@bmtlh10.bnr.ca> John Hickin <hickin@bnr.ca> writes:
>
>I accidently discovered that the following code compiles and executes
using my
>favourite compiler:
>
>#include <iostream.h>
>#include <stddef.h>
>
>void* operator new(unsigned sz,const char*,int)
>{
> return ::operator new(sz);
>}
>
>struct Foo { Foo(); ~Foo(); };
>
>Foo::Foo() { cerr << "Foo::Foo[this==" << (void*)this << ']' << endl;
}
>Foo::~Foo() { cerr << "Foo::~Foo[this==" << (void*)this << ']' <<
endl; }
>
>main()
>{
> Foo* p = new(__FILE__,__LINE__) Foo[ 42 ];
> delete[] p;
>}
>
>
>
>(1) Is it legal C++?
>(2) If so, what should the behavior be?
>
>In my environment (HP-UX 9.x, C++ A.03.05) the Foo::Foo's are called
but the
>Foo::~Foo's are not called. If i change new(__FILE__,__LINE__) to
just new the
>destructors get called as expected.
>
>--
>John Hickin Bell-Northern Research, Montreal, Quebec
>(514) 765-7924 hickin@bnr.ca
>
Yes it is a legal C++ code. And it is supposed to call the overloaded
new one time and the ctor and dtor of the Foo for 42 times. In my
environment (Microsoft Visual C++ 2.0) the same code is working without
any change.
- Ramesh S
rameshs@ix.netcom.com
Re: Operator new Question
Author: J. Kanze
Date: Mon, 07 Aug 1995 12:35
Date: Mon, 07 Aug 1995 12:35
22 lines
1383 bytes
1383 bytes
Steve Clamage (clamage@Eng.Sun.COM) wrote:
|> The results of using "delete" on a pointer value obtained from a placement-
|> new are undefined. Any result is allowed. It is invalid C++, but no
|> diagnostic is required.
Can this be true? I always thought that the results of using delete
were to call the destructors, and then call operator delete(). If the
memory was not obtained by calling operator new( size_t ), then the
results are undefined, but in the example given, the memory was obtained
by the placement new calling operator new( size_t ).
Does this mean that I cannot delete memory obtained by:
p = new ( nothrow() ) T ;
This would sort of make this version of new useless.
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
--Beratung in industrieller Datenverarbeitung
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads