 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthias Hofmann Guest
|
Posted: Tue Oct 28, 2003 6:38 pm Post subject: Prototype for operator new required? |
|
|
Hello!
I have learned from the standard that the function prototype for operator
new is declared in a header file of the standard library. However, I can use
the new operator in my programmes without including any of the standard
headers. Is there no declaration of operator new required because it is
*implicitly* declared? And if so, why is its prototype declared in the
standard library's header?
Best regards,
Matthias Hofmann
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Christoph Schulz Guest
|
Posted: Wed Oct 29, 2003 4:50 pm Post subject: Re: Prototype for operator new required? |
|
|
Hello!
Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:
| Quote: | Hello!
I have learned from the standard that the function prototype for
operator new is declared in a header file of the standard library.
However, I can use the new operator in my programmes without
including any of the standard headers. Is there no declaration of
operator new required because it is *implicitly* declared? And if so,
why is its prototype declared in the standard library's header?
|
See 3.7.3/2:
[...]
The following allocation and deallocation functions (18.4) are
implicitly declared in global scope in each translation unit of
a program
void* operator new(std::size_t) throw(std::bad_alloc);
void* operator new[](std::size_t) throw(std::bad_alloc);
void operator delete(void*) throw();
void operator delete[](void*) throw();
These implicit declarations introduce only the function names
operator new, operator new[], operator delete, operator delete[].
[Note: the implicit declarations do not introduce the names std,
std::bad_alloc, and std::size_t, or any other names that the
library uses to declare these names. Thus, a new-expression,
delete-expression or function call that refers to one of these
functions without including the header <new> is wellformed. However,
referring to std, std::bad_alloc, and std::size_t is illformed
unless the name has been declared by including the appropriate
header. ]
[...]
Regards,
Christoph
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Oct 29, 2003 6:17 pm Post subject: Re: Prototype for operator new required? |
|
|
"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> wrote
| Quote: | Hello!
I have learned from the standard that the function prototype for operator
new is declared in a header file of the standard library. However, I can use
the new operator in my programmes without including any of the standard
headers. Is there no declaration of operator new required because it is
*implicitly* declared? And if so, why is its prototype declared in the
standard library's header?
|
The simple (no placement) new has an implicit declaration. If you want
to use the implementation provided placement new, you can get it declared
by #including <new>. Of course any ones you specifically provide need their
own declaration.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Oct 29, 2003 11:16 pm Post subject: Re: Prototype for operator new required? |
|
|
In article <bnk6h2$sc7$2 (AT) news1 (DOT) nefonline.de>, Matthias Hofmann wrote:
| Quote: | Hello!
I have learned from the standard that the function prototype for
operator new is declared in a header file of the standard library.
However, I can use the new operator in my programmes without
including any of the standard headers.
|
Confusingly, "operator new" is not the same thing as the "new"
operator. The "new" operator
1) allocates memory (may fail, but I'm ignoring that for now)
2) construct an object in that memory
2.1) if that throws an exception, frees the memory then rethrows
| Quote: | Is there no declaration of operator new required because it is
*implicitly* declared? And if so, why is its prototype declared
in the standard library's header?
|
The standard (non-placement, throwing) operator new, new[], delete
and delete[] functions are implicitly declared (standard section
3.7.3 paragraph 2). The placement operator new/delete functions
must be explicitly declared, which is the purpose of <new>. I
don't know why the standard functions are supposed to be declared
there as well.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Micah Cowan Guest
|
Posted: Thu Oct 30, 2003 4:12 pm Post subject: Re: Prototype for operator new required? |
|
|
Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> writes:
| Quote: | In article <bnk6h2$sc7$2 (AT) news1 (DOT) nefonline.de>, Matthias Hofmann wrote:
Hello!
I have learned from the standard that the function prototype for
operator new is declared in a header file of the standard library.
However, I can use the new operator in my programmes without
including any of the standard headers.
Confusingly, "operator new" is not the same thing as the "new"
operator. The "new" operator
1) allocates memory (may fail, but I'm ignoring that for now)
2) construct an object in that memory
2.1) if that throws an exception, frees the memory then rethrows
|
I'm pretty sure OP was aware of the distinction, which is also
pretty much irrelevant to his question: the "new" operator (as
defined by the Standard) would fail to work at all were there no
definitions for "operator new" for it to find (with declarations
for them in scope).
--
Micah J. Cowan
[email]micah (AT) cowan (DOT) name[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Thu Oct 30, 2003 7:08 pm Post subject: Re: Prototype for operator new required? |
|
|
On Tue, 28 Oct 2003 13:38:27 -0500, Matthias Hofmann wrote:
| Quote: | Hello!
I have learned from the standard that the function prototype for operator
new is declared in a header file of the standard library. However, I can use
the new operator in my programmes without including any of the standard
headers. Is there no declaration of operator new required because it is
*implicitly* declared? And if so, why is its prototype declared in the
standard library's header?
|
Yes, it is implicitly included by the compiler whenever you use new or
operator new. It is howevere defined in the standard header <new>.
operator new (std::size_t);
will be responsible for memory allocation, and
placement new defined as:
operator new (std::size_t, void*); will just return the void* pointer
passed (typically) , because the new operator will construct the object at
that location. Unless the library defines other stuff like alignment to be
borne by this placement new.
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Fri Oct 31, 2003 10:05 am Post subject: Re: Prototype for operator new required? |
|
|
Micah Cowan wrote:
<snip>
| Quote: | I'm pretty sure OP was aware of the distinction, which is also
pretty much irrelevant to his question: the "new" operator (as
defined by the Standard) would fail to work at all were there no
definitions for "operator new" for it to find (with declarations
for them in scope).
|
I don't see why. It works without declarations of std::size_t
and std::bad_alloc, which an explicit declaration of operator new
depends on.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|