Good time stork. I have this code.

dburl = db_short_url_find( conf, &(RQ) ); if( dburl == NULL ) { hash = ngx_hash_strlow( RQ.data, RQ.data, RQ.len); dburl = ngx_hash_find(&conf->short_url_hash, hash, RQ.data, RQ.len); /* if not found, use {RQ} */ if( NULL == dburl ) { dburl = &(RQ); } } 

in the second if condition, the condition is always satisfied, even when (I saw it myself) the bdurl is no longer NULL, apparently optimization. The same thing happens if if to bring out (initially it was) and put after the first if-a. dburl == NULL or NULL == dburl is also not important.

 #pragma GCC push_options #pragma GCC optimize ("что-нить") .... #pragma GCC pop_options 

Perhaps as an option. But ... firstly, the function is large, and I would not want to deprive it of all optimization, and secondly, what option is responsible for "such optimization" ?? In general, you need either the correct pragma, preferably on a piece of code, or a trick to bypass the "smart" compiler ... I will be glad to any constructive ideas.

  • one
    Optimization can never make such code not working, unless of course this is the correct optimization. Most likely you simply have a dburl pointer that is always always NULL, you can output it to the console (printf or cout) before checking if and see what it really is. In addition, a reliable way to generally prevent variable optimization is to add the volatile to it in the declaration. - Arty OneSoul
  • Well, yes, of course, can not ... step by step passed, the value looked, the next step and oops! [here, you can see] ( s019.radikal.ru/i603/1710/50/aae2fe7539e9.png ) pop-up message, shows the value of the variable. red stripe, pointer program counter. but at the expense of volatile, this is correct ... I forgot it ... thanks - EugenOS
  • It’s not always right to rely on the readings of the debugger, optimization does prevent the debugger, and sometimes it shows incorrect values. The most reliable way is to output the value somewhere, either to the console, or to a file, or by a message on the screen, depending on where your program can output. Simply, the optimizer can never violate the logic of the program, always optimizing that the logic will not be destroyed. By the way volatile tried to put? I’m sure if you log to the log or console, it turns out that there is 0. - Arty OneSoul
  • It is also quite possible that the debugger shows incorrectly that the execution went into the if body, if there is not zero in dburl, the debugger can show what went into the body when it was not actually entered. To make sure exactly, put the dburl value on the console or the log before the if, and in the if body, put a message saying that we’ve logged into the body. The output to the console or to the file is never optimized and you will definitely see the truth. - Arty OneSoul
  • By the way, tried with voatile, - the same hogwash. Now, just strange. And on the debugger side, on the one hand I agree, but I don’t just see the pointer. pointer points to the data item found. those. all as it should, only the transition to "no". I will try to organize a conclusion to the console. - EugenOS

1 answer 1

In the stupid recreated the project. Rebuilt. And it all worked. I did not understand what happened.

  • Like what? The clumsy build script and debugger that is not able or not configured to control the correctness of the source code. You think that you are debugging the actual code - and in fact the code is three days old. - Pavel Mayorov