Placement syntax

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

In the C++ programming language, placement syntax allows programmers to explicitly specify the memory management of individual objects — i.e. their "placement" in memory. Normally, when an object is created dynamically, an allocation function is invoked in such a way that it will both allocate memory for the object, and initialize the object within the newly allocated memory. The placement syntax allows the programmer to supply additional arguments to the allocation function. A common use is to supply a pointer to a suitable region of storage where the object can be initialized, thus separating memory allocation from object construction.

The "placement" versions of the new and delete operators and functions are known as placement new and placement delete.[1] A new expression, placement or otherwise, calls a new function, also known as an allocator function, whose name is operator new. Similarly, a delete expression calls a delete function, also known as a deallocator function, whose name is operator delete.[2][3]

Any new expression that uses the placement syntax is a placement new expression, and any operator new or operator delete function that takes more than the mandatory first parameter (size_t and void *, respectively) is a placement new or placement delete function.[4]

Expressions

The Standard C++ syntax for a non-placement new expression is[2]

new new-type-id ( optional-initializer-expression-list )

The placement syntax adds an expression list immediately after the new keyword. This expression list is the placement. It can contain any number of expressions.[2][3][5]

new ( expression-list ) new-type-id ( optional-initializer-expression-list )

There is no placement delete expression.[6]

Versions of the g++ compiler prior to version 2.3.1 accept a different syntax for placement new expressions:[7]

new { expression-list } new-type-id ( optional-initializer-expression-list )

Up until version 2.2.2, this was the only placement syntax that that compiler would accept.[7]

Functions

The placement new functions are overloads of the non-placement new functions. The declaration of the non-placement new functions, for non-array and array new expressions respectively, are:[8][9]

void * operator new (size_t) throw(std::bad_alloc);
void * operator new[] (size_t) throw(std::bad_alloc);

The Standard C++ library provides two placement overloads each for these functions. Their declarations are:[8][9]

void * operator new (size_t, const std::nothrow_t &) throw();
void * operator new (size_t, void *) throw();
void * operator new[] (size_t, const std::nothrow_t &) throw();
void * operator new[] (size_t, void *) throw();

In all of the overloads, the first parameter to the operator new function is of type size_t, which when the function is called will be passed as an argument the amount of memory, in bytes, to allocate. All of the functions must return type void *, which is a pointer to the storage that the function allocates.[2]

There are also placement delete functions. They are overloaded versions of the non-placement delete functions. The non-placement delete functions are declared as:[8][9]

void operator delete (void *) throw();
void operator delete[] (void *) throw();

The Standard C++ library provides two placement overloads each for these functions. Their declarations are:[8][9]

void operator delete (void *, const std::nothrow_t &) throw();
void operator delete (void *, void *) throw();
void operator delete[] (void *, const std::nothrow_t &) throw();
void operator delete[] (void *, void *) throw();

In all of the overloads, the first parameter to the operator delete function is of type void *, which is the address of the storage to deallocate.[2]

For both the new and the delete functions, the functions are global, are not in any namespace, and do not have static linkage.[2]

Use

Placement syntax has four main uses: default placement, preventing exceptions, custom allocators, and debugging.

Default placement

The placement overloads of operator new and operator delete that employ an additional void * parameter are used for default placement, also known as pointer placement. Their definitions by the Standard C++ library, which it is not permitted for a C++ program to replace or override, are:[8][9][10]

void * operator new (size_t, void * p) throw() { return p ; }
void * operator new[] (size_t, void * p) throw() { return p ; }
void operator delete (void *, void *) throw() { }
void operator delete[] (void *, void *) throw() { }

Default placement does not require the inclusion of the Standard C++ library header <new> in the source code of a C++ program.[2] However, g++ version4.0 still requires the use of this header (this might be true of other versions of the compiler or other compilers too.)

There are various uses for default placement.

Bjarne Stroustrup originally observed, in his book The Design and Evolution of C++, that pointer placement new is necessary for hardware that expects a certain object at a specific hardware address. It is also required for the construction of objects that need to reside in a certain memory area, such as an area that is shared between several processors of a multiprocessor computer.[11]

Other uses, however, include calling a constructor directly, something which the C++ language does not otherwise permit.[3]

The C++ language does allow a program to call a destructor directly, and, since it is not possible to destroy the object using a delete expression, that is how one destroys an object that was constructed via a pointer placement new expression. For example:[12][6]

p->~T() ;

Preventing exceptions

Normally, the (non-placement) new functions throw an exception, of type std::bad_alloc, if they encounter an error, such as exhaustion of all available memory. This was not how the functions were defined by Stroustrup's Annotated C++ Reference Manual, but was a change made by the standardization committee when the C++ language was standardized. The original behaviour of the functions, which was to return a NULL pointer when an error occurred, is accessible via placement syntax.[3][5][4]

Programmers who wish to do this in their programs must include the Standard C++ library header <new> in the source code. This header declares the global std::nothrow object, which is of type std::nothrow_t (also declared in the header), which is used to call the overloaded new functions that are declared as taking const nothrow_t & as their second parameter. For example:[10]

#include <new>

struct T {} ;

int main ()
{
    // Call the function operator new(size_t, const nothrow_t &) and (if successful) construct the object.
    T * p = new (std::nothrow) T ;
    if (p) {
        // The storage has been allocated and the constructor called.
        delete p ;
    } else
        ; // An error has occurred.  No storage has been allocated and no object constructed.
    return 0 ;
}

Custom allocators

Placement syntax is also employed for custom allocators. This does not use any of the allocator and deallocator functions from the Standard C++ library header <new>, but requires that programmers write their own allocation and deallocation functions, overloaded for user-defined types. For example, one could define a memory management class as follows:[9][8]

#include <cstdlib>
class A {
public:
    void * allocate ( size_t ) ;
    void deallocate ( void * ) ;
} ;

And define custom placement allocation and deallocation functions as follows:[9][8]

void *
operator new (size_t size, A & arena)
{
    return arena.allocate(size) ;
}
void
operator delete (void * p, A & arena)
{
    arena.deallocate(p) ;
}

The program would employ the placement syntax to allocate objects using different instances of the A class as follows:[9][8]

A first_arena, second_arena ;
T * p1 = new (first_arena) T ;
T * p2 = new (second_arena) T ;

Destroying an object whose storage is allocated in such a fashion requires some care. Because there is no placement delete expression, one cannot use it to invoke the custom deallocator. One must either write a destruction function that invokes the custom deallocator, or call the placement delete function directly, as a function call.[6][9][8]

The former would resemble:[9]

void
destroy (T * p, A & arena)
{
    p->~T() ;   // First invoke the destructor explicitly.
    arena.deallocate(p) ;  // Then call the deallocator function directly.
}

which would be invoked from a program as:

A arena ;
T * p = new (arena) T ;
/* ... */
destroy(p, arena) ;

The latter would involve simply writing the destructor invocation and delete function call into the program:[8][13]

A arena ;
T * p = new (arena) T ;
/* ... */
p->~T() ;    // First invoke the destructor explicitly.
operator delete(p, arena) ;   // Then call the deallocator function indirectly via operator delete(void *, A &) .

A common error is to attempt to use a delete expression to delete the object. This results in the wrong operator delete function being called. Dewhurst recommends two strategies for avoiding this error. The first is to ensure that any custom allocators rely upon the Standard C++ library's global, non-placement, operator new, and are thus nothing more than simple wrappers around the C++ library's memory management. The second is to create new and delete functions for individual classes, and customize memory management via class function members rather than by using the placement syntax.[13]

Debugging

Placement new can also be used as a simple debugging tool, to enable programs to print the filename and line number of the source code where a memory allocation has failed. This does not require the inclusion of the Standard C++ library header <new>, but does require the inclusion of a header that declares four placement functions and a macro replacement for the new keyword that is used in new expressions. For example, such a header would contain:[10][14]

#if defined(DEBUG_NEW)
void * operator new (size_t size, const char* file, int line);
void * operator new[] (size_t size, const char* file, int line);
void operator delete (void * p, const char* file, int line);
void operator delete[] (void * p, const char* file, int line);
#define New new(__FILE__, __LINE__)
#else
#define New new
#endif

This would be employed in a program as follows:[10][14]

T * p = New T ;

The custom-written placement new functions would then handle using the supplied file and line number information in the event of an exception. For example:[10][14]

#include <new>
#include <cstdlib>

class NewError {
public:
    NewError(const char * file, int line) { /* ... */ }
    /* ... */
} ;

void *
operator new (size_t size, const char* file, int line)
{
    if (void * p = ::operator new (size, std::nothrow))
        return p ;
    throw NewError(file, line) ;
}

Placement delete

As noted above, there is no placement delete expression. It is not possible to call any placement operator delete function using a delete expression.[6][15]

