0

Which library has the definition for the global new and the delete operator? Specifically which file in the library contains the definition for these operators?

nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
  • This depends on your implementation. – R. Martinho Fernandes Jul 05 '12 at 12:03
  • If you are asking this to look at how they are made, they use the malloc() and free() inside of themselves. – SingerOfTheFall Jul 05 '12 at 12:05
  • @SingerOfTheFall: That is not necessarily true. – CB Bailey Jul 05 '12 at 12:05
  • The new operator has to allocate the memory and then call the constructor. There should be a definition for the new operator which does these things. I am specifically looking for the file where this definition is available. – nitin_cherian Jul 05 '12 at 12:08
  • @LinuxPenseur *somewhere* in the source code of your standard library implementation, which you haven't disclosed which is. – R. Martinho Fernandes Jul 05 '12 at 12:09
  • @LinuxPenseur, may I ask, why do you need this?) – SingerOfTheFall Jul 05 '12 at 12:11
  • @ SingerOfTheFall: Curiosity. Just to know how those have been implemented. We know what they does. I want to know how it is done. – nitin_cherian Jul 05 '12 at 12:13
  • @LinuxPenseur, I see... Maybe this will be somewhat useful for you: "operator new can be called explicitly as a regular function, but in C++, new is an operator with a very specific behavior: An expression with the new operator, first calls function operator new with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed). Finally, the expression evaluates as a pointer to the appropriate type." – SingerOfTheFall Jul 05 '12 at 12:17
  • 1
    Using `new X` is built into the compiler, just like `if` and `switch`. The `operator new()` only handles the memory allocation. – Bo Persson Jul 05 '12 at 12:21

2 Answers2

3

It's not specified where these are defined but it is specified that they are declared in the header <new>. (You shouldn't have to include <new> if you are just using the standard versions used by non-placement new expressions, these are available automatically in all translation units.)

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
3

The non-placement forms of operator new and operator delete (and their array forms) are declared implicitly in the global scope in every translation unit.

The standard placement forms are declared in <new>.

The definitions of library functions are not generally available. Your supplier might supply the source, in which case they will be in there somewhere.

Although, from your comments, you may be talking about the use of new in a new-expression, rather than the operator new function that allocates memory. The new-expression is handled by the compiler, generating the necessary calls to operator new and the object(s) constructor(s). There probably won't be a specific implementation of this anywhere; you'd need to look at the relevant part of the compiler's code generator.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • @ Mike: Could i see the code for them just like seeing the code for 'malloc', 'printf' etc – nitin_cherian Jul 05 '12 at 12:09
  • @LinuxPenseur: The declarations are implicit so you can't see them. The definitions are somewhere in the source that either your library or your compiler was built from - it's up to the supplier whether or not that's available. – Mike Seymour Jul 05 '12 at 12:11