Initialize an array, place new ones, read variables, define behavior?

a class is defined, the only member of which is char[10]

, which has no inheritance or virtual members, which has a constructor that does not mention the array in any way (such that it gets default initialization -> no initialization, like so:

class in_place_string {
    char data[10];

    static struct pre_initialized_type {} pre_initialized;
    in_place_string(pre_initialized_type) {}  //This is the constructor in question

    in_place_string() :data() {} //this is so you don't yell at me, not relevent
};

      

Is this behavior defined for placement - new this class into a buffer that already has data and then read from an array element?

int main() {
    char buffer[sizeof(in_place_string)] = "HI!";
    in_place_string* str = new(buffer) in_place_string(in_place_string::pre_initialized);
    cout << str->data; //undefined behavior?
}

      

I'm sure it's not that well defined, so I'm asking if this is an implementation or undefined behavior.

+3


source to share


2 answers


I think the relevant proposal is 8.5 [dcl.init] clause 12:

If no initializer is specified for an object, the object is initialized by default. When storage of an object with automatic or dynamic storage duration is achieved, the object has an undefined value, and if no initialization is performed on the object, that object retains the undefined value until that value is replaced (5.17). [Note. Objects with static or duration of storing streams are zero-initialized, see 3.6.2. -end note] If an undefined value is generated by an evaluation, the behavior is undefined, except in the following cases:

  • If an undefined value of unsigned narrow character type (3.9.1) is obtained by evaluating:
    • the second or third operand of the conditional expression (5.16),
    • right operand with comma (5.18),
    • a cast operand or conversion to unsigned narrow character type (4.7, 5.2.3, 5.2.9, 5.4) or
    • expression with a discarded value (section 5), then the result of the operation is undefined.
  • If an unsigned narrow character undefined value is constructed by evaluating the correct operand of a simple assignment operator (5.17) whose first operand is an unsigned narrow character lvalue, the undefined value replaces the value of the object referenced by the left operand.
  • If an unsigned narrow character type undefined value is generated by evaluating an initialization expression when an object of the unsigned narrow character type is initialized, that object is initialized to an undefined value.


I don't think any exception applies. Since the value is read before initialization after the object is created, I think the code results in undefined behavior.

+1


source


You are not doing reinterpret_cast

(which is unsafe since the class has non-trivial initialization); you are creating a new object whose member is not initialized.

Doing an lvalue-> rvalue conversion on an uninitialized object gives undefined and undefined behavior. So the object is uninitialized?

According to 5.3.4, all objects created by a new expression have dynamic storage duration. There is no exception for posting a new one.

Objects created by new expression have dynamic storage duration



And then 8.5 says

If no initializer is specified for an object, the object is initialized by default. When storage of an object with automatic or dynamic storage duration is achieved, the object has an undefined value, and if no initialization is performed on the object, that object retains the undefined value until that value is replaced (5.17). [Note. Objects with static or thread storage duration are zero-initialized, see end note] If an undefined value is generated during evaluation, the behavior is undefined, except in the following cases:

and the following cases are admitted only unsigned char

, and even then the meaning is not useful.

In your case, the new object has dynamic storage duration (!), And its non-initialized members are undefined. Reading them gives undefined behavior.

+4


source







All Articles