vendredi 26 juin 2015

new expression evaluation order (pointer assignment)

Consider the following code:

Class* p = nullptr; //global var

This code executed by thread 1:

p = new Class;

This code executed on thread 2:

if (p != nullptr) ...; // does the standard gurantee that the pointer will be assigned only after object is constructed ?

My question is does the standard enforce when p will be assigned to point to allocated memory ? Example 1:

  • new expression call operator new
  • p is assigned to point to newly allocated memory
  • Class's c`tor is invoked and allocated memory is passed to it

Example 2:

  • new expression call operator new
  • Class's c`tor is invoked and allocated memory is passed to it
  • p is assigned to point to newly allocated memory

Aucun commentaire:

Enregistrer un commentaire