2

Just to give some background on me. I'm an experienced C# developer switching to a c++ project. I did c++ for years early in my career but am a bit rusty... With that said. Here's the simplest example I can come up with to show my issue:

#include <vector>
#include <iostream>

using namespace std;

struct Inner
{
public:
    float A;
    unsigned short B;    

    Inner()
        :A(0.0F), B(0)
    {
    }

    Inner(const float& valA, const unsigned short& valB)
        : A(valA), B(valB){}        
};

class Outer
{
private:
    const vector<Inner> CollectionOfInner;

public:
    Outer(const vector<Inner> initialVal = { Inner(1.0F, 4), Inner(9.0F, 4), Inner(81.0F, 18) })
        : CollectionOfInner(initialVal)
    {
        cout << "Values" << '\n';
        for (int i = 0; i < 3; i++)
        {
            cout << "A: " << CollectionOfInner[i].A << " B: " << CollectionOfInner[i].B << '\n';
        }
    }    
};

int main()
{
    Outer test;
}

My program simply creates a stack instance of Outer relying on the default parameter shown above. In debug everything works as expected giving the following output:

Values
A: 1 B: 4
A: 9 B: 4
A: 81 B: 18
Press any key to continue . . .

However, in Release configuration only the first of the three Inner instances is properly initialized and the output is the following:

Values
A: 1 B: 4
A: 4.59009e-039 B: 4138
A: 2.64788e-038 B: 52345
Press any key to continue . . .

I'm sure this has something to do with some optimization that is turned on in release... but for the life of me I can't imagine why you would EVER want this behavior... At first I thought there was just some sort of memory trashing going on in my larger project, but once I confirmed the "bug" with a dirt simple project like this I knew something more fundamental was going on. Note: If I supply a pre-created vector to the Outer constructor everything is fine. The problem appears only when relying on the default constructor. Any Ideas? Also, for reference this is done in Visual Studio 2013 using the standard C++ console application project template.

11
  • for each (Inner value in CollectionOfInner) that's not C++. O.o Jun 15, 2015 at 21:39
  • works fine in C++ 11 :)
    – armstrom
    Jun 15, 2015 at 21:40
  • 2
    After testing it, it works perfectly with both gcc and clang on linux. If I remember correctly VS2013 had a few bugs in their initializer list implementation that got later fixed, so that might be the culprit. I'd suggest making sure that you have the latest update (update 4 I think) or maybe try it with the VS2015 preview. Or try doing it without the initializer list. gcc version, clang version Jun 15, 2015 at 21:59
  • 2
    @RaphaelMiedl Yep, they had more than a few bugs with initializer lists, so much so that Update4 rolled back compiler support for most uses of braced-init-lists. Prior to that there was lots of weird behavior with elements in initializer lists being destroyed prematurely, which is probably what armstrom is running into.
    – Praetorian
    Jun 15, 2015 at 22:38
  • 2
    @armstrom Your example behaves as expected on VS2015 in Release mode. For VS2013 you'll have to come up with a solution that does not involve using vector's initializer list constructor.
    – Praetorian
    Jun 15, 2015 at 22:42

1 Answer 1

1

Consensus seems that this is a bug in Visual Studio 2013. Answering this myself to resolve the question. Thanks everyone!

3
  • You didn't share an MVCE so nobody can determine that for sure.
    – kfsone
    Jun 16, 2015 at 0:45
  • @kfsone to be honest the missing stuff for a MVCE is as simple as it gets, it's just a main function creating a dummy Outer object... I added it into OP's question for now. Jun 16, 2015 at 0:51
  • @RaphaelMiedl it's often that last line than can make or break, c.f. someone earlier with weird compiler errors all tracked down to an auto it : ... instead of auto& it : ...
    – kfsone
    Jun 16, 2015 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.