The placement delete functions are called from placement new expressions. In particular, they are called if the constructor of the object throws an exception. In such a circumstance, in order to ensure that the program does not incur a memory leak, the placement delete functions are called. A placement new expression first calls the placement operator new function, then calls the constructor of the object upon the raw storage returned from the allocator function. If the constructor throws an exception, it is necessary to deallocate that storage before propagating the exception back to the code that executed the placement new expression, and that is the purpose of the placement delete functions.[6][2][4][15]

The placement delete function that is called matches the placement new function that was invoked by the placement new expression. So, for example, if the following code is executed, the placement delete function that is called will be operator delete(void *, const A &):[6][2][15]

#include <cstdlib>

struct A {} ;
struct E {} ;

class T {
public:
    T() { throw E() ; }
} ;

void * operator new ( size_t, const A & ) ;
void operator delete ( void *, const A & ) ;

int main ()
{
    A a ;
    T * p = new (a) T ;
    return 0 ;
}

This is why the pointer placement delete functions are defined as no-operations by the Standard C++ library. Since the pointer placement new functions do not allocate any storage, there is no storage to be deallocated in the event of the object's constructor throwing an exception.[6]

If no matching placement delete function exists, no deallocation function is called in the event of an exception being thrown by a constructor within a placement new expression. There are also some (older) C++ implementations that do not support placement delete (which, like the exception-throwing allocator functions, were an addition made to C++ when it was standardized) at all. In both such situations, an exception being thrown by a constructor when allocating using a custom allocator will result in a memory leak. (In the case of the older C++ implementations, a memory leak will also occur with non-placement new expressions.)[4][15]

References

  1. "Placement New/Delete". C++ Language and Library. Glen McCluskey & Associates LLC. 2000-06-26. http://glenmccl.com./nd_cmp.htm. Retrieved 2008-11-26. 
  2. 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 Ray Lischner (2003). C++ in a Nutshell. O'Reilly. pp. 72–73,128–129,310,623–625. ISBN 059600298X. 
  3. 3.0 3.1 3.2 3.3 Stanley B. Lippman (1997). C++ Gems. Cambridge University Press. pp. 386–389. ISBN 0135705819. 
  4. 4.0 4.1 4.2 4.3 Scott Meyers (1998-04-01). "Placement new and placement delete". Dr. Dobb's Journal (United Business Media LLC). http://ddj.com//showArticle.jhtml?documentID=cuj9804meyers&pgno=2. 
  5. 5.0 5.1 Kyle Loudon (2003). C++ Pocket Reference. O'Reilly. pp. 109–110. ISBN 0596004966. 
  6. 6.0 6.1 6.2 6.3 6.4 6.5 6.6 Nicholas Solter and Scott Kleper (2005). Professional C++. Wiley. pp. 458–461. ISBN 0764574841. 
  7. 7.0 7.1 Joe Buck (1997-05-12). "3.4. g++ won't accept the placement new syntax.". Frequently asked questions about the GNU C++ compiler. http://desy.de./user/projects/C++/g++faq/placement_new_syntax.html. Retrieved 2008-11-26. 
  8. 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 Dirk Vermeir (2001). Multi-paradigm Programming Using C++. Springer. pp. 113–115. ISBN 1852334835. 
  9. 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 Stroustrup, Bjarne (1997). The C++ Programming Language (Third Edition ed.). Addison-Wesley. pp. 255–256,576. ISBN 9780201889543. 
  10. 10.0 10.1 10.2 10.3 10.4 Gail Anderson (1998). "Object Storage Management". Navigating C++ and Object-oriented Design. Prentice Hall. pp. 345–356. ISBN 0135327482. 
  11. Stroustrup, Bjarne (1994). "Memory Management". Design and Evolution of C++. Addison-Wesley. ISBN 9780201543308. 
  12. Graham M. Seed and Barry J. Cooper (2001). An Introduction to Object-oriented Programming in C++ (Second Edition ed.). Springer. pp. 435–436. ISBN 1852334509. 
  13. 13.0 13.1 Stephen C. Dewhurst (2003). "Gotcha #62: Replacing Global New and Delete". C++ Gotchas. Addison-Wesley. pp. 173–176. ISBN 9780321125187. http://informit.com./articles/article.aspx?p=30642&seqNum=3. 
  14. 14.0 14.1 14.2 Wu Yongwei (2007-12-31). "A Cross-Platform Memory Leak Detector". Wu Yongwei's Programming Page. http://wyw.dcweb.cn/leakage.htm. Retrieved 2008-11-26. 
  15. 15.0 15.1 15.2 15.3 Gail Anderson (1998). "Exception Handling". Navigating C++ and Object-oriented Design. Prentice Hall. pp. 631–632. ISBN 0135327482. 

Further reading

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...