ArticleslgStudy

science

Placement syntax

Placement syntax is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Placement syntax rather than just read about it. In short: Placement syntax in the C++ programming language refers to the syntax for explicitly specifying 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.

Key takeaways

  • Placement syntax belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Placement syntax to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Placement syntax from memory before moving on to harder problems.

Reference excerpt

Placement syntax in the C++ programming language refers to the syntax for explicitly specifying 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. 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. 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) is a placement new or placement delete function. A placement new function takes two input parameters: size_t and void*.

History In earlier versions of C++, there was no such thing as placement new; instead, developers used explicit assignment to this within constructors to achieve similar effect. This practice has been obsolete and removed by C++98, and the third edition of Stroustrup's book, The C++ Programming Language, does not mention this technique.

Expressions The C++ syntax for a non-placement new expression is

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.

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

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:

The C++ Standard Library provides two placement overloads each for these functions. Their declarations are:

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 specifying 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. There are also placement delete functions. They are overloaded versions of the non-placement delete functions. The non-placement delete functions are declared as:

The standard library provides two placement overloads each for these functions. Their declarations are:

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. Operators new and delete must reside in the global namespace and cannot have static linkage.

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 library, which it is not permitted for a C++ program to replace or override, are:

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. Other uses, however, include calling a constructor directly, something which the C++ language does not otherwise permit. 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, i.e. p->~X();.

Use cases Placement new is used to prevent operator new from allocating memory (such as if it has been pre-allocated and one wishes to place the object there), but still construct the object. Examples of typical situations where this may be required are:

To create objects in memory shared between two different processes. To ensure objects are created in non-pageable memory. To separate memory allocation from construction e.g. in implementing a std::vector<T> (see the reserve() method). The constructor, unlike other functions, is called when there is no object (only memory), and when it finishes executing, the memory is now a fully initialized object. Therefore, the constructor cannot be called on an object, however, it must access (and initialize) non-static members. This makes calling the constructor directly an error. The solution is the placement form of operator new. This operator is implemented as:

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. Usage of this requires header <new> from the standard library (or equivalently, module std). 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 std::nothrow_t& as their second parameter. For example:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Placement syntax

Start with the simplest possible case. Write down what Placement syntax claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Placement syntax before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Placement syntax ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Placement syntax

In research
Placement syntax appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Placement syntax in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Placement syntax is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, so understanding it makes those chapters shorter.
In everyday life
Look for Placement syntax outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Placement syntax” →

How to study Placement syntax in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Placement syntax means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Placement syntax out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Placement syntax in simple terms?

Placement syntax in the C++ programming language refers to the syntax for explicitly specifying 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…

Why does Placement syntax matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Placement syntax?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Placement syntax.

Tags

  • C++

Keep exploring