1

While porting a VS6 project to VC++2012 environment, I'm experiencing a strange behavior...

Let's say I've the following

// double AreaIco = 75.0;
// double theApp.m_GlobalScale = 0.25;
double ToLong(double); // elsewhere defined
double result = ToLong(AreaIco * theApp.m_GlobalScale * theApp.m_GlobalScale);

What I find is that the ToLong function gets "0" as input parameter

This also happens if I try to introduce temporary variables:

double temp1 = AreaIco * theApp.m_GlobalScale;
double temp2 = temp1 * theApp.m_GlobalScale;
AreaIcoInScala = ToLong(temp2);

Both temp1 and temp2 evaluate to either 0 or a denormalized value.

However, trying to evaluate the expression in QuickWatch returns correct value.

Does anyone have any clue for such a behavior? I fear there's some ancient bug in the code, which has been covered since now by somethink in VS6...

Thanks anyone for support, anyway.

Marco Veglio
  • 332
  • 1
  • 8
  • What is ToLong? Did you forget a tag? – manuell Dec 04 '13 at 21:50
  • What is the parameter type for `ToLong`? And what is the difference between `theApp.m_GlobalScale` and `theApp.GlobalScale`? – Michał Dec 04 '13 at 22:56
  • The problem is obviously with `ToLong`, which you didn't show. Where and how is this function defined? The only `ToLong` I know is a .NET function, which was therefore not available in VS6. – Roger Rowland Dec 05 '13 at 05:46
  • I'm sorry, that function is defined in the project. However the issue is not there, since 1) the function is actually receiving a wrong input parameter - the multiplication shown in my post evaluates to either 0 or a denormalized value 2) If I try to make the multiplication, assign it to a temporary value (temp1 and temp2) and pass that temporary to the ToLong() function, the temporary itself shows a bad value (either 0 or denormalized); in this case the ToLong is not called at all. – Marco Veglio Dec 05 '13 at 07:05
  • In which case, the following should also resolve to zero: `double temp1 = 75.0 * 0.25;` and I bet it doesn't. So, there must be something you're not showing us. Have you checked that the operands are not being corrupted after initialisation? – Roger Rowland Dec 05 '13 at 08:33

1 Answers1

1

After some futher investigation, we found it was a problem with data structure alignment. In VS options the Struct Member Alignment was set to 4 bytes, while a #pragma pack 2 was present in one of the source files.

Strange enough, everything worked under VS6, the porting to VS2012 made this issue come out.

Marco Veglio
  • 332
  • 1
  • 8