0

I am wary of using = for initializing a new variable:

MyClass my_var = MyClass(some_data);
  1. Because I believe that this requires my_var to first be default constructed, and then assigned.
  2. When my_var is shared memory, this can cause race conditions.

Can someone confirm or deny my fears? Are primitive types treated differently?

Elliot Cameron
  • 5,235
  • 2
  • 27
  • 34
  • 2
    http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializati – NPE Jan 16 '14 at 21:39

3 Answers3

2

Because I believe that this requires my_var to first be default constructed, and then assigned.

No. Using the = operator on a variable declaration of a class type has special handling. The compiler will never default-construct-then-copy the variable object like you think. What will actually happen instead is either:

  1. a temp MyClass object will be copy-constructed from some_data, then my_var will be copy-constructed from the temp, then the temp will be freed. As if you had written this:

    MyClass my_var(MyClass(some_data));
    
  2. the compiler will optimize away the temp completely and simply copy-construct my_var from some_data directly. As if you had written this:

    MyClass my_var(some_data);
    

    This is the usual case, especially if you write this:

    MyClass my_var = some_data;
    

    Instead of this:

    MyClass my_var = MyClass(some_data);
    

When my_var is shared memory, this can cause race conditions.

The way you have written it, no. my_var is either a local variable of a function/method, or it is a global variable. Either way, declaring and assigning a variable in the same statement is not a race condition since the variable cannot be shared until after it has been constructed. If you declare the variable first and assign it in a separate statement, then there would be a race condition.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • What if `my_var` is a function-local static? – Elliot Cameron Jan 16 '14 at 22:08
  • A function-local static is only initialized once, when the function is called for the first time. There would be a race condition only if multiple threads call that function at the same time. But once it has been constructed, there is no more race condition on construction, just on access (if the logic inside the object is not thread-safe). – Remy Lebeau Jan 16 '14 at 22:13
  • So there *is* a race condition if the function is called "for the first time" by two different threads simultaneously. – Elliot Cameron Jan 17 '14 at 21:45
  • That is what I said, yes. – Remy Lebeau Jan 17 '14 at 22:06
1

Because I believe that this requires my_var to first be default constructed, and then assigned.

No. my_var is never default constructed, nor is the temporary. An rvalue of type MyClass is first constructed, using some_data, and then the copy constructor (or move constructor if you are in C++11) is called to construct my_var.

When my_var is shared memory, this can cause race conditions.

Well, yes. Theoretically it could cause race conditions.

Shoe
  • 74,840
  • 36
  • 166
  • 272
0

In your code line, only the copy-constructor of MyClass is called.

If you wrote MyClass my_var and later in the code my_var = some_data, then MyClass::operator=(...) would be called.

To complete this answer, I don't see how a shared variable can be "in risk of a race condition" during construction (becaused it cannot be shared until it has been constructed).

barak manos
  • 29,648
  • 10
  • 62
  • 114