Topic: operator new[] and delete[]


Author: AllanW@my-dejanews.com
Date: 1998/11/18
Raw View
In article <72s3v7$r0h$1@nnrp1.dejanews.com>,
  rado42@my-dejanews.com wrote:
> Hi gurus,
>
> Does anybody know wthether operator new and operator new[] are
> distingushable in the external scope? I mean, provided I wan't to
> make my own new, should I (or can I) also supply new[]? If so, how
> can it be done?

Yes, absolutely. You shouldn't have to prototype these functions
if you use the "normal" signatures:
   void * operator new[](size_t);
   void * operator new(size_t);
   void operator delete[](void*);
   void operator delete(void*);
If you use other function signatures, you will have to declare
them before use.

> I observed this strange behaviour with MSC5:
>
> If I declare new[] (and define it) it gets called by the new[] expressions.
> If I only define it (in other file) it is not called; instead 'new' is called.

Didn't happen for me. Are you sure you used the correct signature?

> OTOH my 'new(size_t)' is called without even if not declared.

As it should be.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/11/18
Raw View
In article 1@nnrp1.dejanews.com, AllanW@my-dejanews.com writes:
>In article <72s3v7$r0h$1@nnrp1.dejanews.com>,
>  rado42@my-dejanews.com wrote:
>> Does anybody know wthether operator new and operator new[] are
>> distingushable in the external scope? I mean, provided I wan't to
>> make my own new, should I (or can I) also supply new[]? If so, how
>> can it be done?
>
>Yes, absolutely. You shouldn't have to prototype these functions
>if you use the "normal" signatures:
>   void * operator new[](size_t);
>   void * operator new(size_t);
>   void operator delete[](void*);
>   void operator delete(void*);

But those are not the "normal" signatures, by which I assume you
mean the predefined versions that come with the standard library.
The standard functions must have throw specifications, and the above
declarations will conflict with them. The standard specifies
four more versions besides.

>If you use other function signatures, you will have to declare
>them before use.

Yes, and they must be "placement forms", since the standard signatures
are pre-empted by the standard. You can replace the standard functions
with your own versions, but you can't use subtly different versions
of the standard signatures and get predictable results.

The standard signatures are as follows:

    // single-object forms
    void* operator new(size_t size) throw(std::bad_alloc);
    void* operator new(size_t size, const std::nothrow_t&) throw();
    void  operator delete(void* ptr) throw();
    void  operator delete(void* ptr, const std::nothrow_t&) throw();

    // array forms
    void* operator new[](size_t size) throw(std::bad_alloc);
    void* operator new[](size_t size, const std::nothrow_t&) throw();
    void  operator delete[](void* ptr) throw();
    void  operator delete[](void* ptr, const std::nothrow_t&) throw();

The original question was about MSC5, and I don't know whether that
particular compiler uses these forms. But since this is comp.STD.c++,
I'm discussing the language definition. Questions about a particular
version of a particular compiler should be addressed to a newsgroup
devoted to that compiler.

---
Steve Clamage, stephen.clamage@sun.com



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: rado42@my-dejanews.com
Date: 1998/11/17
Raw View
Hi gurus,

Does anybody know wthether operator new and operator new[] are
distingushable in the external scope? I mean, provided I wan't to
make my own new, should I (or can I) also supply new[]? If so, how
can it be done?

I observed this strange behaviour with MSC5:

If I declare new[] (and define it) it gets called by the new[] expressions.
If I only define it (in other file) it is not called; instead 'new' is called.

OTOH my 'new(size_t)' is called without even if not declared.

I find this somewhat inconsistent. So, what does the Dtandard say?

------
The same questions apply for delete and delete[], of course.

Thanks for any hints.

Radoslav Getov

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]