# Moving a variable declaration affects unit test...

## Recommended Posts

So if I declare my class this way:

private:
vector<ContractInfo*> obligations;
map<EconActor*, double> borrowers;
double discountRate;


then my unit tests fail. But if I do it this way:

private:
double discountRate;
vector<ContractInfo*> obligations;
map<EconActor*, double> borrowers;



they pass. My heart fills with unbounded joy at the thought of tracking this one down...

I suspect I have a destructor somewhere that I ought to have declared virtual, but didn't. Any better guesses?

##### Share on other sites

Maybe you use one member in the constructor for initializing the other and as the construction order depends on their declaration order it may change the outcome.

##### Share on other sites

Nope, the members are all independent. Good one, though - I'll keep it in mind for the future.

##### Share on other sites

Turns out I was looking up an un-initialised variable, interpreting it as double, and consistently getting NaN in one case and some small but numerical value in the other. Not sure why the variable location should so consistently flip me between NaN and number, though. Unless perhaps it is the struct layout that does it; the value in question comes from dereferencing the end of that map.

##### Share on other sites

You may have changed the size of the class due to additional (or few) pad bytes inserted by the compiler to correctly align the members. Check sizeof(the type) before and after, or use /d1reportSingleClass layout in VS.

##### Share on other sites

Turns out I was looking up an un-initialised variable, interpreting it as double, and consistently getting NaN in one case and some small but numerical value in the other. Not sure why the variable location should so consistently flip me between NaN and number, though

Probably just coincidence...

Even though the value in this case is "undefined", in practice, it can be deterministic.

It probably was initialized as part of another object, that then went out of scope, and the memory then reused for this object.

Edited by Olof Hedman

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628320
• Total Posts
2982057

• 12
• 9
• 9
• 13
• 11