Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would not rely on gcc compiler flags to do this. Compiler flags can change across versions of gcc, and do change across compilers. You may find yourself needing to debug the same code in six months in Visual C++...</p> <p>@CharlesBailey makes a nice suggestion for how to do this with an <code>extern</code> variable. Here's one alternative that doesn't require a variable to be exposed to the entire module or kept in static storage.</p> <p>Declare a temporary variable <code>volatile</code> in the scope of the <code>if</code> statement:</p> <pre><code>if (volatile bool dbg = false) { do_some_debug_printing_and_checking(); } </code></pre> <p>This keeps the scope of the temporary variable quite narrow. The <code>volatile</code> qualifier does not let the compiler assume anything about the variable, or optimize the branch away.</p> <p>One thing to keep in mind is that the variable is always allocated on the stack, and will be kept on the stack until the function exits. Both this approach and the <code>extern</code> approach should work, but have slightly different (and probably negligible) tradeoffs.</p> <p>If you're willing to use macros to help solve this problem, then you can easily disable the temporary variable when your release your code into production:</p> <pre><code>#ifndef IS_DEBUGGING # define IS_DEBUGGING 0 #endif #if IS_DEBUGGING # define TMP_DBG_FLAG volatile bool dbg_flag = false #else # define TMP_DBG_FLAG false #endif </code></pre> <p>Then declare your <code>if</code> statement as:</p> <pre><code>if ( TMP_DBG_FLAG ) { do_some_debug_printing_and_checking(); } </code></pre> <p>When you define IS_DEBUGGING to be 1, the local variable is created, declared volatile, and kept. When you define IS_DEBUGGING to be 0, the macro expands to the constant <code>false</code> and the compiler optimizes the branch away. Something very similar could be done for the <code>extern</code> approach, as well.</p> <p>This few extra lines of code, but they're independent of the number of times you use TMP_DBG_FLAG. The code is also a lot more readable than using tons of <code>ifdef</code>s. The macro could be made a bit safer (by appending the value of <code>__LINE__</code> to it), but this would require three macros, and is probably not necessary:</p> <pre><code>#if IS_DEBUGGING // paste symbols 'x' and 'y' together # define TMP_DBG_FLAG_SYMCAT0(x,y) x ## y // need one level of indirection to expand __LINE__... # define TMP_DBG_FLAG_SYMCAT(x,y) TMP_DBG_FLAG_SYMCAT0(x,y) # define TMP_DBG_FLAG volatile bool TMP_DBG_FLAG_SYMCAT(dbg_flag_,__LINE__) = false #else # define TMP_DBG_FLAG false #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload