C++ loop hangs if any code is added inside if Condition -


i have following code strange behaviour:

struct mime {     int key;     string value; };  class storage {  private:      int size;     mime * _storage;     int last = 0;  public:      storage(int __size) {         size = __size;         _storage = new mime[size + 2];     }      void add(const mime & __mime) {          (int x = 0; x < last; x++)             if (_storage[x].key == __mime.key) {}          _storage[last++] = __mime;     } };  void test2() {      int thissize = 1000000;     storage storage(thissize);     auto start_t = chrono::high_resolution_clock::now();     (int = 0; < thissize; i++) {         mime temp;         temp.key = i;         temp.value = "hey";         storage.add(temp);     }     cout << chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start_t).count() << " milliseconds\n" << endl; }   int main() {      test2();      cout << "done" << endl;      return 0; } 

this code run in 120 milliseconds me. when add code here inside {}

if (_storage[x].key == __mime.key) {} 

like

if (_storage[x].key == __mime.key) { return; } 

or else... program runs in 10 mins or hangs adding return; or other code. when add return; or other code in if condition, program hangs. in (if) nothing happens in process because doesn't seem run return; or other code.

any suggestions?

there's nothing strange behavior. it's optimizer recognizes for loop safely not executed @ if has empty block inside.

the optimizer says:

for (int x = 0; x < last; x++)     if (_storage[x].key == __mime.key) {} 

look has no side effects...which mean, bunch of lines if executed not change state of program, so, i'd better save time , not execute it.

try compile -o0 still long runtime.


in order double check cause, 1 can trick compiler , force keep code using volatile

so following code won't optimized away , not matter content of for

  (volatile int x = 0; x < last; x++){       //whatever    } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -