c++ - Strange bug with unique_ptr -


i have friend showed me code gives different results in release or debug configuration in vs2012. there's no multithreading, became interested why happens. boils down this: 1 class has member

std::unique_ptr<double[]> m_oldro; 

in loop do:

if(m_oldro[i] < ro_min)     ro_min = m_oldro[i]; 

ro_min defined double initial value 10000. in debug ro_min ends being equal 9.999997e-001. in release ro_min equals -3.818927e+005. things become strange:

1) if add such code before if statement, result changes normal:

double doublevalue = 0; for(size_t k = 0; k < m_triangles; ++k) {     doublevalue  = m_oldro[k];     std::cout << "doublevalue = " << doublevalue << "\n"; } 

result:

doublevalue = 1.000000e+000 doublevalue = 1.000000e+000 doublevalue = 9.999997e-001 doublevalue = 1.000000e+000 doublevalue = 1.000000e+000 doublevalue = 1.000000e+000 doublevalue = 9.999999e-001 doublevalue = 1.000000e+000 doublevalue = 1.000030e+000 doublevalue = 1.000030e+000  ro_min = 9.999997e-001 

2) if add code, result changes wrong:

double doublesum = 0; for(size_t k = 0; k < m_triangles; ++k) {     doublesum  += m_oldro[k];     std::cout << "doublesum = " << doublesum << "\n"; } 

result:

doublevalue = 1.000000e+000 doublevalue = -3.818927e+005 doublevalue = 9.999997e-001 doublevalue = 3.818947e+005 doublevalue = 1.000000e+000 doublevalue = 1.000000e+000 doublevalue = 9.999999e-001 doublevalue = 1.000000e+000 doublevalue = 1.000030e+000 doublevalue = 1.000030e+000  doublesum = 1.000000e+000 doublesum = -3.818917e+005 doublesum = -3.818907e+005 doublesum = 4.000000e+000 doublesum = 5.000000e+000 doublesum = 6.000000e+000 doublesum = 7.000000e+000 doublesum = 8.000000e+000 doublesum = 9.000030e+000 doublesum = 1.000006e+001  ro_min = -3.818927e+005 

print explanation, it's enough add or assign value of m_oldro[k] change programm behaviour in release version. have idea why happens?


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -